Lab 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

The Islamic University of Gaza

Engineering Faculty

Department of Computer Engineering

Fall 2021

ECOM 2003

Eng. Baraa Alastal

Computer Programming: C++


Lab#5: Loops Part I

Contents
❖ Introduction
❖ While loop
❖ Do-While loop
❖ for loop

Introduction
Loops are used in programming to repeat a specific block until some end condition
is met.
Suppose that you need to display a string (e.g., "Welcome to C++!") 100 times. It
would be tedious to write the following statements 100 times:
So, how do you solve this problem?
C++ provides a powerful construct called a loop that controls how many times an
operation or a sequence of operations is performed in succession. Using a loop
statement, you simply tell the computer to display a string 100 times without
having to code the print statement 100 times.

The While Loop


In computer programming, a while loop executes statements repeatedly while the
condition is true. The syntax of a while loop is

2
Lab#5: Loops Part I

❖ How while loop works?


1. The while loop evaluates the test expression.
2. If the test expression is true, codes inside the body of while loop is evaluated.
3. Then, the test expression is evaluated again. This process goes on until the test
expression is false.
4. When the test expression is false, while loop is terminated.

❖ Flowchart of while Loop

You can write a sentence "Welcome to C++!" 100 times using the while loop as
follows:
int count = 0;
while (count < 100)
{
cout << "Welcome to C++!\n";
count++;
}

3
Lab#5: Loops Part I

❖ Ex1: Write C++ Program to find factorial of a number

In this program, user is asked to enter a positive integer which is stored in variable
number. Let's suppose, user entered 4.
Then, the while loop starts executing the code. Here's how while loop works:
1. Initially, i = 1, test expression i <= number is true and factorial becomes 1.

2. Variable i is updated to 2, test expression is true, factorial becomes 2. 3.

Variable i is updated to 3, test expression is true, factorial becomes 6.

4. Variable i is updated to 4, test expression is true, factorial becomes 24.

5. Variable i is updated to 5, test expression is false and while loop is terminated.

The Do-While Loop


A do-while loop is the same as a while loop except that it executes the loop body
first and then checks the loop continuation condition.
The syntax of a while loop is

4
Lab#5: Loops Part I

❖ How do...while loop works?


1. The codes inside the body of loop are executed at least once. Then, only the test
expression is checked.
2. If the test expression is true, the body of loop is executed. This process continues
until the test expression becomes false.
3. When the test expression is false, do...while loop is terminated.

❖ Flowchart of do...while Loop

❖ Ex2: Write C++ Program to add all numbers entered by user until he enters 0

5
Lab#5: Loops Part I

The for Loop


Repeats a statement or group of statements while a given condition is true. It tests
the condition before executing the loop body. Its syntax is:

❖ How for loop works?


1. The initialization statement is executed only once at the beginning.

2. Then, the test expression is evaluated.


3. If the test expression is false, for loop is terminated. But if the test expression
is true, codes inside body of for loop is executed and update expression is
updated.
4. Again, the test expression is evaluated and this process repeats until the test
expression is false.

6
Lab#5: Loops Part I

❖ Flowchart of for Loop

❖ Ex3: Write C++ Program to find factorial of a number.

7
Lab#5: Loops Part I

Note:
❖ The initial-action in a for loop can be a list of zero or more comma-separated
expressions.
❖ The action-after-each-iteration in a for loop can be a list of zero or more comma-
separated statements.
for (int i = 0, j = 0; (i + j < 10); i++, j++) {
// Do something
}
❖ If the loop-continuation-condition in a for loop is omitted, it is implicitly true.

for ( ; ; ) Equivalent while (true)


{ {
// Do something // Do something
} This is better }
(a) (b)

8
Lab#5: Loops Part I

❖ Lab work:

1. Convert the following for loop statement to a while loop and to a do-while
loop:
int sum = 0;
for (int i = 0; i <= 1000; i++)
sum = sum + i;
2. Write a program to calculate the sum of Natural Numbers "user
entered number ".

❖ Home work:

1- Write a program that prompts the user to enter 10 integer numbers


and then prints the largest number.
2- (Find numbers divisible by 3 or 6, but not both) Write a program that
displays all the numbers from 300 to 400, 5 per line, that are divisible
by 3 or 6, but not both. Numbers are separated by exactly one space.

You might also like