Conditional Statements: if, if-else, Nested if-else C

Conditional Statements

Conditional statements are used to execute the statements or transfer them from one part of the program to another depending on a condition. Some conditional statements if, if-else, Nested if-else, if…else…if ladder.

if-statement

When we use if statement to express conditional expressions, it evaluates the given condition. If the given condition is true then it will execute the statement or given set of statements otherwise (if the given condition is false) the control is transferred to the next statement.

Syntax of if-statement:

#include <stdio.h>
#include <stdlib.h>
int main()
{
   if (condition)
{
     //Block of C statements here
     //These statements will only execute if the condition is true
    printf("    ");
} 
return 0;
}

Flowchart of if-statement:

Flowchart-of-if-statement

Example of if-statement:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int n;
    printf("Enter a Number:");
    scanf("%d",&n);
    if(n>=10){
        printf("The number is greater than 10");
    }
    return 0;
}
 
Enter a Number:11
The number is greater 10

if-else Statement

The if-else statement is used to select any one of the two possible alternatives.

In other words, the if-else statement is used to execute a statement block or a single statement depending on the value of a condition. The if-else statement contains two statement blocks.

The first statement block will be executed, if the condition is true otherwise the second block followed by else will be executed.

Syntax of if-else statement:

{
  if (condition)
{
   // 1st statements block of if body
}
else
{
   // 2nd statements block of else body
}

Flowchart of if-else statement:

Flowchart-of-if-else-statement

Example of if-else statement:

#include <stdio.h>
#include <stdlib.h>
int main()
{
   int age;
   printf("Enter your age:");
   scanf("%d",&age);
   if(age>=18)
   {
      /* This statement will only execute if
             the above condition (age>=18) returns true */
 printf("you are eligible to vote");
   }
   else
   {
 /* This statement will only execute if the condition
             specified in the "if" returns false.*/
 printf("you are not eligible to vote");
   }
   return 0;
}
 

Output:

Enter your age:18
you are eligible for voting

Here, If-statement can execute because the age is over 18 and the output is showing that you are eligible to vote. If the age is under 18 then, else-statement executes and shows that you are not eligible to vote.

Nested if

Nested if is an if within another if. It is used to select more than two choices.

Syntax of nested if:

if (expression is true)
{
if (expression is true)
{
statements;
}

Example of nested if:

#include <stdio.h>
#include <stdlib.h>
int main()
{
   int i;
   printf("Enter an even number less than 10:\n");
   scanf("%d",&i);
   if(i<10)
   {
       if(i%2==0)
       printf("You have entered the correct number");
   }
 
 
 
   return 0;
}

Output:

Enter an even number less than 10:
2
You have entered the correct number

Nested if-else statement

Writing an entire if-else within either the body of if statement or in the body of else statement is called nested if-else.

Syntax of nested if-else:

if (condition 1)
{
statement 1;
...
}
else
    {
        if (condition 2)
        {
          statement 2;
          ...
}
else
    {
        statement 3;
        ...
 
    }
}

Example of nested if-else statement:

#include <stdio.h>
#include <stdlib.h>
int main()
{
   int i;
   printf("Enter either 1 or 2: ");
   scanf("%d", &i);
   if(i==1)
    printf("You have entered one");
   else
    {
        if(i==2)
        printf("You have entered two");
        else
        printf("Invalid Entry!");
    }
 
 
 
   return 0;
}
 

If we enter 1, we will get following output:

Enter either 1 or 2: 1
You have entered one

if…else…if ladder statement:

Common programming constructs in c is the if…else…if ladder which is also called the “if…else…if staircase” because of its appearance.

In other words, a multipath decision is a change of its in which the statement is associated with each else is an if.

Syntax of if…else…if ladder:

if (conditio 1)
{
    statement 1;
}
else if(condition 2)
    {
    statement 2;
}
else if(condition 3)
    {
    statement 3;
}
else if(condition n)
    {
    statement n;
}
else
{
    default statement;
}
outside statement;

Flowchart of if…else…if ladder:

Flowchart of if...else...if ladder
//Program to find greater number between 3 digit number and input from the user
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a,b,c;
    printf("Enter three values:");
    scanf("%d%d%d", &a,&b,&c);
    if(a>b && a>c)
    {
        printf("a is greater");
    }
    else if(b>c)
    {
     printf("b is greater");
    }
    else
    {
        printf("c is greater");
    }
    return 0;
}
 
 
 

Output:

Enter three values:4 7 9
c is greater

Previous Lecture:

Data Types
ASCII Code
Switch Case Statement

If you have any problem in solving your questions, then join our Telegram group or search @Learnsimply, we will help you.

Please write comments if you find anything incorrect in the Conditional statements if-else lecture.

Leave a Comment

en English
X