If else and logic problems.

I recently started learning C++ and am just embarking on learning C. I came across a small problem that I don't know if I can solve simply. I was setting up an if else conditional statement where if X is even, it does one thing and if x is odd, it does another. To test if it is even I use the modulus operator. I can do division for the odd and if the number is a non integer than x is odd. How can I do this? Am I going about it the right way? (The "non integer" is not part of the code, I just don't know what I would place there which is what I am asking, mainly.)

1
2
3
4
5
6
7
8
9
10
11
12
if (x%2=0)
	{
		z=x*y
		x=z
		cout << x;
	}
else if (x/2= not integer)
	{
	 	z=x/y
		x=z
		cout << x;
	}
Last edited on
Figured it out. I can use (x/2)%.5=0 as the condition.
Last edited on
You are somewhat close. To begin, before we go into how to do the statement, I'd like to mention the difference between = and ==. The single equals sign is used for assigning a value to a variables. It can be changed slightly with some other symbols, namely the plus or minus sign. Examples:
1
2
3
4
5
6
7
int x = 5; // sets x to 5
int y; // y is not set yet
y = 8; // sets y to 8
x = y; // changes x from 5 to y's value, which means both x and y are now 8
y = x + 1; // y now is x(8) + 1, or 9
x += y; // x is increased by y, so it currently is 8, but after adding y(9) is 17
y -= 5; // subtracts 5 from y, so y goes from 9 to 4 


Double equal signs on the other hand are not for setting variables, but comparing values. They see if 2 values are the same. If they are the same, it returns true, or 1, but if they are different, it returns false, or 0. It can be expanded a bit with symbols like the greater then sign or the lesser then sign, or the not sign (exclamation sign). Examples:
1
2
3
4
int x = 5, y = 4;
if(x == y); // this statement will return false, since they are not the same
if(x > y); // this statement will return true, since x is greater then y
if(x != y); // this statement will return true, since x is not equal to y 


Lastly, for your problem, this should work:
1
2
3
4
5
6
7
8
9
10
11
12
13
int x = 11, y = 3, z = 0;
if (x%2 == 0) // no remainder
{
	z = x*y;
	x = z;
	cout << x;
}
else // no need for another if statement to see if it is odd, since we know it is
{
	z = x/y;
	x = z;
	cout << x;
}
Topic archived. No new replies allowed.