1.
Introduction
2.
Type of Conditional statement
2.1.
1. If statement
2.1.1.
Syntax
2.2.
2. If-Else statement
2.2.1.
Syntax
2.3.
3. Nested If-else statement
2.3.1.
Syntax
2.4.
2.4.1.
Syntax
2.5.
5. Switch statement
2.5.1.
Syntax
3.
FAQs
4.
Key Takeaways
Last Updated: Mar 27, 2024

# Conditional Statement

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

## 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.

## Type of Conditional statement

1. If the statement

2. If-Else statement

3. Nested If-else statement

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

``````#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();
}``````

Output

``````enter the number 4
4 number is even``````

Also see, Floyd's Triangle in C

### 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

``````#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();
}``````

Output

``````enter the number5
5 number is odd``````

### 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

``````#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();
} ``````

Output

``b is greatest``

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

``````#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();
}``````

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

``````#include<stdio.h>
#include<conio.h>
void main( )
{

{
printf("Excellent!");
}
{
printf("Well done");
}
{
printf("You passed");
}
{
printf("Better try again");
}
else
{
printf("You Failed!");
}
getch();
}``````

Output

``Well done``

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

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

## FAQs

1. What are conditional statements in c?
Ans: The conditional statements in c programming are the statements that might test any particular condition according to the user's requirement. Those conditional statements in c assist us in making your mind up within the program in any condition.
2. Can we use if-else in the switch case?
Ans: An if-then-else statement can take a look at expressions based totally on ranges of values or conditions, while a switch statement tests expressions based most effective on a single integer, enumerated value, or String object. Technically, the final break isn't required because flow falls out of the switch statement.
3. Is the switch case a conditional statement?
Ans: The switch is a type of conditional statement to evaluate an expression in opposition to multiple possibilities and execute one or more blocks of code based totally on matching cases.

## Key Takeaways

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. Yet learning never stops because there is a lot more to learn. So head over to our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, and much more.

Live masterclass