If statement have an optional else block. Here, we must noticed that, if and else block cannot be executed simultaneously. See the below syntax of if...else statement.
Syntax:
if (Condition) {
// statements to be executed if the condition is true
}
else {
// statements to be executed if the condition is false
}
How if...else statement works?
- If condition returns true then the statements inside the body of “if” are executed and the statements inside body of “else” are skipped.
- If condition returns false then the statements inside the body of “if” are skipped and the statements in “else” are executed.
Flow chart of if...else statement:
Flow chart of if...else statement in C |
Example 1: If...else Statement
Let's understand with a simple example to check whether a number is even or odd using if-else statement.
/* Program to check whether a number is even or odd using if-else */
#include <stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number%2==0){
printf("%d is even number",number);
}
else{
printf("%d is odd number", number);
}
return 0;
}
Output:
Enter a number:14
14 is even number
In above, when the user enter number 14 the condition (number % 2 == 0) is returns true. Hence, the statement inside the body of "if" is executed.
Remember: In C, Zero (0) is used to represent false, and one (1) is used to represent true
Note: If there is only one statement is present in the “if” or “else” body then you do not need to use the braces (parenthesis). Have a look at the below example.
The above program can also be written like this 👇👇:
#include <stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d", &number);
if(number%2==0)
printf("%d is even number", number);
else
printf("%d is odd number", number);
return 0;
}
Ladder if...else...if statement:
It is used in the scenario where there are multiple cases to be performed for different conditions. In if-else-if ladder statement, if a condition is true then the statements defined in the if block will be executed, otherwise if some other condition is true then the statements defined in the else-if block will be executed, at the last if none of the condition is true then the statements defined in the else block will be executed. There are multiple else-if blocks possible. It is similar to the switch case statement where the default is executed instead of else block if none of the cases is matched.
Syntax:
if(condition1){
//code to be executed if condition1 is true
}else if(condition2){
//code to be executed if condition2 is true
}
else if(condition3){
//code to be executed if condition3 is true
}
...
else{
//code to be executed if all the conditions are false
}
Example 2: Ladder if...else...if statement
#include <stdio.h>
int main(){
int number=0;
printf("enter a number:");
scanf("%d", &number);
if(number==10){
printf("number is equals to 10");
}
else if(number==50){
printf("number is equal to 50");
}
else if(number==100){
printf("number is equal to 100");
}
else{
printf("number is not equal to 10, 50 or 100");
}
return 0;
}
Output
Enter a numbr:12
Number is not equal to 10, 50 or 100
Enter a number:50
Number is equal to 50
- Control statement in C
- Simple if
- Nested if
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.