what does if(!(x%3)) mean?

Viewed 2737

I am attempting to solve a hw problem in which I need to write down what the program will output. I am stuck however, on the syntax "if ( !(i%3)). What does that really mean? Does it mean that the program is checking for any i that is divisible by three? aka, is the if statement only runs if i is divisible by three?

int main () {
for (int i=0; i<10; (i<3?i++;i+=2)) {
    if (!(i%3)) {
        continue;
    }
    else if (i%7 ==0) {
        break;
    }
    cout << i<< endl;
}
8 Answers

Does it mean that the program is checking for any i that is divisible by three? aka, is the if statement only runs if i is divisible by three?

Correct. The longer version of that check would be

if (i % 3 == 0)
    continue;

The most common use case for such branching is probably FizzBuzz.

İt means if i is not(!) divisible by 3 continue. For example if i is 3,6,9 it won't continue otherwise it will continue.

if (x) where x is int implicitly compared with zero. I.e. if (x) equals to if (x != 0). ! is negation. So if (!x) equals to if (x == 0). And the last step is if (!(i%3)) equals to if ((i%3) == 0) what is the same with check, that i deivisible by 3

The if() statement is false only if the result inside the parentheses is 0 (false). Take a look at your program:

i%3 may return 0 (false), 1 (true), or 2 (true)

The negation operator ! changes the result of the operation (i%3). So, if the i is divisible with 3 the statement will return 0 (false). Being negate, ! it will result in True. Otherwise the result of (i%3) will be true and with the operator ! the result of the hole statement will be false. Basically this code is checking if the value of i is divisible with 3.

Other options will be:

if (0==i%3)
{
/*code*/
}

Your code can be simplified as below

int main() {
    for (int i=0; i<10;) 
    {
        if (i % 3 == 0) {
            continue;
        }
        else if (i % 7 == 0) {
            break;
        }

        cout << i << endl;

        i = i<3 ? i+1 : i+2;
    }
}

When you write a integer variable like i as a condition, what happens is that if i==0 then the result of the condition is false, otherwise it would be true.

Let's check it out in your program, if(!(x%3)), let's name condition= !(x%3), when this condition is true? when x%3 == 0, note that the negation operator ! is behind x%3, so in this case the condition would be equal to true, more formally the condition is equal to :

if(x%3==0)

these kinds of conditions are common, check this example out :

int t = 10;
while(t--){
    cout<<t<<endl;
}

The above condition i.e if(!(i%3)) will true when " i is not disvisable by 3" Hope this helps.

In java and other languages there is a special type to represent booleans and evaluate expressions; in c and its variants there is no such thing, instead an expression is considered "true" if -taken as a integer number- is equal to 0; false for every other value. (Fun fact: this is why you usually end the code by return 0)

So if(x%3) in c is equivalent to if(x%3==0) in other languages. That said, if(x%3) execute the if body when the remainder of x/3 is 0, that is when x is a multiple of 3. In your code you have the ! before, that -as you may know- "inverts" the expression. That means that if(!(x%3)) can be read as "If the remainder of the integer division of x by 3 is not 0", or alternatively: "If x is not a multiple of 3".

So, basically, you saw it right.

Hope I helped!

Related