If else statement is the extension of the simple if statement. It also handles the case of the false condition. Meaning that if the test expression is false then another block called the else block will be executed instead of the if block.
The general syntax is as shown below.
// syntax for simple if
if(test_expression){
// if-block/statements to be executed
}
else{
// else block/statments
}
// other statements
The flowchart of the if-else block is shown below.
We can extend the previous simple-if example to display the proper message if the number is not divisible by 5.
#include <stdio.h>
int main(){
int number;
printf("Enter an integer number:");
scanf("%d", &number);
// simple if condition
if(number % 5 == 0){
printf("It is divisible by 5.\n");
}
else{
printf("It is not divisible by 5.");
}
return 0;
}
If else construct either executes if block or else block.