# if statement

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-11-2003
ZakkWylde969
if statement
I came across this program in my book and read about it.

Code:

```#include <iostream> using namespace std; int main() {         long number=0;         cout << endl                 << "Enter an integer number less than 2 billion: ";         cin >> number;         if(number % 2L)                 cout << endl                         << "your number is odd." << endl;         else                 cout << endl                     << "your number is even." << endl;         return 0; }```
I don't quite understand the if(number % 2L) statement really means. A ran on google and went to a couple open chats about it and got nothing. All I can make of this is.

The if statment runs starts the if loop.

Number is the variable.

What does the % 2L mean?
• 07-11-2003
quzah
% is the modulus operator.

What this does is it gives you the remainder of the number divided by whatever number is to the right.

The L tells the system that the number 2 should be represented as a long instead of an int, which is what it would treat it as if you didn't add the L.

Quzah.
• 07-11-2003
Eibro
% (Modulus) gives the remainder after a division operation. So if number isn't a multiple of 2, the if statement will be true. For example: 4 % 2 = 0, as 4 goes evenly into 2. 5 % 2 = 1, as 5 doesn't go evenly into 2. Basically, number % 2 tests whether a number is even or odd. The 'L' on the end of the 2 ensures the compiler treats it as a long.

edit: Beaten :(
• 07-11-2003
ZakkWylde969
Ok. So if(number % 2L) would mean divide my number by 2. and turn it into a long variable. So should my output be switched? So the correct code would have the else as odd and the even as if?
• 07-11-2003
bennyandthejets
No, the modulus operator returns the remainder of a division. You're not actually changing the value or type of your variable. If (number%2L) resolves to 1, the first code will run. Otherwise, the second code will run.
• 07-11-2003
ZakkWylde969
:confused:
• 07-11-2003
quzah
Quote:

Originally posted by ZakkWylde969
:confused:
What's not to understand?

cout << "7 / 3 is " << 7 / 3 << endl << "7 % 3 is " << 7 % 3 << endl;

You do know basic division, right? Well when you aren't resolving to the decimal point, you use the remainder. The % is used to give you the remainder of a number divided by another.

Quzah.
• 07-11-2003
o0obruceleeo0o
Well they already explained it, but maybe more examples will help :). Like they said above, % just gets the remainder of the devision operation. Therefore 10%2=0 and 10%3=1. 10/2 devides cleanly, no remainder, whereas 10/3 is 3 with a remainer of 1(hence 10%3=1). Whenever you do x%2, you are basically checking if the number is even or odd (even if the result is 0, odd if the result is 1). Well, hope that helped!
• 07-11-2003
bennyandthejets
On a different note, I was thinking about even and odd numbers. In terms of binary, the difference between them is simply the first bit. So maybe a better way to check it is to check if (number&1) is true. Division is CPU intensive isn't it? A bitwise check should be quicker (technically, not practically).
• 07-11-2003
ZakkWylde969
I know division but I don't see how 10 / 2 equils 0. It equals five. I don't know what math calsses you guys took but sofar my teachers have tought me 10 split in 2 is five..
• 07-11-2003
quzah
Quote:

Originally posted by ZakkWylde969
I know division but I don't see how 10 / 2 equils 0. It equals five. I don't know what math calsses you guys took but sofar my teachers have tought me 10 split in 2 is five..
Re-read his post. He never said that 10/2 is zero. He said:
Quote:

% just gets the remainder of the devision operation. Therefore 10%2=0
And then:
Quote:

10/2 devides cleanly, no remainder,
Perhaps you need to pay more attention. Your problem would have been solved in the first reply.

Quzah.
• 07-11-2003
ZakkWylde969
I might just be retarded but I don't get a lick of this.
• 07-11-2003
quzah
Quote:

Originally posted by ZakkWylde969
I might just be retarded but I don't get a lick of this.
I'm inclined to agree. Did you even compile the simple cout line I gave you as an example? Perhaps programming is just not for you.

Quzah.
• 07-11-2003
ZakkWylde969
Quote:

Originally posted by quzah
I'm inclined to agree. Did you even compile the simple cout line I gave you as an example? Perhaps programming is just not for you.

Quzah.

I'm glad I have your support. I'm just not understanding. You guys are explaining it over and over in the SAME words. MAybe a more detailed explanation would help other than it gives you the remainder divided by blah blah. I'm just not understanding.
• 07-11-2003
o0obruceleeo0o
Sorry Zakk, but there's just not many ways to explain it. Ok, you know division so you know that 10/5=2. You should also know that 10/5 doesn't have a remainder. If the remainder part is getting you, when dividing a remainder is what is left over. For example: 10/6=1 with a remainder of 4. 10/3=3 with a remainder of 1. 10/3 has a remainder of 1 because 3x3=9 and that leaves 1 left over(to make it = 10). After learning what a remainder is, please refer to my orignal post :D
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last