Types of Conditional Statements (if, if-else, if-else-if, Nested)

There are different types of conditional statements available in the C programming language that make our program capable of making decisions on its own. We’ll go over each type of conditional statement with examples, but first, let’s talk about what conditional statements are.


What are Conditional Statements?

Conditional statements are fundamental blocks of programming that allow programs to make decisions based on certain conditions. For example, there is a specific statement dedicated to check if a certain condition is met, then some action will be performed, otherwise some other action will be performed. Based on our requirements, we can use a conditional statement in our programs.

There are several types of conditional statements available in C for us to use in different situations, but in this tutorial, we will understand the two fundamental types – if and if-else. Let’s get started with the if statement.


1.  if Statement

The if statement is the most basic form of conditional statement. It executes a block of code only when a certain condition is true. 

The syntax of the if statement is as follows:

if (condition) {
    // Code to be executed if the above condition is True.
}

To understand the flow of if statement in C, observe the following flowchart:

Flowchart of the if statement

Let’s look at a simple example to understand the use of the if statement:

#include <stdio.h>

int main() {
   int x = 10;
 
   if (x >= 10) {
       printf("x is greater than or equal to 10.");
   }
 
   return 0;
}

Output:

x is greater than or equal to 10.

In the above code, the execution of the printf() function depends on the condition (x >= 10). Only when this condition is true will the printf() function be executed. Now, if you change the value of x to 9 or any value less than 10, observe the output. Do you see any text displayed? The output window is blank, indicating that the printf() function is not executed this time.


2.  if-else Statement

The if-else statement extends the functionality of the if statement by providing an alternative block of code which will be executed when the condition of the if statement becomes false. 

The syntax is as follows:

if (condition) {     // Code to be executed if the above condition is True. } else {     // Code to be executed if the condition is False. }

Flowchart:

Flowchart of the if-else statement

Example:

#include <stdio.h>

int main() {
   int x = 5;
 
   if (x >= 10) {
       printf("x is greater than or equal to 10.");
   } else {
       printf("x is less than 10.");
   }
 
   return 0;
}

Output:

x is less than 10.

Examine the else block in the above code. Last time, when we executed the code without the else block, no output was displayed if the value of x was less than 10. This absence of output caused confusion among beginner programmers who were unsure about what went wrong. There is nothing wrong in the code, it’s just that we were not printing an appropriate message when the if condition is false. The else block provides us the way to print the appropriate message, in this case “x is less than 10”, when the condition is false. Therefore, it is at the discretion of the programmer whether to utilize the else block to execute specific actions when the ‘if’ condition is not true.

So, this is all about if and if-else statements. Next, we will understand the Nested-if statement.


3. if-else-if Statement

The if-else-if statement is used when multiple conditions need to be checked sequentially. This is particularly useful when we want to check more than two conditions and evaluate some code based on those conditions.

Syntax:

if (condition1) {
    // Code to be executed if condition1 is True.
} else if (condition2) {
    // Code to be executed if condition2 is True.
} else {
    // Code to be executed if none of the above conditions are True.
}

Flowchart:

Figure:  Flowchart of the if-else-if statement

Example:

#include <stdio.h>

int main() {
   int marks = 75;
 
   if (marks >= 90) {
       printf("Grade: A\n");
   } else if (marks >= 80) {
       printf("Grade: B\n");
   } else if (marks >= 70) {
       printf("Grade: C\n");
   } else if (marks >= 60) {
       printf("Grade: D\n");
   } else {
       printf("Grade: F\n");
   }
 
   return 0;
}

Output:

Grade: C

The code above is pretty straightforward. The student’s mark is 75, so their grade is C. The only thing to remember is that the conditions in an if-else-if ladder are always checked sequentially. The third condition is only checked if the first two conditions are false, and since the third condition is true, the conditions after it are not evaluated. That’s why the output is “Grade: C.”


4. Nested if-else Statement

The nested if-else statement is simply an if-else statement inside another if-else statement.

Syntax:

if (condition1) {
    // Code to be executed if condition1 is true.
    if (condition2) {
        // Code to be executed if condition1 and condition2 is true.
    } else {
        // Code to be executed if condition1 is true and condition2 is false.
    }
} else {
    // Code to be executed if condition1 is false.
}

Flowchart:

Figure:  Flowchart of the Nested if-else statement

Example:
Consider the following program where we want to determine whether the student has passed in subject 1 and subject 2. If he got at least 40 marks in both subjects, then he passed both subjects.

#include <stdio.h>

int main() {
   int subject1 = 40;
   int subject2 = 90;
 
   if (subject1 >= 40) {
       if (subject2 >= 40) {
           printf("Congratulations! You passed both subjects.\n");
       } else {
           printf("Sorry! You failed subject 2.\n");
       }
   } else {
     printf("Sorry! You failed subject 1.\n");
   }  
 
   return 0;
}

Output:

Congratulations! You passed both subjects.

Alright, that’s all we need to know about if-else-if and nested if statements for now. In the next tutorial, we’ll get into the switch statement in detail. Stay tuned!



Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.