Table of contents
1.
Introduction
2.
Type of Conditional statement
2.1.
1. If statement
2.1.1.
Syntax
2.2.
C
2.3.
Steps for if conditional statement in C
2.4.
2. If-Else statement
2.4.1.
Syntax
2.5.
C
2.6.
Steps to use if-else conditional statement in C
2.7.
3. Nested If-else statement
2.7.1.
Syntax
2.8.
C
2.9.
Steps for Nested if-else conditional statement in C
2.10.
4. If-Else If ladder
2.10.1.
Syntax
2.11.
C
2.12.
5. Switch statement
2.12.1.
Syntax
2.13.
C
3.
Frequently Asked Questions
3.1.
What is the conditional statement?
3.2.
What are the 4 conditional statements?
3.3.
What is a conditional expression statement in C?
4.
Conclusion
Last Updated: Jan 5, 2025
Easy

Conditional Statement

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Conditional statements assist you in determining based on certain situations. These conditions are particular by means of a set of conditional statements having boolean expressions, which might be evaluated to a boolean value true or false. 

Conditional statement

Also Read About, Sum of Digits in C, C Static Function

Type of Conditional statement

1. If the statement

2. If-Else statement

3. Nested If-else statement

4. If-Else If ladder

5. Switch statement

1. If statement

It is one of the effective conditional statements if the statement is responsible for modifying the flow of execution of a program. Suppose the statement is continually used with a condition. This condition is evaluated first before executing any statement inside the body of If.

Syntax

if(expression)
{
//code to be executed
}

 

Example

  • C

C

#include<stdio.h>
#include<conio.h>
void main()
{
   int num=0;
   printf("enter the number");
   scanf("%d",&num);
   if(num%2==0)
   {
       printf("%d number is even",num);
   }
   getch();
}
You can also try this code with Online C Compiler
Run Code

 

Output

enter the number 4
4 number is even


Also see, Floyd's Triangle in C

Steps for if conditional statement in C

These are the steps for using an if conditional statement in C:

  1. Write the if keyword: Start with if.
  2. Add a condition in parentheses (): Put the condition you want to test inside parentheses.
  3. Add curly braces {}: Use curly braces to enclose the code that should run if the condition is true.
  4. Write the code inside the curly braces: Add the statements you want to execute when the condition is true.

2. If-Else statement

In this form of a construct, if the value of test-expression is true, then the true block of statements may be finished. If the value of the test expression is false, then the false block of statements may be executed. In any type of case, the control can be automatically transferred to the statements performing outside the block of If after the execution.

Syntax

if(expression)
{
 //Statements
}
else
{
 //Statements
}

 

Example

  • C

C

#include<stdio.h>
#include<conio.h>
void main()
{
   int num=0;
   printf("enter the number");
   scanf("%d",&num);
   if(num%2==0)
   {
       printf("%d number is even", num);
   }
   else
   {
       printf("%d number is odd",num);
   }
   getch();
}
You can also try this code with Online C Compiler
Run Code

 

Output

enter the number5
5 number is odd

Steps to use if-else conditional statement in C

These are the steps for using an if-else conditional statement in C:

  1. Write the if keyword: Start with if.
  2. Add a condition in parentheses (): Put the condition to test inside the parentheses.
  3. Add curly braces {} for the if block: Include the code that should run if the condition is true.
  4. Write the else keyword: After the if block, use else.
  5. Add curly braces {} for the else block: Include the code that should run if the condition is false.

3. Nested If-else statement

The nested if...else statement is used while a program requires a couple of test expressions. It's also called a multi-way selection statement. When a series of decisions are involved in a statement, we use the if-else statement in nested form.

Syntax

if( expression )
{ 
 if( expression1 )
 {
 statement-block1;
 }
 else 
 {
 statement-block 2;
 }
}
else
{
 statement-block 3;
}

 

Example

  • C

C

