0% found this document useful (0 votes)
3 views7 pages

CS Chapter4 2024

Uploaded by

takisamai058
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)
3 views7 pages

CS Chapter4 2024

Uploaded by

takisamai058
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/ 7

University of Batna 2 Module: Computer Science 1

Institute of Hygiene and Safety Level: 1st Year Bachelor's Degree


Department of Common Core Studies

Chapter 4: Loops
1. Introduction
In computer programming, loops are used to repeat a block of code. For example, let's say
we want to show a message 100 times. Then instead of writing the print statement 100
times, we can use a loop.
That was just a simple example; we can achieve much more efficiency and sophistication in
our programs by making effective use of loops.
There are 3 types of loops in C++:
1. for loop
2. while loop
3. do...while loop

2. For loop
A For loop is a repetition control structure that allows us to write a loop that is executed a
specific number of times. The loop enables us to perform n number of steps together in one
line. It has three parameters. The first parameter is initialization, which is executed once at
the beginning of the loop. The second parameter is condition, which tells if the loop should
continue to execute. The third part is incrementing, which happens right before each
execution of the body of the for loop.

Syntax:
for (declaration_initialisation; condition; update) {
instructions;
}

declaration_initialisation : declares and initializes variable and is executed only


once befor the loop starts.
condition: if true, the body of for loop is executed if false, the for loop is terminated

Update: updates the value of initialized variables and again checks the condition

Dr. Beloucif Assia 1


University of Batna 2 Module: Computer Science 1
Institute of Hygiene and Safety Level: 1st Year Bachelor's Degree
Department of Common Core Studies

a. Flowchart of the for loop

Figure1. flowchart of the for loop


Example 1:

#include <iostream>
using namespace std;
int main() {
for(int i=1;i<5;i=i+1){
cout<<“Hello"<<endl;
}
return 0;
}

Example 1 explained:
Here, we can explain the Loop parameters as the following:

• Parameter 1: int i=1 sets a variable before the loop starts.


• Parameter 2: i<5 defines the condition for the loop to run (i must be less than 5). If
the condition is true, the loop will start over again, if it is false, the loop will end.
• Parameter 3: i=i+1 increases the value of the counter i each time the code block in
the loop has been executed.
We can explain the Loop iterations as the following:

Iteration Variable condition Action


1st i=1 true Hello is printed and i is increased to 2
2nd i=2 true Hello is printed and i is increased to 3
3rd i=3 true Hello is printed and i is increased to 4
4th i=4 true Hello is printed and i is increased to 5
5th i=5 false The loop is terminated

Dr. Beloucif Assia 2


University of Batna 2 Module: Computer Science 1
Institute of Hygiene and Safety Level: 1st Year Bachelor's Degree
Department of Common Core Studies

Example 2: Find the sum of first ten Natural Numbers


#include <iostream>
using namespace std;
int main() {
int sum;
sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
cout << "Sum = " << sum << endl;
return 0;}

Example 2 explanation:

In the above example, we have the sum variable which will contain the sum of the ten first
integer numbers. First variable sum is assigned with 0. And then the for loop is iterated, we
can explain the for parameters as the following:
int i = 1: initializes the i variable

i <= 10: runs the loop as long as i is less than or equal to 10

i++: increases the i variable by 1 in each iteration

When i becomes 11, the condition is false and sum will be equal to 0 + 1 + 2 + ... + 10.
Example 3: Multiplication of two integers using for loop

#include <iostream>
using namespace std;
int main( ){
int a,b, c=0;
cout<<"Enter an integer number: ";
cin>>a;
cout<<"Enter an integer number: ";
cin>>b;
for (int i=1;i<=b; i++ ){
c=c+a;
}
cout<<"the result is: "<<c<<endl;
return 0;}

Dr. Beloucif Assia 3


University of Batna 2 Module: Computer Science 1
Institute of Hygiene and Safety Level: 1st Year Bachelor's Degree
Department of Common Core Studies

Notes:

• The initialization and increment statements can perform operations unrelated to the
condition statement, or nothing at all – if you wish to do. But the good practice is to
only perform operations directly relevant to the loop.
• A variable declared in the initialization statement is visible only inside the scope of
the for loop and will be released out of the loop.
• Don’t forget that the variable which was declared in the initialization statement can
be modified during the loop, as well as the variable checked in the condition.
• Don’t write a semicolon after the end of parenthesis.

3. C++ while loop:


The while loop loops through a block of code as long as a specified condition is true. The
syntax of the while loop is:

Syntax:
while (condition){
instructions;
}

Here,

A while loop evaluates the condition

If the condition evaluates to true, the code inside the while loop is executed.

The condition is evaluated again.

This process continues until the condition is false.

When the condition evaluates to false, the loop terminates.

Flowchart of the while loop:

Figure2. flowchart of the while loop

Dr. Beloucif Assia 4


University of Batna 2 Module: Computer Science 1
Institute of Hygiene and Safety Level: 1st Year Bachelor's Degree
Department of Common Core Studies

Example:
#include <iostream>
using namespace std;
int main(){
int i=0;
while(i<5){
cout<<"i="<<i<<endl;
i++; }
cout<<“The last value of i is:"<<i<<endl;
return 0;
}

Example explanation:
In this example, the code in the loop will run, over and over again, as long as a variable i is
less than 5. When the value of i is incremented to 6 the condition is false and the loop is
terminated and then the message “The last value of i is: 6" is printed on the
screen.

4. do/while loop
The do/while loop is a variant of the while loop with one important difference: the body of
do/while loop is executed once before the condition is checked.

Syntax:
do{
instructions;
}while(condition);

Here,

instructions are executed at first. Then condition is evaluated.


If condition evaluates to true, the body of the loop inside the do statement is executed
again.
The condition is evaluated once again.

If condition evaluates to true, the body of the loop inside the do statement is executed
again.

This process continues until condition evaluates to false. Then the loop stops.

Dr. Beloucif Assia 5


University of Batna 2 Module: Computer Science 1
Institute of Hygiene and Safety Level: 1st Year Bachelor's Degree
Department of Common Core Studies

Flowchart of the do/while loop:

Figure 3. flowchart of the do/while loop


Example 1:

#include <iostream>
using namespace std;
int main(){
int pass;
do{
cout<<“Please enter password";
cin>>pass;
}while(pass !=10);
return 0;
}

Example explanation:
When the user runs this program, a message “Please enter password" is displayed
on the screen, and he can type his password. If the entered password is not equal to 10, the
user is prompted to enter the password again.

5. Infinite loop
If the condition in a loop is always true, it runs forever (until memory is full). For example
(using for loop):

Dr. Beloucif Assia 6


University of Batna 2 Module: Computer Science 1
Institute of Hygiene and Safety Level: 1st Year Bachelor's Degree
Department of Common Core Studies

#include <iostream>
using namespace std;
int main(){
for(int i = 1; i > 0; i++) {
cout<<i;
}
return 0;
}

In the above program, the condition i > 0 is always true which will then run the code for
infinite times.

6. 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
example:

#include <iostream>
using namespace std;
int main(){
for (int i = 1; i <= 2; ++i) {
cout << "Outer: " << i << endl;
for (int j = 1; j <= 3; ++j) {
cout << " Inner: " << j << endl;
}
} return 0;
}

Dr. Beloucif Assia 7

You might also like