Lecture 4
Lecture 4
Lecture 4
Lecture Outline
• Loop or Repetition Control Structures
• While Loops
• Do-while Loops
• For loops
• Nested loops
• Infinite loops
Introduction
• Each computer task being performed has been coded into the
software by a programmer, and each task must have the ability
to be used over and over.
• Imagine if a word processor was programmed to make text bold only
once.
• All programming languages provide statements to create a loop.
• The loop is the basic component of a repetition structure.
• These statements are a block of code, which under certain conditions,
will be executed repeatedly
Iteration
• The number of times a task is repeated is always a significant
part of any repetition structure.
• Programmers must be aware of how many times a loop will be
repeated to ensure that the loop performs the task correctly.
• In computer lingo, a single pass through a loop is called a loop
iteration.
• E.g. a loop that executes three times goes through three iterations.
Pre-Test and Post-Test Loops
• Loop structures can be divided into two fundamental types:
• pre-test loops
• post-test loops
• In a post-test loop, the test condition occurs after the body of
the loop is executed.
• In a pre-test loop, the test condition occurs before the body of
the loop is executed.
Pre-Test and Post-Test Loops
Pre-Test Loops Post-Test Loops
No
Yes
Yes
No
Using a Loop Control Variable
• To make any loop end correctly, we usually declare a variable to
control the loop’s execution. Three separate actions may occur:
• The control variable is initialized before entering the loop.
• The control variable is tested, and if the result is true, the loop body is
entered.
• Inside the body of the loop, we must take some action that alters the
value of the control variable (to bring the loop to a halt eventually).
Types of Loops in C++
• There are three types of loops in C++
• For loop
• While loop
• Do-while loop
While-Loop
• The while loop loops through a block
of code as long as a specified
condition is true • Syntax:
• A while loop evaluates the condition
• If the condition evaluates to true, the while (condition) {
code inside the while loop is //loop body to be executed
executed.
• The condition is evaluated again. }
• This process continues until the
condition is false.
• When the condition evaluates to
false, the loop terminates.
While-Loop
• Syntax: No
Syntax:
do {
// code block to be executed
Yes
} while (condition);
No
Example: Do-While Loop
Pseudocode: C++ code:
1. start int i = 0;
2. declare i as integer do {
3. do cout << i << "\n";
4. write i i++;
5. i = i + 1 } while (i < 5);
6. while i < 5
7. end
For-Loop
• When you know exactly how many times you want to loop through a
block of code, use the for loop.
Syntax:
for (initialization; condition; update) {
// body of for loop to be executed
}
• Initialization – initializes variables and is executed only once before the code block.
• Condition – if true the body of for-loop is executed. if false, the for-loop is
terminated.
• Update – updates the value of initialized variables. Executed every iteration.
For Loop
Initialization expression
Body of for-loop
Update expression
Loop terminates
Example: For-Loop
//Pseudocode to display 5 numbers // C++ Code to display 5 numbers
#include <iostream>
1. start using namespace std;
2. for (int i = 0; i < 5; i++) {
int main(){
3. write i
for (int i = 0; i < 5; i++) {
4. end for
cout << i << "\n";
5. end }
return 0;
}
Example: For-Loop
// Pseudocode to display /* C++ Code to display
// “Hello world” ten times “hello world” 10 times */
#include <iostream>
1. start
using namespace std;
2. for (int i = 0; i < 10; i++) {
3. write “Hello world” int main( ){
4. end for for (int i = 0; i < 10; ++i) {
5. end cout << “Hello world” << endl;
}
return 0;
}
Example
// Pseudocode to display the sum of the first n positive numbers
1. Start
2. Declare num, sum as integers
3. Set sum = 0
4. Write “Enter a positive number”
5. Input num
6. for (int i = 0; i < num; ++i) {
7. sum = sum + i
8. end for
9. write “Sum = ”+sum
10. end
Example: For-Loop
/* C++ Code to find the sum of first n natural numbers */
#include <iostream>
using namespace std;
int main( ){
int num, sum = 0;
cout << "Enter a positive integer: ";
cin >> num;
for (int i = 0; i < num; ++i) {
sum += i;
}
cout << "Sum = " << sum << endl;
return 0;
}
Example: While-Loop
/* C++ Code to find the sum of first while (number >= 0) {
n natural numbers */ sum += number;
#include <iostream> cout << "Enter a number: ";
using namespace std; cin >> number;
}
int main() { cout <<"\nThe sum is "<< sum
int number; << endl;
int sum = 0; return 0;
cout << "Enter a number: "; }
cin >> number;
The foreach Loop
} while(count == 1);
while(true) {
// body of the loop
}