# 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. ## 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. In C, zero is "false", anything else is "true".

--
Mats

3. 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.

4. Originally Posted by Nightowl
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. 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.

6. 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. 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.

8. Originally Posted by Nightowl
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. 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

10. a&b is not a%b

11. Originally Posted by vart
a&b is not a%b
Hehe, that's a dirty typo. Fixed.

Greets,
Philip

12. Originally Posted by Snafuist
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