Decision Control and Loop Control in C++
Decision Control and Loop Control in C++
_______________________________________________________________________________________________
Example :
#include<iostream>
using namespace std;
int main()
{
int num ;
cout<<"Enter a number less than 10= " ;
cin>>num ;
if ( num <= 10 )
{
cout<<"Number is less than 10";
}
}
Output :
2) If-else :
Syntax :
if(Condition)
{
// Execute the Statements if condition is TRUE;
}
else
{
// Execute the Statements if condition is FALSE;
}
Flowchart :
Example :
#include<iostream>
using namespace std;
int main()
{
int num ;
cout<<"Enter a number less than 10= " ;
cin>>num ;
if ( num <= 10 )
{
cout<<"Number is less than 10";
}
else
{
cout<<"Wrong Number !!! please enter number less than 10 ";
}
}
Output:
3) if-else ladder :
Syntax :
if(Condition 1)
{
// Execute the Statement 1 if condition 1 is TRUE;
}
else if(Condition 2)
{
// Execute the Statement 2 if condition 2 is TRUE;
}
else if(Condition 3)
{
// Execute the Statement 3 if condition 3 is TRUE;
}
else
{
// Execute the Statement if every condition is FALSE;
}
Flowhart :
Example :
#include <iostream>
using namespace std;
int main()
{
int age;
cout<<"Enter your age\n";
cin>>age;
if (age>=18)
{
cout<<"You can vote!";
}
else if(age>=10)
{
cout<<"You are between 10 to 18 and you can vote for classroom election";
}
else if(age>=3)
{
cout<<"You are between 3 to 10 and you can vote for babies election";
}
else{
cout<<"You cannot vote because your not born!";
}
}
Output :
4) nested if-else:
Syntax:
if(Condition 1)
{
// Execute the Statements if condition 1 is TRUE;
if(Condition 2)
{
// Execute the Statements if condition 2 is TRUE;
if(Condition 3)
{
// Execute the Statements if condition 3 is TRUE;
}
else
{
// Execute the Statement if condition 3 is FALSE;
}
}
else
{
// Execute the Statement if condition 2 is FALSE;
}
}
else
{
// Execute the Statement if condition 1 is FALSE;
}
Flowchart :
Example :
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"enter the number greater than 0 and less than 100 = \t";
cin>>num;
if(num>0)
{
if(num<100)
{
cout<<"\tnumber is in between 1 to 100 \n";
}
else
{
cout<<"\tnumber must be greater than 100 \n";
}
}
else
{
cout<<"\t\t\tnumber is zero or smaller than zero \n";
}
return 0;
}
Output :
__________________________________________________________
Selection Statements
__________________________________________________________
• Every time we execute the switch the value inside choice variable is matched with case
name/ case value.
• Switch executes that block of code, which matches the case value/ case_name.
• If the value does not match with any of the cases, then thedefault block is executed.
Switch Case :
NOTE: Even if you don’t write break it will not give you an error . But that makes your
switch partially perfect .
Example :
#include<iostream>
using namespace std;
int main()
{
int order;
cout<<"\t\t\t\t\t\t\t\tWELCOME TO ROYAL HOTEL\n\n";
cout<<"\t\t\t\t\t\t************************ MENU
************************\t\t\t\t\t\t\n";
cout<<"\t\t\t\t\t\t\t\t\t 1. TEA\n";
cout<<"\t\t\t\t\t\t\t\t\t 2. COFFEE\n";
cout<<"\t\t\t\t\t\t\t\t\t 3. WATER\n";
cout<<"\n\n\t\t\t\t enter your order = ";
cin>>order;
switch(order)
{
case 1: cout<<"\t\t\t\t\t\t\t serve Tea\n";
break;
case 2: cout<<"\t\t\t\t\t\t\t serve Coffee\n";
break;