BMS 201 C++ Control Structures 2021
BMS 201 C++ Control Structures 2021
End Select
If … End if
There are three ways of
implementing if
Single If
Double If
Multiple if
Single if
Syntax
If (condition)
{
Statements
}
End if
Example
If ( Age >= 18 )
{
cout << “ Adult”;
}
End if
IF-ELSE STATEMENT (Double if)
Example: while loop
// A program for custom countdown using the while loop
#include<iostream.h>
int main ( )
{
int n;
cout << “Enter the starting number:”;
cin >> n;
while (n > 0)
{
Cout << n << “ , ”;
– – n;
}
return 0;
}
The output of the above program is:-
Enter the starting number (e.g. 10)
10, 9, 8, 7, 6, 5, 4, 3, 2, 1
Another Example
intx, y ;
x=1;
y= 2;
While ( x<=5)
{
Cout << y << “ ”;
Y +=2;
x= x+1;
}
Output is 2 4 6 8 10
Comparing do.. While and While condition
Syntax
Do while(condition)
{ {
statements ; statements;
} }
while (condition);
Example using do -- while(condition)
int x, y;
x= 1;
y = 2;
do
{
cout << y << “ ”;
y =y+2;
x+=1;
}
Example using while (condition)
int x, y;
x= 1;
y = 2;
while(x<=5)
{
cout << y << “ ”;
y =y+2;
x+=1;
}
FOR CONTROL STRUCTURE