Conditional control statements
in a c language
Presented By 1. Shubha kumari
2. B.Sony
3. A. Prasanna
Content
Introduction
Types of conditional statements
Simple if
If-else
Nested if
If else ladder
conclusion
Introduction
A statement which is used to execute a block
of statements.
If conditions become true. Other wise, next
statement is executed.
Types of conditional statements
There are four types of conditional
statements
Simple if statements
If-else statements
Nested if statements
If-else-if statement (if-else ladder)
Simple if statements:
In simple if statements, if the given condition is
TRUE during the execution then it executes next
statements or block of statements. If its FALSE,
it skips the execution of the next statement.
Syntax: if (condition)
{
…….
block of statements;
………
}
Flow chart of simple if staement:
Simple if program
#include<stdio.h> Output:
void main ()
{
Enter the value of a: 5
int a=4; a is odd number
printf("Enter the value of a: //if its true
");
scanf("%d",&a);
Enter the value of a: 6
if(a%2==1) //if its false
{
printf("a is odd number");
}
Return 0;
}
If-else statement:
If-else used to verify the given condition and
executes only one out of the two block of
statements based on he condition result.
The if-else statement evaluates the specified
condition.
If it is TRUE, it executes a block of
statements(TRUE block).
If the condition is FALSE, it executes another
block of statements(FALSE block).
Syntax and flowchart
If (condition)
{
……
true block of statements;
……
}
else
{
……
False block of statements;
……
}
If else statement program
#include<stdio.h>
Output:
void main ()
{ Run 1: Enter the value
int a=4; of a: 26 a is even
printf("Enter the value of a:
");
number
scanf("%d",&a); //if its true
if(a%2==1)
{
Run 2: Enter the value
printf("a is odd number of a: 53 a is odd
"); number
}
else{
printf("a is even number");
}}
Nested if statements:
Writing a if Syntax: if (condition)
statement inside {
If (condition)
another if statement
…….
is called nested if True block statements 1;
statement. }
……..
}
Else
{
False block statement 1;
}
Flow chart nested if:
Nested if statement program:
#include<stdio.h> Printf(“\n the number is less than 5 \
Void main() n”);
{ }
int n; }
Clrscr(); Else
Printf(“enter the value for n:”); {
Scanf(“%d”,&n); printf(“\n The number is entered is
If(n>5) less than zero \n”);
{ }
if(n>5) Printf(“\n End of the program”);
{ Getch();
Printf(“\n The number is greater }
than 5 \n”); Output:
} enter the value of n: 9
Else The number is greater than 5
{ End of the program
If-else-if statement(if-else ladder):
Writing a if Syntax:
if(condition1)
statement inside
{
else of an if
……
statement is called true block of statements 1;
if-else-if statement. …….
}
Else if(condition2)
{
false block of condition1;
&
true block of condition2;
}
Flowchart of if else ladder
If else ladder program
#include<stdio.h> Printf(“n is less than zero”);
Void main()
{
}
int n; getch();
Clrscr(); }
Printf(“enter the value of n:”);
Output:
Scanf(“%d”, &n);
If(n>0) enter the value of n:2
{ Given number is positive
printf(“given number is positive number
number”)
} //if its false
”) Else if(n<0) Enter the value of n:-3
{ Given number is negative
Printf(“given number is negative
number);
number
What we have learnt
Conditonal control staements
Simple if
If-else
Nested if
Else if ladder
The end
Thank you for giving this
opportunity