Computer Programming C++ Chapter Two
Computer Programming C++ Chapter Two
Computer Programming
Chapter III
Control Structures
Discuss what decision and control structures are and why they are important
Example
Develop an algorithm, draw the flow chart, write a pseudo code and a C++ code to
add two numbers (integers)
I. Algorithm
Begin Calculation (the Program)
1. Input a Value for the 1st Variable
2. Input a Value for the 2nd Variable
3. Add the two values
4. Output the result
End Calculation
Not many programs execute all their statements in strict order from
beginning to end.
Most programs (like many humans) decide what to do in response to
changing circumstances.
The flow of control jumps from one part of the program to another,
depending on decision to make in the program.
Program statements that cause such jumps are called control
statements
There are two major categories: loops and decisions.
Haramaya University, HiT, School of Electrical and Computer
Saturday, June 8, 2024
Engineering
Control Structures
8
3. A shop will give discount of 10% if the cost of purchased quantity is more than 1000
birr
Ask user for quantity
Suppose, one unit will cost 100 (the price)
Output if n = 10
false 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, Fire.
Haramaya University, HiT, School of Electrical and Computer
Saturday, June 8, 2024
Engineering
2. Repetitive Structures or Loops (cont’d)
19
// example
#include<iostream>
using namespace std;
int main()
{
Print n int n;
cout<<"Enter Starting Number: ";
cin>>n;
n-- do
{
cout<<n<<" , ";
n--;
true }
n>0 while(n > 0);
cout<<"Fire. \n";
return 0;
false }
Output if n = 10
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, Fire.
Haramaya University, HiT, School of Electrical and Computer
Saturday, June 8, 2024
Engineering
2. Repetitive Structures or Loops (cont’d)
21 // example
#include<iostream> Enter a Number (0 to end): 123
using namespace std; You entered 123
int main() Enter a Number (0 to end): 90
{ You entered 90
unsigned int n; Enter a Number (0 to end): 12
do You entered 12
Enter a Number (0 to end): 0
{
You entered 0
cout<<"Enter a Number (0 to end): "; Loop Terminated!
cin>>n;
cout<<"you entered <<n<<" \n ";
}
while(n != 0);
cout<<"Loop Terminated!";
return 0;
}
Haramaya University, HiT, School of Electrical and Computer
Saturday, June 8, 2024
Engineering
2. Repetitive Structures or Loops (cont’d)
22
se
No lon
m ic o
It repeats the statement(s) while condition remain true
Condition is checked
Statement(s) is executed
Increment
Condition true
Body of loop
?
false
// example
#include<iostream>
using namespace std;
n
io
t
on
en
The fields in the for
at
iti
em
iz
nd
al
cr
loop are optional
iti
co
in
int main()
in
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, Fire.
Haramaya University, HiT, School of Electrical and Computer
Saturday, June 8, 2024
Engineering
2. Repetitive Structures or Loops (cont’d)
26
// example
#include<iostream>
using namespace std; Notice the omitted
int main()
{ fields in the for loop
int i = 0;
for (;;)
but the semi colon
{ among them are not
cout<<i<<" , ";
i++;
if(i == 10)
{
cout<<"Fire!";
break;
}
}
return 0;
}
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, Fire.
Haramaya University, HiT, School of Electrical and Computer
Saturday, June 8, 2024
Engineering
2. Repetitive Structures or Loops (cont’d)
27
Optionally using comma operator (,) we can specify more than one instruction in
any fields
// example
#include<iostream>
using namespace std;
int main()
{
for (int n = 0, i = 20; n != i; n++, i--)
cout<<n<<" , "<<i<<" , ";
cout<<"\b";
return 0;
}
0, 20, 1, 19, 2, 18, 3, 17, 4, 16, 5, 15, 6, 14, 7, 13, 8, 12, 9, 11
Haramaya University, HiT, School of Electrical and Computer
Saturday, June 8, 2024
Engineering
3. Divergence of Control
28
break
continue
goto and
exit
// example
#include<iostream>
using namespace std;
Using break int main()
{
for (int n = 10; n > 0; n--)
{
We can leave a loop cout<<n<<" , ";
if (n==3)
even if the condition for it end {
is not reached yet cout<<"Countdown Aborted";
break;
}
}
return 0;
}
// example
#include<iostream>
The continue instruction using namespace std;
int main()
{
for (int n = 10; n > 0; n--)
Causes the program to skip {
to the rest of the loop in the
if (n==5)
present iteration continue;
cout<<n<<" , ";
}
cout<<"Fire! ";
return 0;
}
10, 9, 8, 7, 6, 4, 3, 2, 1, Fire!
Haramaya University, HiT, School of Electrical and Computer
Engineering Saturday, June 8, 2024
The goto Instruction
31
// example
goto allows
#include<iostream>
to make absolute jump to another location using namespace std;
(point) in the program int main()
The destination is identified by a label, {
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, Fire!
Haramaya University, HiT, School of Electrical and Computer
Engineering Saturday, June 8, 2024
The exit function
32
// example
The purpose of exit to terminate the
#include<iostream>
running program with a specified using namespace std;
code int main()
{
for(int n = 10; n > 0; n--)
An exit code of 0 means the program {
terminates normally and any other if (n == 5)
code means that an error has occurred exit(0);
cout<<n<<" , ";
(happened)
}
cout<<"Fire.;
}
10, 9, 8, 7, 6
Haramaya University, HiT, School of Electrical and Computer
Engineering Saturday, June 8, 2024
4. Selective Structure (switch)
33
There are situation when a program must take one of several actions
depending on the value of some variable or expression
The switch statement provides
a convenient alternative to the if when dealing with a multi-way branch
Where
valueA, valueB etc are some integer or character constants
The switch evaluates expression
And then jumps to “case” labeled by the corresponding constant value or
The default “case” if no match has been found
The break statement is used to break out of the switch
1. Develop a program in C++ that calculates the factorial of a given number. Use for loop to
calculate the factorial, & do – while loop to perform the operation as many times as user want
Input: n for factorial and ch for repetition
Output: factorial of n
2. Write a program which takes a number x from the user and displays = 1 + 2 + 3 + … + x
Input: x
Output: = 1 + 2 + 3 + … + x
3. e can be approximated by e1 = 1+ by the first n term using Taylor/Maclaurin series expansion.
Write a C++ program to approximate the value of e using n terms
Input: n (number of terms)