0% found this document useful (0 votes)
15 views20 pages

MIU-SEN 101 (Principles of Programming 1) : Mr. Abdulhamid Idris

Sen

Uploaded by

kren4456
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)
15 views20 pages

MIU-SEN 101 (Principles of Programming 1) : Mr. Abdulhamid Idris

Sen

Uploaded by

kren4456
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/ 20

MIU-SEN 101 Mr.

Abdulhamid Idris
(PRINCIPLES OF PROGRAMMING 1)
-- 5 --
SECTION FIVE: REPETITION/LOOPING
A loop in programming is a control structure that allows you to repeat a block of code
multiple times.
They are useful for executing the same piece of code with different inputs or for iterating
over data structures like arrays.

Types of loops in C++


C++ supports three types of loops, these are:
1. for loop
2. while loop
3. do...while loops
REPETITION/LOOPING CONTD.
The for loop
The for loop is used when you know exactly how many times you want to loop through a
block of code
Flowchart of the for loop Syntax of the for loop

for ( initialization; test; update )


{

statement;
statement;
// Place as many statements here as necessary.

}
REPETITION/LOOPING CONTD.
The for loop contd.

for ( initialization; test; update )


{

statement;
statement;
// Place as many statements here as necessary.

 initialization is executed (one time) before the execution of the code block.

 test defines the condition for executing the code block.

 update is executed (every time) after the code block has been executed.
REPETITION/LOOPING CONTD.
The for loop contd.

Example 1
The following program uses a for loop to display the message Hello World! ten times
REPETITION/LOOPING CONTD.
The for loop contd.

Example 2
The following program uses a for loop to display the first ten natural numbers
REPETITION/LOOPING CONTD.
The for loop contd.

Example 3
The following program uses a for loop to display the first fifteen even numbers
REPETITION/LOOPING CONTD.
The while loop
The while loop has two important parts: (1) an expression that is tested for a true or false
value, and (2) a statement or block that is repeated as long as the expression is true.

Flowchart of the while loop Syntax of the while loop

while (condition)
{

statement;
statement;
// Place as many statements here as necessary.

}
REPETITION/LOOPING CONTD.
The while loop contd.

Example 1
The following program uses a while loop to display the message Hello World! ten times
REPETITION/LOOPING CONTD.
The while loop contd.

Example 2
The following program uses a while loop to display the first ten natural numbers
REPETITION/LOOPING CONTD.
The while loop contd.

Example 3
The following program uses a while loop to display the first fifteen even numbers
REPETITION/LOOPING CONTD.
The do while loop
The do while loop is a variant of the while loop. This loop will execute the code block once,
before checking if the condition is true, then it will repeat the loop as long as the condition is
true.
Flowchart of the do while loop Syntax of the do while loop

do
{
// statement(s) to be executed

}
while (condition);
REPETITION/LOOPING CONTD.
The do while loop contd.

Example 1
The following program uses a do while loop to display the message Hello World! ten times
REPETITION/LOOPING CONTD.
The do while loop contd.

Example 2
The following program uses a do while loop to display the first ten natural numbers
REPETITION/LOOPING CONTD.
The do while loop contd.

Example 3
The following program uses a while loop to display the first fifteen even numbers
EXERCISE
1. Use a while loop to display the first ten odd numbers.

2. Write a nested loop that displays 10 rows of ‘#’ characters. There should be 15 ‘#’ characters in
each row.
3. Write a for loop that displays the following set of numbers: 0, 10, 20, 30, 40, 50 . . . 1000
4. Write a loop that asks the user to enter a number. The loop should iterate 10 times and keep a
running total of the numbers entered.
5. Write a do-while loop that asks the user to enter two numbers. The numbers should be added and
the sum displayed. The user should be asked if he or she wishes to perform the operation again. If
so, the loop should repeat; otherwise, it should terminate.
ASSIGNMENT
1. Convert the following while loop to a do-while loop:
int x = 1;
while (x > 0)
{
cout << "enter a number: ";
cin >> x;
}
2. Convert the following do-while loop to a while loop:
char sure;
do
{
cout << "Are you sure you want to quit? ";
cin >> sure;
} while (sure != 'Y' && sure != 'N');
ASSIGNMENT CONTD.
3. Find and correct all the errors in the following program:
//This program adds two numbers entered by the user.
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
char again;
while (again == 'y' || again == 'Y')
cout << "Enter a number: ";
cin >> num1;
cout << "Enter another number: ";
cin >> num2;
cout << "Their sum is << (num1 + num2) << endl;
cout << "Do you want to do this again? ";
cin >> again;
return 0;
}

You might also like