If Else Statement in C
On this page, you will learn about the if-else statement in C programming, the If statement, the If-else statement, the Else-if statement in C, and the nested if-else statement. In addition to this, you will get syntax, a flowchart, and examples of all types of if-else statements.
What is the if-else statement in C programming?
The if-else statement is one of the fundamental control flow statements in the C programming language. It allows a program to make decisions based on a particular condition. The if-else statement executes a block of code if a particular condition is true, and another block of code if the condition is false.
There are four types of if-else statements in C programming:
- If statement
- If-else statement
- Else-if statement
- Nested if-else statement
1. If statement:
The if statement is the most basic type of conditional statement in C. It is used to check a single condition and execute the code within the statement if the condition is true. If the condition is false, the code within the statement is skipped.
Syntax of if statement in c:
if (condition) {
// code to execute if condition is true
}
Example of if statement in c:
#include<stdio.h>
int main(){
int x = 10;
if (x > 5){
printf("x is greater than 5");
}
return 0;
}
Output:
x is greater than 5
2. If-else statement:
The if-else statement provides a way to execute different code based on the outcome of a single condition. If the condition is true, the code within the if statement is executed, and if it is false, the code within the else statement is executed.
Syntax of if-else statement in c:
if (condition){
//code to execute if condition is true
}
else{
//code to execute if condition is false
}
Flowchart of if-else statement:
Example 1 of if-else statement:
#include<stdio.h>
int main(){
int a=5, b=3;
if(a>b){//becomes ture
printf("a is grater then b");
}
else{
printf("b is grater then a");
}
return 0;
}
Output:
a is grater then b
Example 2 of if-else statement:
#include<stdio.h>
int main(){
int a=2, b=3;
if(a>b){//becomes false
printf("a is grater then b");
}
else{
printf("b is grater then a");
}
return 0;
}
Output:
b is grater then a
3. Else-if statement in c:
The else-if statement, also known as the else-if ladder, is an extension of the basic if-else statement in C programming. It allows the programmer to check multiple conditions and execute different statements based on the results of those conditions.
The syntax for the else-if statement is:
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 none of the conditions are true
}
Here, the program first checks condition1. If it is true, the code inside the first block is executed. If it is false, it moves on to the next condition, condition2. If condition2 is true, the code inside the second block is executed. This process continues until the last condition is checked. If none of the conditions are true, the code inside the final else block is executed.
The else-if statement allows the programmer to check multiple conditions in a more concise and efficient way than using nested if statements. It is also more readable and easier to understand.
Overall, the else-if statement is a useful tool for writing more complex and conditional programs in C.
Flowchart of else-if statement or if-else ladder in c:
Example of else-if statement or if-else ladder in c:
#include<stdio.h>
int main(){
int score = 85;
char grade;
if (score >= 90 && score <= 100) {
grade = 'A';
} else if (score >= 80 && score <= 89) {
grade = 'B';
} else if (score >= 70 && score <= 79) {
grade = 'C';
} else if (score >= 60 && score <= 69) {
grade = 'D';
} else {
grade = 'F';
}
printf("Your grade is %c\n", grade);
return 0;
}
Output:
Your grade is A
1. Nested if-else statement:
Nested if-else statements in C programming refer to the usage of if-else statements inside another if-else statement. This means that you can have multiple if-else statements inside each other to make complex decisions in your program.
Syntax of nested if-else statement in C:
if(condition1){
//code to be executed if condition1 is true
if(condition2){
//code to be executed if both condition1 and condition2 are true
}
else{
//code to be executed if condition1 is true and condition2 is false
}
}
else{
//code to be executed if condition1 is false
}
In the above example, if condition1 is true, the code inside the first if block will be executed. If condition2 is also true, the code inside the nested if block will be executed. If condition2 is false, the code inside the else block of the nested if-else statement will be executed. If condition1 is false, the code inside the else block of the outer if-else statement will be executed.
It is important to note that each type of if-else statement can be nested within one another to create more complex conditional statements.
Flowchart of nested if-else statement in C:
Example of nested if-else statement in c:
#include<stdio.h>
int main() {
int num1 = 10;
int num2 = 20;
if (num1 == num2) {
printf("The numbers are equal.");
}
else {
if (num1 > num2) {
printf("Num1 is greater than num2.");
}
else {
printf("Num2 is greater than num1.");
}
}
return 0;
}
Output:
Num2 is greater than num1.