CS Chapter4 2024
CS Chapter4 2024
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;
}
Update: updates the value of initialized variables and again checks the condition
#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:
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
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;}
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.
Syntax:
while (condition){
instructions;
}
Here,
If the condition evaluates to true, the code inside the while loop is executed.
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,
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.
#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):
#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;
}