Reputation: 49
I am going to print no
if for loop i
is not divided by 3 and otherwise continue the loop
(i%3==0)? continue : (printf("no"));
am getting error on this line
Upvotes: 4
Views: 2194
Reputation: 224577
This will not work as the conditional operator is part of an expression whose value is either the second or third clause, and continue
is a statement.
The conditional operator can be used in ways such as this:
days_in_february = is_leap_year() ? 29 : 28;
So now imagine if what you wanted was allowed. If you did this:
val = (i%3==0)? continue : (printf("no"));
Then what would value
be set to if the condition was true? continue
is not an expression and therefore doesn't "have a value" so such a statement wouldn't make sense.
You need to use a standard if
statement to accomplish this:
if (i%3==0) {
continue;
} else {
printf("no");
}
Upvotes: 1
Reputation: 11
I would say you can do it with while loop easily :
while(1){
if(i%3 == 0)
i = i % 3;
}else{
printf_s("no");
break;
}
Upvotes: 1
Reputation: 520
No. In a ternary operator, both of the values must be expressions. continue
is a statement, not an expression. Use an if
statement instead.
Upvotes: 3