IF and the % question.

This is a discussion on IF and the % question. within the C Programming forums, part of the General Programming Boards category; Hello all, It has been several years since I have looked at form of a computer language, and of course ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    5

    IF and the % question.

    Hello all,
    It has been several years since I have looked at form of a computer language, and of course I am working to learn C first.

    I purchased a great book, well great so far, C Programming A Modern Approach, Second Edition. Of course after each chapter there are questions and exercises to complete.

    During the course of completing the exercises I came across one that has raised a couple questions.

    I am including the code prior to asking my question as it will most likely turn out to be a very basic answer, however I can not wrap my head around it totally and it is causing some confusion.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    
       int i, sum;
       i = 0;
    
       for (i = 0; i <  10; i++) 
        {
         if (i % 2)
         continue;
        sum += i;
        }
       printf("%d\n", sum);
    
    
       return 0;
    }
    Now when the program is executing the
    Code:
     
    if(i % 2)
    it is testing for a result to be a TRUE or FALSE. Based upon what I have seen it is treating those numbers that have a remainder of 0 as TRUE. So 2, 4, 6, and 8 all return a TRUE and then the continue is executed and we drop out of the inner loop.

    However what has me truly confused, and this is where the simple explanation is flying over my head, why are 0 and 1 not matching and thus hitting the continue or is that actually what is happening and I am just not seeing it. I have tried to place several "test points" in the program to see the result during each loop with very mixed results.

    I understand this will be a basic explanation of the remainder operator, but for the life of me I am not grasping why the 0 and 1 which return a result of 0, as far as I can figure, are not also matching. I am curious if someone can perhaps show me a different approach to the remainder operator.

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In C, zero is "false", anything else is "true".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    false = 0, true = anything but false.

    In the example if(i % 2), it will execute the code block following the if statement if i is an odd number, since i % 3 is 1, or true, but i % 2 is 0, or false.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    5
    Quote Originally Posted by Nightowl View Post
    false = 0, true = anything but false.

    In the example if(i % 2), it will execute the code block following the if statement if i is an odd number, since i % 3 is 1, or true, but i % 2 is 0, or false.
    matsp and Nightowl thank you for the responses.

    Looking at the TRUE and FALSE I believe that is where my confusion is. When I walk through the program on paper I see where a TRUE and FALSE would be. Perhaps it is how I am applying the TRUE and FALSE and when.

    From what I believe the following would be a false,
    Code:
    if (1 % 2)
     continue;
    sum += i;
    There for the sum += i would be executed. However when the program runs it does not appear that this is happening and 1 is not being added to the value ofsum. Is that a correct assumption or have I misunderstood the way IF is doing its matching.

    Oddly enough when I have the following this also appears to return a TRUE yet I am seeing sum += i happening.
    Code:
    if (2 % 2)
     continue;
    sum += i;
    So from this it appears the the if is being matched however the sum += i results with a 2.

    The result of the program at the printf is 20 and the only method that I have been able to map out on paper to come up with that result by adding i to sum is having what I believe should be a FALSE appear for the IF test, case in point i = 2.


    There is something I am just not grasping based upon the TRUE and FALSE concept with this IF. Everything I see tells me that a FALSE means do sum += i and a TRUE means skip sum += i, however the actual result is the exact inverse.

    Thanks

  5. #5
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    The code you originally posted should print the sum of all the even numbers from 0 to 9. In other words, it should print (0 + 2 + 4 + 6 + 8), or 20.

    Which it does.

    From what I believe the following would be a false,
    Code:
    if (1 % 2)
     continue;
    Actually, that is a true, since 1 % 2 == 1. Therefore, it would continue the loop without adding i to sum.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    5
    Nightowl thank you, looks like it is time to break out the 20 year math books and read up more on the remainder/mod() operator.

    For the life of me I saw that as a 1 % 2 == 0, guess I did not carry it far enough.

    Thank you again.

  7. #7
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Not a problem. Just post again if you need any more help with anything.

    The most important thing to remember when dealing with C booleans is that false == 0, true == !false.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    5
    Quote Originally Posted by Nightowl View Post
    Not a problem. Just post again if you need any more help with anything.

    The most important thing to remember when dealing with C booleans is that false == 0, true == !false.
    LOL Will do.

  9. #9
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    looks like it is time to break out the 20 year math books and read up more on the remainder/mod() operator.
    Note that modular arithmetic from mathematics doesn't have much to do with the "modulo" operator in programming languages. In mathematics, the modulo operation establishes a congruency relation between three numbers and thus creates residue classes. In programming languages, the "modulo" operator computes the common residue of these residue classes, i.e. the remainder of a division.

    The name "modulo operator" is misleading. It does what every third-grader learns in school.


    If you're experiencing problems with if statements containing modulo division, write them down explicitly:

    if(a%b == 0)
    is true iff b is a divisor of a. Once you're used to modulo division, you can omit the test for equality, so the compiler doesn't have to.

    Greets,
    Philip
    Last edited by Snafuist; 03-19-2009 at 03:29 PM. Reason: typo
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    a&b is not a%b
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by vart View Post
    a&b is not a%b
    Hehe, that's a dirty typo. Fixed.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  12. #12
    Registered User
    Join Date
    Mar 2009
    Posts
    5
    Quote Originally Posted by Snafuist View Post
    Note that modular arithmetic from mathematics doesn't have much to do with the "modulo" operator in programming languages. In mathematics, the modulo operation establishes a congruency relation between three numbers and thus creates residue classes. In programming languages, the "modulo" operator computes the common residue of these residue classes, i.e. the remainder of a division.

    The name "modulo operator" is misleading. It does what every third-grader learns in school.


    If you're experiencing problems with if statements containing modulo division, write them down explicitly:



    is true iff b is a divisor of a. Once you're used to modulo division, you can omit the test for equality, so the compiler doesn't have to.

    Greets,
    Philip


    Philip thank you that is where I think I am still learning. The if(a%b == 0) being TRUE is what was confusing me and that example does help quite a bit.

    Thank you

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21