In this tutorial you will learn about the C If Else Statement and its application with practical example.
C if-else Statement
The c if-else statement is an extended version of the If statement. When we want to execute a block of code when the if the condition is true and another block of code when the if the condition is false, In such a case we use the if-else statement. The statements inside the “if” body only execute if the given condition returns true. If the condition returns false then the statements inside the “else” body are executed.
But what if we want to do something else if the condition is false. Here comes the C else statement. We can use the else statement with the if statement to execute a block of code when the condition is false.
C If else Statement flowchart
if-else Statement Syntax
The general syntax of if-else statement is as follows:
Syntax:-
1 2 3 4 5 |
if(condition){ // statements } else { // statements } |
Working of if-else Statement
Here, the if statement evaluates the test condition inside the parenthesis ()
. The Condition is a Boolean expression that results in either True or False. if it results in True then statements inside if the body is executed, if it results in False then statements inside else body is executed.
C if-else Example
Below is a simple example to demonstrate the use of if-else in c programming:
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdio.h> void main() { int a = 10; int b = 20; printf("W3Adda - C If else Statement\n"); if(a > b){ printf("a is greater than b\n"); } else{ printf("b is greater than a\n"); } } |
Output:-