0% found this document useful (0 votes)
5 views26 pages

Lecture 4

Lecture notes on computers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views26 pages

Lecture 4

Lecture notes on computers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Programming

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

while (condition) { Yes

//loop body to be executed


}
// C++ code to print from 0 to 4
Example: While-Loop #include <iostream>
Pseudocode to print numbers from 0 to 4 using namespace std;

1. Start int main(){


2. Declare i as integer int i = 0;
3. Set i = 0 while (i < 5) {
4. While i < 5 then cout << i << "\n";
5. Write i i++;
6. i=i+1 }
7. End while return 0;
8. End }
Do-While Loop
• The do-while loop is a post – test loop.
• The body of the loop is executed at first. Syntax:
Then the condition is evaluated.
• If the condition evaluates to true, the body
of the loop inside the do statement is do {
executed again. // loop body to be executed
• The condition is evaluated once again. } while (condition);
• If the condition evaluates to true, the body
of the loop inside the do statement is
executed again.
• This process continues until the condition
evaluates to false. Then the loop stops.
Do-While Loop

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

for (initialization; condition; update) {


false
// body of for loop to be executed Test
condition
}
true

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

• There is also a "for-each loop", Syntax:


which is used exclusively to loop
through elements in an array (or
vectors) for (type variableName : arrayName) {
• For every value in the collection, // body of loop to be executed
the for loop is executed and the }
value is assigned to the variable.
Example: foreach Loop

//Pseudocode //C++ Code


Start #include <iostream>
using namespace std;
Declare num [5] as integer
int main(){
Set num = {10, 20, 30, 40, 50}
int num[5] = {10, 20, 30, 40, 50};
for (int i : num) for (int i: num) {
write i cout << i << "\n";
End for }
End return 0;
}
Nested Loops
• It is also possible to place a loop inside another loop. This is called a
nested loop.
• The "inner loop" will be executed one time for each iteration of the
"outer loop”
for (int i = 1; i <= 2; ++i ) { //outer loop
cout << "Outer: " << i << "\n"; // Executes 2 times
for (int j = 1; j <= 3; ++j) { // inner loop
cout << " Inner: " << j << "\n"; //Executes 6 times (2 * 3)
}
}
Infinite Loop
• If a loop’s test condition is never satisfied, then the loop will never be
exited, and it will become an infinite loop.
• It runs forever until memory is full.
• When you set up a loop and put in a test condition, be sure that the test
condition can be met.
• Computers don’t mind doing a task many times but forever is simply too many!
Infinite Loop
// infinite for loop // infinite do...while loop

for(int i = 1; i > 0; i++) { int count = 1;


// block of code
} do {
// body of loop
// infinite while loop

} while(count == 1);
while(true) {
// body of the loop
}

You might also like