If else Statements in C

If else statements in C Program

In this article, I am going to discuss If else Statements in C Program with Examples. Please read our previous articles, where we discussed the basics of Control Statements in C. Before understanding if-else statements, let us first understand the Selection Statements in C Langauge.

What are Selection Statements in C?

Selection statements allow you to control the flow of program execution on the basis of the outcome of an expression or state of a variable known during run time. It executes different sections of code depending on a specific condition or the value of the variable. Selection statements can be divided into the following categories:

  • if-else statements (Will discuss in this article)
  • switch statements (Will discuss in the next article)
if-else statements in C:

The ‘if’ control statement allows you to check the validity of a certain condition & perform required operations depending on the genuineness of the condition. If the condition followed by the ‘if’ keyword holds True, the code is written inside the braces of the ‘if’ statement will be executed, otherwise the program control will skip the loop execution and continue with the remaining program. ‘if’ statement is generally accompanied by the ‘else‘ keyword which lets the compiler know about actions to be performed if the condition following the ‘if’ statement is False.

if-else statement control flow in C

Syntax of the if-else statement in C:

Syntax of if else statement in C

Where if and else are reserved words. The expression is Relational or Boolean expression that evaluates to a TRUE(1) or FALSE(0) value. Statement_1 and statement_2 may either be a single C statement or a block of C statements.

Program to understand the if-else statement in C:
#include<stdio.h>
int main ()
{
   int x = 2;
   if (x >= 5)
   {
      printf ("Condition holds True!");
   }
   else
   {
      printf ("Condition is False!");
   }
   return 0;
}

Output: Condition is False!

If you execute the same program with a different value assigned to the variable ‘x’, the output may differ. In the below example, if assign x = 10, the if statement will be executed.

#include<stdio.h>
int main ()
{
   int x = 10;
   if (x >= 5)
   {
      printf ("Condition holds True!");
   }
   else
   {
      printf ("Condition is False!");
   }
   return 0;
}

Output: Condition holds True!

The purpose of ‘if’ statement in a program is to allow multiple execution paths for varying user inputs, making it more interactive!

Nested if-else statements in C:

When an if-else statement is present inside the body of another “if” or “else” then this is called nested if-else. Nested ‘if’ statements are used when we want to check for a condition only when a previous dependent condition is true or false. C allows us to nested if statements within if statements, i.e, we can place an if statement inside another if statement.

Nested if-else statements in C

Syntax of nested if-else statements in C:

Syntax of nested if else statements in C

Program to understand nested if-else statements in C:
#include <stdio.h> 
int main() 
{ 
    int i = 10; 
    if (i == 10) 
    { 
    	if (i < 15) // First if statement 
     printf("i is smaller than 15\n"); // Nested - if statement 
      // Will only be executed if statement above is true.
        if (i < 12) 
           printf("i is smaller than 12 too\n"); 
        else
           printf("i is greater than 15"); 
    } 
   return 0; 
}

Output:

Program to understand nested if else statements in C

Ladder if-else statements in C:

In Ladder if-else statements one of the statements will be executed depending upon the truth or falsity of the conditions. if condition1 is true then Statement 1 will be executed and so on but if all conditions are false then Statement 3 will be executed. The C if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions are true, then the final else statement will be executed.

Ladder if-else statements in C

Syntax to use Ladder if-else statements in C:

Syntax to use Ladder if-else statements in C

Program to understand Ladder if-else statements in C:
#include <stdio.h> 
int main() 
{ 
    int i = 20; 
    if (i == 10) 
    {
        printf("i is 10");
    } 
    else if (i == 15) 
    {
        printf("i is 15"); 
    }
    else if (i == 20)
    { 
        printf("i is 20");
    } 
    else
    {
        printf("i is not present");
    } 
}

Output: i is 20

In the next article, I am going to discuss Switch Statements in C with examples. Here, in this article, I try to explain if-else Statements in C with examples. I hope you enjoy this article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

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