Lecture 5&6 - Control Structures
Lecture 5&6 - Control Structures
LEARNING OUTCOMES
int main ()
{
for (int num = 10 ; num > 0; num --) {
cout << num << ", ";
}
cout << “Thank You!\n";
return 0;
}
Control Structures
Cont.
6. Selective Structure
switch (car) {
case 1: cout << “Pajero";
break;
case 2: cout << “Toyota";
break;
case 3: cout << “Mercedes";
break;
default:
cout << “Tuk Tuk";
}
Exercise Control Structures
Write a program whose output is shown
..
Control Structures
Control structures used in C++
– If (is a single selection control structure)
– if/else loop (is a double selection control
structure)
– while loop (is a repetitive conditional control
structure)
– do/while loop (is a repetitive conditional control
structure)
– for loop (is unconditional control structure)
– switch loop (is multiple selection control
structure)
Control Structures
if/else control structures
• These are conditional selection
control structures. They allow
selection to be made based on
two possible outcomes.
• Select is performed if the
condition is true or skipped and
perform on else statements if
the condition is false.
Control Structures
The general format of if/else:
if(condition)
{
Action1;
}
else
{
Action2;
}
Note: If condition is satisfied, only action1 is executed else action2 is executed. In all
instances, the use of action stands for one or many valid statements. The condition is an
large=a;
if(b>large)
large=b;
else
large=c;
return 0;
}
Example 2: Evaluating Even and Odd Numbers
Algorithm:Evaluating Even and Odd numbers
1. Request/Prompt for a number
2. Input the number
3. If the remainder of the number divided by 2 is equal to
zero
3.1 display message number is even
Solution
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter a number";
cin>>n;
if(n%2==0)
cout<<" The Number " <<n << " is Even";
else
cout<<" The Number "<< n << " is Odd";
system("pause");
return 0;
}
Control Structures
While Control Structure
• This is a repetitive conditional control
structure. The loop is tested at the beginning.
A program repeats an action while some
condition remains true.
• The general format is:
Example 3: Displaying using while loop
Solution
#include<iostream>
using namespace std;
int main()
{
int n=1;
while(n<=20)
{
cout<<n<<"\n";
n=n+1;
}
system("pause");
return 0;
}
Example 4: Displaying Numbers Squares and Cubes
from 1 to 20
#include<iostream>
using namespace std;
int main()
{
int n=1;
cout<<"Number\tSquare\tCube"<<endl;
while(n<=20)
{
cout<<n<<n << "\t" << n*n <<"\t" <<n*n*n <<endl;
n++;
}
system("pause");
return 0;
}
Do while Control Structure
}while(n<=20);
system("pause");
return 0;
}
For Control Structure
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Number\tSquare\tCube"<<endl;
for(n=1; n<=20; n++)
cout<<n <<"\t" <<n*n <<"\t" <<n*n*n <<endl;
system("pause");
return 0;
}
Example 7 Displaying with setwidth
Manipulation
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int n;
cout<<"Number"<< setw(15) <<"Square"<< setw(15) <<"Cube"<<endl;
for(n=1; n<=20; n++)
cout<<n <<setw(15) <<n*n <<setw(15) <<n*n*n <<endl;
system("pause");
return 0;
}
Example 8:Investment
Example 8: Computing Investment
#include<iostream> #include<iostream>
#include<cmath> #include<cmath>
#include<iomanip> #include<iomanip>
using namespace std; using namespace std;
int main() int main()
{ {
double p=1000; double p=1000;
double r=0.05; double r=0.05;
double a; double a;
int n;
int n;
cout<<fixed<<setprecision(2);
cout<<"Year”<<”\t\t”<<”Amount"<<endl;