Thread: if statement

  1. #1
    ___
    Join Date
    Jun 2003
    Posts
    806

    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?
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    % 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.
    Hope is the first step on the road to disappointment.

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    % (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

  4. #4
    ___
    Join Date
    Jun 2003
    Posts
    806
    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?
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    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.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    ___
    Join Date
    Jun 2003
    Posts
    806
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by ZakkWylde969
    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.
    Hope is the first step on the road to disappointment.

  8. #8
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    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!

  9. #9
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    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).
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  10. #10
    ___
    Join Date
    Jun 2003
    Posts
    806
    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..
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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:
    % just gets the remainder of the devision operation. Therefore 10%2=0
    And then:
    10/2 devides cleanly, no remainder,
    Perhaps you need to pay more attention. Your problem would have been solved in the first reply.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    ___
    Join Date
    Jun 2003
    Posts
    806
    I might just be retarded but I don't get a lick of this.
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  14. #14
    ___
    Join Date
    Jun 2003
    Posts
    806
    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.
    "When I die I want to pass peacefully in my sleep like my grandfather did, not screaming and yelling like the passengers in his car."

  15. #15
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM