Thread: if statement

  1. #16
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Zack, % and / are not the same operators. We should make that clear. They ARE related, but not the same. First, let's do some division practise.

    There are ten apples, and three people. They want to divide fairly, but they dont want to break any into pieces. What happens? They each take 3, and leave one over. That one apple is the remainder. Now, in terms of C++, that equation is the following:

    10%3=1;

    10 modulus 3 equals 1. Or simply, if you divide 10 by 3 without decimal places, you'll have a remainder of 1.

    Now, it happens that using number%2 will give you even/odd status. For example: 2%2=0. 2 divided by 2 will give you no remainder. 3%2=1. 3 divided by 2 will give you 1 remainder. 4%2=0. 4 divided by 2 gives no remainder. So that is the basis of your program.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by ZakkWylde969
    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.
    Well you've stated you understand basic division, so you do know what a remainder is, right? You didn't answer my last question. Did you copy and paste that cout example I gave you? If so, the illustration should be quite clear.

    Here is a basic division, think grade school, lesson for you:
    Code:
       ____
    3 | 7
    Think back to grade school. This is how you wrote basic division problems. Given the above problem, what is your answer?

    It should be:

    "Seven divided by three is 2, with a remainder of one."

    Drawn out:

    3 x 2 = 6
    7 - 6 = 1

    Thus, you have a remainder of one when you divide seven by three.

    Ok, well the "%" operator is used to give you that remainder. So:
    Code:
    int number = 0;
    int remainder = 0;
    
    number = 7 / 3; //number now holds 2
    remainder = 7 % 3; //remainder holds 1
    
    //Thus:
    
    cout << "Number times three: " << number * 3 << endl;
    cout << "7 - 6 is " << 7 - 6 << "which is our remainder! See: " << remainder;
    If you can't follow that, give up. Programming is not for you.

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

  3. #18
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    parallelism to the max! We all posted the same post, within 2 minutes.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #19
    ___
    Join Date
    Jun 2003
    Posts
    806
    Originally posted by 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
    Thanks a bunch. That pretty much cleared it all up for me.
    "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. #20
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    Zakk, study the code that Quzah wrote for you. It is a very good and simple example and if you study it for a few hours it will all make sense to you . Maybe if you tried plugging in some different numbers in his code it will help as well...

  6. #21
    ___
    Join Date
    Jun 2003
    Posts
    806
    ^^^^^^ Look at my above post.
    "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. #22
    plzduntlakliekthiskthx
    Join Date
    Oct 2002
    Posts
    138
    Originally posted by ZakkWylde969
    Thanks a bunch. That pretty much cleared it all up for me.
    No problem, glad ya get it now . Postcount++;

  8. #23
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Boy...I can't even remember using remainders at all...the first I remember using is fractions...I think that was 4th grade
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

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