Conditional Statements
Presented by:
[Link], MUBEEN SALEEM, [Link],WAJID
KHALIQ
Contents
Conditional Statements
If statement
If else statement
If else if statement
Nested if statement
Switch statement
What is conditional statement?
Conditional statement allow you to make a decision based
on the result of condition.
Types:-
➢ If statement
➢ If else statement
➢ If else if statement
➢ Nested if statement
➢ Switch statement
What is if statement ? Explain.
If statement is used when we test a condition, if condition is true then
if block statement will be executed, otherwise skipped remaining code
Syntax of if statement
if (condition)
{
statement;
// block of code to be executed if the condition is true
}
[Link]
Flowchart of if statement
[Link]
Example of If statement
Write a program that inputs two numbers and finds if second number is square of first.
#include<iostream.h>
#include<conio.h>
int main()
{
Output:
clrscr(); Enter a number: 5
int a,b; Enter a number: 25
cout<<“Enter a number”;
2nd number is square of 1st
cin>>a;
cout<<“Enter a number”;
cin>>b;
if (a*a==b)
{
cout<<“2nd number is square of 1st number”;
}
getch();
}
[Link]
What is if else statement ? Explain.
It is used to perform two operations for a single condition, if the
given condition is true then if block executed otherwise else block
executed.
Syntax of if else statement
if (condition)
{
// block of code to be executed if the condition is true
}
else {
// block of code to be executed if the condition is false
}
Mubeen saleem
Flowchart of if else statement
Mubeen saleem
Example of If else statement
Write a program that inputs a number and finds whether is even or odd using if else.
#include<iostream.h>
#include<conio.h>
int main()
{ Output:
clrscr();
int n; Enter a number: 10
cout<<“Enter a number”; 10 is even.
cin>>n;
if (n%2==0)
{
cout<<n<< “is even”;
}
else{
cout<<n<<“is odd;
}
getch();
}
Mubeen saleem
What is if else if statement ? Explain.
It is used to perform multiple cases for different [Link] this statement
there is one if condition and multiple else if conditions and one else block.
Syntax of if else if
[Link]
flowchart of if else if statement
[Link]
Example of If else if statement
Write a program that inputs test score of a student and display his grade
according to his following criteria:
Test score Grade
>=90 A
80-89 B
70-79 C
60-69 D
Below 60 F
[Link]
Example of If else if statement
Output:
Enter your test score: 74
Your grade is C.
[Link]
What is Nested if statement ? Explanation.
When we define if block inside another if block is called nested if
statement.
Syntax of if else if
Wajid khaliq
Example of Nested if statement
Write a program that inputs three numbers and display whether all
number are equal or not using nested if condition:
Output:
Enter three numbers: 74 ,25 30
Numbers are different
Wajid khaliq
What is Switch statement ? Explain.
The Switch statement is another conditional [Link] can be used easily
when there are many choices available and only one should be executed.
Syntax
Wajid khaliq
Example of Switch statement
Write a program that input a character from the user and check whether it
is a vowel or constant.
Wajid khaliq