![]() |
VOOZH | about |
The if else in C is an extension of the if statement which not only allows the program to execute one block of code if a condition is true, but also a different block if the condition is false.
10 is greater than 5
Explanation: In the above program, the condition checks if the variable n is greater than 5. If true, if block is executed and it prints that n is greater than 5. If it evaluates to false, the else block is executed, printing that n is less than 5. Since n is 10, it prints "10 is greater than 5".
The below flowchart explains the if else works in C:
if statement is true, then the code inside the if block is executed and the rest is skipped. else blockNegative
Explanation: In this program, the if statements check if the given number n is less than 0 which means that the number is negative. As n is negative, it prints the if block. But if the number n was positive, the else block would have been executed.
Note: If long at the block only contains the single statement, we can skip the curly braces.
11