0% found this document useful (0 votes)
483 views11 pages

Conditional Statements in C Programming

Uploaded by

Kalyanam A
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
483 views11 pages

Conditional Statements in C Programming

Uploaded by

Kalyanam A
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 11

Conditional Statements in C Programming

The conditional statements in c are the statements that are used to


check any particular condition according to the user’s requirement.
These conditional statements in c help us to make a decision in the
program in any condition.
Types of Conditional Statements in C
In C programming language, there are the following types
of conditional statements in c.
1. if Statement
2. if-else Statement
3. nested if-else Statement
4. switch Statement
5. if-else if ladder
Let’s see these conditional statements in C one by one.

1.If Statement in C
Definition
If Statement is a basic conditional statement in C Programming. If
Statement is used as a single statement in C program, then code inside
the if block will execute if a condition is true. It is also called a one-way
selection statement.
Syntax of If Statement
The general syntax of if Statement in c is given below
if(expression)
{
//code to be executed
}
If the condition is evaluated as true, then the block statement is
executed, but if the condition is evaluated as false, then control is passed
to the next Statement following it.
C Program for if Statement
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf(“enter the number”);
scanf(“%d”,&n);
if(n%2==0)
{
printf(“%d number in even”,n);
}
getch();
}

Program Output
We have executed this program on Turbo C Compiler and got the output
as shown in the following picture.
2.if-else Statement in C

Definition
if else statement in c allows two-way selection. If the given condition is
true. Then program control goes inside the if block and execute the
statement.
The condition is false then program control goes inside the else block
and execute the corresponding statement.

Syntax of if-else Statement


The general syntax of if else statement in c is given as following.
if(expression or condition)
{
// statement ;
}
else
{
//statement ;
}
Program for if-else Statement
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
printf(“enter the number”);
scanf(“%d”,&n);
if(n%2==0)
{
printf(“%d number in even”,n);
}
else
{
Printf(“%d number is odd”,n);
}
getch();
}

Program Output
We have executed this program on Turbo C Compiler and got the output
as shown in following picture.

3. Nested if-else Statement


Definition
A nested if-else statement is used to check more than one condition.
Nested if-else is also known as a multi-way selection statement. If there
is a series of decisions is involved in a statement, we use if else in nested
form.
Syntax of Nested if-else Statement
The general syntax of else if-else-if is as follows.
if(expression)
{
Statement
}
else if
{
// Statement
}
else if
{
//Statement
}
else
{
// Statement
}
The execution of the above nested if-else Statement is as follows.

First, it will check the given condition associated with the if statement; if
the result of the given condition is True, then the Statement inside the if
block is executed.

If the given condition is false, then it will check the first else if part and
the condition of is true then the statements related to else if is Executed
otherwise the pointer goes to next else if and this process is contained
And if the result of the all the else if a condition is false then the pointer
is automatically going to else part and the Statement of the else
automatically executed.

Nested if-else Program


Following the program to find the greatest number among the three
number.

#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,c;
clrscr();
printf(“Please Enter three number”);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf(“a is greatest number”);
}
else
{
printf(“c is greatest number”);
}
}
else
{
if(b>c)
{
printf(“b is greatest number”);
}
else
{
printf(“c is greatest”);
}
}
getch();
}

Program Output
We have executed this program on Turbo C Compiler and got the output
as shown in following picture.

4.if-else-if Ladder Statement in C


Definition
The if-else-if conditional Statement in c is used to execute one code
from multiple conditions. It is also known as a multi-path decision
statement. It is a sequence of if-else statements where every if a
statement is associated with else if Statement and last would be an else
statement.
Syntax of If-else-if Ladder Statement
The general syntax of if-else- if a ladder is shown below
if(expression1)
{
//statements
}
else if(expression2)
{
//statements
}
else if(expression3)
{
//statements
}
else
{
//statements
}
if else if ladder Program
#include<stdio.h>
#include<conio.h>
void main( )
{
int a;
printf(“enter a number”);
scanf(“%d”,&a);
if( a%5==0 && a%8==0)
{
printf(“This number divisible by both 5 and 8”);
}
else if( a%8==0 )
{
printf(“This number is divisible by 8”);
}
else if(a%5==0)
{
printf(“this number is divisible by 5”);
}
else
{
printf(“This number is divisible by neither 8 nor 5”);
}
getch();
}

Program Output
We have executed this program on Turbo C Compiler and got the output
as shown in following picture.

5 . Switch Case Statement


A switch case statement in C Programming tests the value of a choice
variable and compares it with multiple cases. When the case match is
found, a block of statements associated with that specific case is
executed.
Each case in a block of a switch has a different choice which is referred
to as an identifier. When the user enters a value, then the value provided
by the user is compared with all the cases inside the switch block until
the exact match is found.
If a case match is not found, then the default statement is executed, and
the control goes out of the switch block.
Switch Case Statement Program
Program to demonstrate the use of switch statement is as follow
#include<stdio.h>
#include<conio.h>
void main()
{
int choice;
printf( “Enter any value ” );
scanf( “%d”, & choice );
switch ( choice ) {
case 1:
printf( “It is hot weather” );
break;
case 2:
printf( “It is a stormy weather” );
break;
case 3:
printf( “It is a sticky weather” );
break;
default:
printf( “It is a pleasant weather!n” );
break;
}
getch();
}

Program Output
We have executed this program on Turbo C Compiler and got the output
as shown in following picture.

You might also like