0% found this document useful (0 votes)
64 views13 pages

C++ Loop Types: For, While, Do-While

Loops allow code to be repeatedly executed in C++. The main types of loops are the for loop, while loop, and do-while loop. For loops iterate over a set number of times based on an initialization, condition, and increment/decrement. While loops repeat as long as a condition is true. Do-while loops execute the code block at least once even if the condition is false. Nested loops can iterate through multi-dimensional data. Loops improve efficiency and readability by automating repetitive tasks versus copying and pasting code.

Uploaded by

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

C++ Loop Types: For, While, Do-While

Loops allow code to be repeatedly executed in C++. The main types of loops are the for loop, while loop, and do-while loop. For loops iterate over a set number of times based on an initialization, condition, and increment/decrement. While loops repeat as long as a condition is true. Do-while loops execute the code block at least once even if the condition is false. Nested loops can iterate through multi-dimensional data. Loops improve efficiency and readability by automating repetitive tasks versus copying and pasting code.

Uploaded by

Jalal Ahmadi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

IN the name of Allah

Types of Loops in C++

In programming, a loop is a construct that allows a set of instructions to be


executed repeatedly until a certain condition is met. Loops are used to automate
repetitive tasks and to iterate over collections of data.
There may be a situation, when you need to execute a block of code
several number of times. In general, statements are
executed sequentially: The first statement in a function is executed first,
followed by the second, and so on.
In C++ there are three main types of loops exist;
1: the for loop
2: the while loop
3: the do-while loop
4: Nested loops
1: the for loop

In programming, a for loop is a control flow statement that allows you to iterate over a set of values a
specific number of times. It consists of three parts: initialization, condition, and increment/decrement.
Execute a sequence of statements multiple times and abbreviates the
code that manages the loop variable.
Example of For loop
#include <iostream>
using namespace std;

int main() {

for (int i = 1; i <= 10; ++i) { Out put:


cout << i<<endl; 1
} 2
3
return 0; 4
} 5
6
7
8
9
10
The while loop

A while loop in C++ is a control flow statement that allows you to repeatedly execute a block of code as
long as a certain condition is true.
Repeats a statement or group of statements while a given condition is true. It tests the condition before
executing the loop body.

#include <iostream>
using namespace std;

int main() {
int i = 1;

while (i <= 5) {
cout << i << endl;
i++;
}

return 0;
}
#include <iostream>
Out put:
using namespace std;
A
B
int main() {
C
char letter = 'A';
D
Up to
while (letter < 'Z') {
Z
cout << letter << " ";
letter++;
}

return 0;
}
do { // statements to be executed } while (condition);

the do-while loop


do { // statements to be executed } while (condition);
do { // statements to be executed } while (condition);

A do-while loop in C++ is a control flow statement that allows you to repeatedly execute a block of
code as long as a certain condition is true, but unlike the while loop, the condition is checked at the
end of each iteration instead of at the beginning. This means that the block of code is executed at least
once, even if the condition is false.
Like a ‘while’ statement, except that it tests the condition at the end of the loop body.

the syntax:

do {
// statements to be executed
} while (condition);
Example of do while loop

#include <iostream> Output:


using namespace std; 100
int main (){ 99
int a = 100; 98
int i = 100; 97
do { .
cout << i << endl; .
i--; .
} while (i > 0); .
return 0; 1
}
Nested loops

Nested loops can also be used for more complex tasks, such as iterating through a 2D array or searching for
specific values in a set of data. In general, nested loops are used whenever we need to perform a task that
requires iterating through multiple sets of data, where each set of data requires its own set of iterations.
We can use one or more loop inside any another ‘while’, ‘for’ or
‘do..while’ loop.
Example of Nested loop

#include <iostream> Out put


Table of 10
int main () { 10*1=10
10*2=20
using namespace std; 10*3=30
int m; 10*4=40
10*5=50
int n; 10*6=60
for (m = 10; m <= 11; m++) { 10*7=70
10*8=80
cout << "Table of " << m << endl; 10*9=90
for (n = 1; n <= 10; n++) { 10*10=100
Table of 11
cout << m << "*" << n << "=" << (m*n) 11*1=11
<< endl; 11*2=22
11*3=33
} 11*4=44
} 11*5=55
11*6=66
return 0; 11*7=77
} 11*8=88
11*9=99
11*10=110
What is difference between for
loop and while loop
In C++, both for loops and while loops are used for iterating over a set of statements, but they differ in their syntax and
how they control the flow of execution.
The for loop is typically used when you know the exact number of times you want to loop through a block of code. The
syntax of a for loop includes three parts: initialization, condition, and increment.
The while loop, on the other hand, is used when you don't know the exact number of times you want to loop through a
block of code. The syntax of a while loop includes only a condition.
Why we use loop in C++?
The advantageous of loops

Overall, loops are an essential tool in programming, and their


advantages include efficiency, flexibility, condition-based
execution, improved readability, and reduced errors.
1. Solve question mathematics ,
2. Print the marks student easy.
3. Print the name of students easy
4. It can print arrays elements easy.
5. Loops is used for making program facilities.
Thanks a lot from your attention

You might also like