Control Structures
Repetition (Loops)
ICT172: Principles to Programming
Lecture Notes 1
Quick Quiz: What is the output if the score is set to 85
if (score >= 90) {
cout<< 'A';
} else if (score >= 80) {
cout<< 'B';
} else if (score >= 70) {
cout<< 'C';
} else if (score >= 60) {
cout<< 'D';
} else {
cout<< 'F';
}
}
ICT172: Principles to Programming
Lecture Notes 2
Quick Quiz: What is the output if the score is set to 59
if (score >= 90) {
cout<< 'A';
} else if (score >= 80) {
cout<< 'B';
} else if (score >= 70) {
cout<< 'C';
} else if (score >= 60) {
cout<< 'D';
} else {
cout<< 'F';
}
}
ICT172: Principles to Programming
Lecture Notes 3
Quick Quiz: What is the output if the age is set to 61
if (age <= 12) {
cout<< "Child";
} else if (age >= 13 && age <= 19) {
cout<< "Teen";
} else if (age >= 20 && age <= 64) {
cout<< "Adult";
} else {
cout<< "Senior";
}
}
ICT172: Principles to Programming
Lecture Notes 4
Quick Quiz: What is the output if the age is set to 5
if (age <= 12) {
cout<< "Child";
} else if (age >= 13 && age <= 19) {
cout<< "Teen";
} else if (age >= 20 && age <= 64) {
cout<< "Adult";
} else {
cout<< "Senior";
}
}
ICT172: Principles to Programming
Lecture Notes 5
Quick Quiz: What is the output if the age is set to 70
if (age <= 12) {
cout<< "Child";
} else if (age >= 13 && age <= 19) {
cout<< "Teen";
} else if (age >= 20 && age <= 64) {
cout<< "Adult";
} else {
cout<< "Senior";
}
}
ICT172: Principles to Programming
Lecture Notes 6
Quick Quiz: What is the output if the age is set to 70
if (age <= 12) {
cout<< "Child";
} else if (age >= 13 && age <= 19) {
cout<< "Teen";
} else if (age >= 20 && age <= 64) {
cout<< "Adult";
} else {
cout<< "Senior";
}
}
ICT172: Principles to Programming
Lecture Notes 7
C++ Loops
• Suppose you want to add five numbers to find their
average.
cin >> num1 >> num2 >> num3 >> num4 >> num5;
sum = num1 + num2 + num3 + num4 + num5;
average = sum / 5;
• What of if you want to add and average 100, 1000, or
more numbers?
• This way of coding takes an exorbitant amount of
space and time.
• It also requires recoding for different values.
ICT172: Principles to Programming
Lecture Notes 8
Repetition/Loops
•Repetition control structures also called loops allow a
program to repeats statement(s) a certain number of
times based on some condition(s).
•C++ has three repetition, or looping, structures that let
you repeat statements over and over until certain
conditions are met.
A. while loop
B. do while loop
C. for loop
ICT172: Principles to Programming
Lecture Notes 9
The While Statement
•The while loop is a pre-test looping structure.
Syntax
initialize loop control variable (s) (LCV)
while (condition); //to test the LCV
{
Statement (s) …
Update loop control variable (s) (LCV)
}
while loop exits only when the expression is false.
ICT172: Principles to Programming
Lecture Notes 10
Repetition/Loops
ICT172: Principles to Programming
Lecture Notes 11
Example
int x = 3;
while (x>0) {
cout<<"x ="<<x;
x--;
}
ICT172: Principles to Programming
Lecture Notes 12
Example
Consider the following C++ program segment:
i = 20;
while (i < 20)
{
cout << i << " ";
i = i + 5;
}
cout << endl;
ICT172: Principles to Programming
Lecture Notes 13
The do while Statement
•The do while loop is a post-test looping structure.
•Syntax
initialize loop control variable (s) (LCV)
do{
Statement (s) …
Update loop control variable (s) (LCV)
} while (condition);
do while loop exits only when the expression is false.
ICT172: Principles to Programming
Lecture Notes 14
Example
int x = 3;
do{
cout<<“x =“<<x;
x--;
} while (x>0);
ICT172: Principles to Programming
Lecture Notes 15
for Statement
• The C++ for looping
structure is a specialized
form of the while loop.
• Its primary purpose is to
simplify the writing of
counter-controlled loops.
• for loop is typically called
a counted or indexed for
loop
ICT172: Principles to Programming
Lecture Notes 16
for loop
The syntax of the for loop is;
for (initialization; loop_condition; update_statement){
statement(s)…
}
ICT172: Principles to Programming
Lecture Notes 17
Example
for (int x=0; x<3; x++)
{
cout<<“x=”<<x;
}
First time: x = 0;
Second time: x = 1;
Third time: x = 2;
Fourth time: x = 3; (don’t execute the body)
ICT172: Principles to Programming
Lecture Notes 18
for <==> while
for (initialization; loop_condition; update_statement)
{
statements;
}
equals
Initialization;
while (loop_condition)
{
Statement;
Update statement;
}
ICT172: Principles to Programming
Lecture Notes 19
Example
• Assume that the following code is correctly
inserted into a program, what is the final value
of s?
int s = 0;
for (int i = 1; i < 5; i++)
{
s = 2 * s + i;
}
cout << s << endl;
ICT172: Principles to Programming
Lecture Notes 20
Factorial program
int main()
{
int number= 4;
int answer;
int count;
answer= 1;
count= number;
while (count >= 0) {
answer= answer* count;
count--;
}
cout<<answer<<endl;
return 0;
}
ICT172: Principles to Programming
Lecture Notes 21
Attendance Quiz
Which of these is a repetition structure in C++?
A. if ()
B. for ()
C. exit
D. continue
ICT172: Principles to Programming
Lecture Notes 22
Quick Quiz: What is the value of sum after the first iteration?
int sum = 0;
for (int i = 1; i <= 10; ++i) {
sum += i;
cout << "Sum after adding " << i << " is: " <<
sum << endl;
}
ICT172: Principles to Programming
Lecture Notes 23
Quick Quiz: What will be the value of sum after the loop completes all iterations?
int sum = 0;
for (int i = 1; i <= 10; ++i) {
sum += i;
cout << "Sum after adding " << i << " is: " <<
sum << endl;
}
ICT172: Principles to Programming
Lecture Notes 24
Quick Quiz: How many times will the while loop execute in the given code snippet?
int i = 1;
while (i <= 10) {
if (i % 2 == 0) {
cout << i << " is even" << endl;
} else {
cout << i << " is odd" << endl;
}
++i;
}
ICT172: Principles to Programming
Lecture Notes 25
Quick Quiz: What will be the output when i = 10?
int i = 1;
while (i <= 10) {
if (i % 2 == 0) {
cout << i << " is even" << endl;
} else {
cout << i << " is odd" << endl;
}
++i;
}
ICT172: Principles to Programming
Lecture Notes 26
Quick Quiz: What will be the output for i = 3 in the given code snippet?
int i = 1;
while (i <= 10) {
if (i % 2 == 0) {
cout << i << " is even" << endl;
} else {
cout << i << " is odd" << endl;
}
++i;
}
ICT172: Principles to Programming
Lecture Notes 27
Class Exercise
• Using the concept of loops, write a C++
program that takes the name of a friend and
prints it 15 times on the screen.
ICT172: Principles to Programming
Lecture Notes
End of Lecture
ICT172: Principles to Programming
Lecture Notes 29
break Statements
•When the break statement executes in a
repetition structure, it immediately exits from
the structure.
•The break statement is typically used for two
purposes:
i. To exit early from a loop.
ii. To skip the remainder of the switch
structure.
ICT172: Principles to Programming
Lecture Notes 30
Example
sum = 0;
cin >> num;
while (cin)
{
if (num < 0) {
cout << "Negative number found in the data." << endl;
break;
}
sum = sum + num;
cin >> num;
}
ICT172: Principles to Programming
Lecture Notes 31
Continue Statement
•When the continue statement is executed in a loop,
it skips the remaining statements in the loop and
proceeds with the next iteration of the loop.
•In a while and do. . .while structure, the expression
(that is, the loop-continue test) is evaluated
immediately after the continue statement.
•In a for structure, the update statement is executed
after the continue statement, and then the loop
condition (that is, the loop-continue test) executes.
ICT172: Principles to Programming
Lecture Notes 32
Example
sum = 0;
cin >> num;
while (cin)
{
if (num < 0) {
cout << "Negative number found in the data." <<
endl;
continue;
}
sum = sum + num;
cin >> num;
}
ICT172: Principles to Programming
Lecture Notes 33
End of Lecture
ICT172: Principles to Programming
Lecture Notes 34