Control Constructs
SESSION 5
Control Constructs
It control the execution flow of statements in a program.
Three Types, they are
1. Sequence Control Constructs:
Execution flow is one after another.
2. Selection Control Constructs :
Execution flow is based on the condition.
3. Iteration Control Constructs :
Execution flow is repeated statements or
[Link] 2
Selection Control flow statements
I. if, if else, nested if, if else if
II. Switch case
III. Conditional operators
[Link] 3
If statement
Syntax:
if(condition)
{
statements;
}
Executes a block of
code if a specified
condition is true.
[Link] 4
Eg:Largest of 2 numbers
.
#include<iostream.h>
#include<conio.h>
void main()
{
int n1,n2,l;
clrscr();
cout<<"Enter 2 no's";
cin>>n1>>n2;
l=n1;
if(n1<n2)
{
l=n2;
}
cout<<"Largest no :"<< l;
getch();
[Link] 5
}
If else statement
Executes one block of
code if the condition is
true, another if it's false.
Syntax:
if(condition)
{
statements;
}
else
{
statements;
} [Link] 6
Eg: Largest of two numbers
.
//Largest of 2 no's
#include<iostream.h>
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
cout<<"Enter 2 no's";
cin>>n1>>n2;
if(n1<n2)
{
cout<<"Largest no n2:"<<n2;
}
else
{
cout<<"Largest no n1:"<<n1;
}
getch();
[Link] 7
}
Nested if statement
An if statement inside another if statement.
Syntax:
if(condition)
{
if(condition)
{
statements;
}
statements;
}
[Link] 8
.
[Link] 9
Eg: Largest of three numbers
#include<iostream.h>
.
#include <conio.h>
void main()
{
int n1,n2,n3;
clrscr();
cout<<"Enter 3 no's";
cin>>n1>>n2>>n3;
if(n1>n2)
{
if(n1>n3)
cout<<"Largest no :"<<n1;
}
if(n2>n1)
{
if(n2>n3)
cout<<"Largest no :"<<n2;
}
if(n3>n1)
{
if(n3>n2)
cout<<"Largest no :"<<n3;
}
getch();
} [Link] 10
If else if statement
Tests multiple conditions sequentially.
Syntax:
if(condition)
{
statements;
}
else if(Condition)
{
statements;
}
else
{
statements;
} [Link] 11
.
[Link] 12
Eg:Largest of three numbers
.
#include<iostream.h>
#include<conio.h>
void main()
{
int n1,n2,n3;
clrscr();
cout<<"Enter 3 no's";
cin>>n1>>n2>>n3;
if(n1>n2&&n1>n3)
{
cout<<"Largest no :"<<n1;
}
else if(n2>n1&&n2>n3)
{
cout<<"Largest no :"<<n2;
}
else
{
cout<<"Largest no :"<<n3;
}
getch();
[Link] 13
}
Questions:
1. Write a program to check the given no is even or
odd
2. Write a program to check the given no is +ve or
–ve or zero
3. Write a program to check the given alphabetic
character is vowel or not
4. Write a program to check the given year is leap
year or not
[Link] 14
Conditional operators (? :)
It replaces if else statements .
It is also called ternary operators, because three
expressions are used to perform operations.
Syntax:
exp1?exp2:exp3;
Here exp1 is a condition ,this is true then return
exp2, otherwise return exp3.
[Link] 15
Eg: Largest of two numbers
.
//Largest of 2 no's
#include<iostream.h>
#include<conio.h>
void main()
{
int n1,n2;
clrscr();
cout<<"Enter two no's";
cin>>n1>>n2;
(n1>n2)?cout<<"Largest no
n1"<<n1:cout<<"Largest no n2:"<<n2;
getch();
} [Link] 16
Eg: Largest of three numbers
.
#include<iostream.h>
#include<conio.h>
void main()
{
int n1,n2,n3;
clrscr();
cout<<"Enter three no's";
cin>>n1>>n2>>n3;
(n1>n2&&n1>n3)?cout<<"Largest no n1"<<n1:
(n2>n1&&n2>n3)?cout<<"Largest no n2:”<<n2 :
cout << “Largest no n3 :” << n3;
getch();
} [Link] 17
Questions:
1. Write a program to check the given no is
even or odd
2. Write a program to check the given no is
+ve or –ve or zero
3. Write a program to check the given
alphabetic character is vowel or not
[Link] 18
Switch case
Selects one of many code blocks to execute.
Syntax:
switch(expression)
{
case constant1:
stmts;
break;
case constant2:
stmts;
break;
“ “
default:
stmt;
break;
[Link] 19
}
Eg: //Given Alphabetic character is vowel or not.
.
#include<iostream.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
cout<<"Enter an Alphabetic character\n";
cin>>ch;
switch(ch)
{
case 'a':
case 'A':
cout<<"A is vowel" ;
break;
case 'e':
case 'E':
cout<<"E is vowel" ;
break;
case 'i':
case 'I':
cout<<"I is vowel" ;
break;
case 'o':
case 'O':
cout<<"o is vowel" ;
break;
case 'u':
case 'U':
cout<<"U is vowel" ;
break;
default:
cout<<"Not a Vowel";
break;
} getch(); [Link] 20
}
case 2:
Eg: //Calculations
res=n1-n2;
.
#include<iostream.h>
#include<conio.h>
void main()
cout<<"Result of
"<<n1<<“-”<<n2<<"=“<<res;
break;
{ case 3:
int n1,n2,res,choice; res=n1*n2;
clrscr(); cout<<"Result of
cout<<"Enter two no's:"; "<<n1<<“*”<<n2<<"=“<<res;
cin>>n1>>n2; break;
case 4:
cout<<"[Link] \
[Link] \[Link] \ res=n1/n2;
[Link]\n"; cout<<"Result of
"<<n1<<“/”<<n2<<"=“<<res;
cout<<"\nEnter your choice:";
res=n1%n2;
cin>>choice;
cout<<“Result of reminder =“<<res;
switch(choice) break;’
{ default:
case 1: cout<<"Wrong choice";
res=n1+n2; break;
cout<<"Result of”<<n1<<“+” }
<<n2<<“=”<<res; getch();
break; [Link] } 21
Questions
1. Write a program to check the given
alphabetic character is vowel or not
2. Write a program to check the given
character is in VIBGYOR or not
3. Write a program to you have entered
Choice 1 , then find the largest of two
numbers
Choice 2, find the square of a given no
Choice 3, to check the given no is +ve or -ve
[Link] 22
END
[Link] 23