#include<stdio.h>
#include<conio.h>
void main( )
{
   int a,b,c;
   a=2,b=4,c=1;
   if(a>b)
   {
       if(a>c)
       {
           printf("a is greatest");
       }
       else
       {
           printf("c is greatest");
       }
   }
   else
   {
       if(b>c)
       {
           printf("b is greatest");
       }
       else
       {
           printf("c is greatest");
       }
   }
   getch();
}
You can also try this code with Online C Compiler
Run Code

 

Output

b is greatest

Steps for Nested if-else conditional statement in C

These are the steps for using a nested if-else conditional statement in C:

  1. Start with the outer if keyword: Begin with an if statement.
  2. Add a condition in parentheses (): Place the condition for the outer if statement inside parentheses.
  3. Use curly braces {} for the outer if block: Include the code to run if the outer condition is true inside curly braces.
  4. Inside the outer if block, start with an inner if keyword: Write another if statement inside the block of the outer if.
  5. Add a condition in parentheses (): Put the condition for the inner if statement inside parentheses.
  6. Use curly braces {} for the inner if block: Include the code to run if the inner condition is true inside curly braces.
  7. Write else for the inner if (optional): After the inner if block, use else to handle cases where the inner condition is false.
  8. Add curly braces {} for the inner else block (optional): Include the code to run if the inner condition is false inside curly braces.
  9. Write else for the outer if (optional): After the outer if block, use else to handle cases where the outer condition is false.
  10. Add curly braces {} for the outer else block (optional): Include the code to run if the outer condition is false inside curly braces.

4. If-Else If ladder

The if-else-if statement is used to execute a code from multiple conditions. It's also known as a multipath decision statement. It is a sequence of if..else statements in which each if statement is associated with an else if statement and remaining might be an else statement.

Syntax

if(condition1)
{
 //statements
} 
else if(condition2)
{
 //statements
}
else if(condition3)
{
 //statements
}
else
{
 //statements
}

 

Example

  • C

C

#include<stdio.h>
#include<conio.h>
void main( )
{
   int a;
   printf("enter a number ");
   scanf("%d",&a);
   if( a%7==0 && a%10==0)
   {
       printf("divisible by both 7 and 10");
   }
   else if( a%10==0 )
   {
       printf("divisible by 10");
   }
   else if(a%7==0)
   {
       printf("divisible by 7");
   }
   else
   {
       printf("divisible by no one");
   }
   getch();
}
You can also try this code with Online C Compiler
Run Code

 

Output

enter a number 21
divisible by 7

5. Switch statement

Switch statement reacts as a substitute for a long if-else-if ladder. This is used to check a list of cases. A switch statement includes one or more case labels that might be tested against the switch expression. While the expression matches a case, then the associated statements with that case could be executed.

Syntax

Switch (expression)
{
 case value1:
 //Statements 
 break;
 case value 2:
 //Statements
 break; 
 case value 3:
 //Statements 
 case value n:
 //Statements
 break;
 Default:
 //Statements
}

 

Example

  • C

C

#include<stdio.h>
#include<conio.h>
void main( )
{
   char grade = 'B';
  
   if (grade == 'A')
   {
       printf("Excellent!");
   }
   else if (grade == 'B')
   {
       printf("Well done");
   }
   else if (grade == 'D')
   {
       printf("You passed");
   }
   else if (grade == 'F')
   {
       printf("Better try again");
   }
   else
   {
       printf("You Failed!");
   }
   getch();
}
You can also try this code with Online C Compiler
Run Code

 

Output

Well done


Also see, Tribonacci Series. and  Short int in C Programming

Frequently Asked Questions

What is the conditional statement?

A conditional statement controls the flow of execution based on whether a condition is true or false, allowing decision-making in code.

What are the 4 conditional statements?

The four conditional statements are if, else if, else, and switch, used to control the execution flow based on conditions.

What is a conditional expression statement in C?

A conditional expression statement in C evaluates a condition and executes one of two expressions based on whether the condition is true or false.

Conclusion

In this blog, we learned about all types of conditional statements, how we can use all conditional statements, and how we learned which statement is useful. 

Live masterclass