C If Statement Expression Solving
Expression Solving inside if Statement : C programming
Arithmetic expression can be solved inside if statement. In this case expression will be evaluated first and then result will be checked to decide the flow of code.
void main () { int num = 56; if(num % 2 == 0) // Expression printf ("\nNumber is Even"); else printf ("\nNumber is Odd"); }
Explanation of Statement :
Consider the if statement -
if(num % 2 == 0)
above if statement contain simple expression which will be evaluated like this -
Result of Expression = (num % 2 == 0) = (56 % 2 == 0) = (0 == 0)
Now we can say that result of above expression is true thus if statement condition becomes true and if block gets executed.
Expression Solving : Arithmetic Operation
We will consider different verities of statements , Please refer below table -
If Statement | Expression Result | Comment |
---|---|---|
if(60) |
true | Positive Number inside if will execute if block |
if(0) |
true | Zero Number inside if will make condition false and else block gets executed |
if(-60) |
true | Negative number will be considered as true and if block gets executed |
if(20 - 20 + 50) |
true | Expression will be executed and then it will be checked whether it is non-zero or zero |
[box]Non Zero Number is considered as true and Zero will be considered as false[/box]