LOOPS
LOOPS
#include <iostream>
int main()
return 0;
Output
Hello World
Hello World
Hello World
Hello World
Hello World
while loop
1. – First checks the condition, then
executes the body.
for loop
2. – firstly initializes, then, condition
check, execute body, update.
do-while
loop
3.
– firstly, execute the body then
condition check
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.
Syntax:
for (initialization expr; test expr; update expr)
{
// body of the loop
// statements we want to execute
}
Explanation of the Syntax:
• Initialization statement: This statement gets executed only once, at the
beginning of the for loop. You can enter a declaration of multiple variables of
one type, such as int x=0, a=1, b=2. These variables are only valid in the
scope of the loop. Variable defined before the loop with the same name are
hidden during execution of the loop.
• Condition: This statement gets evaluated ahead of each execution of the
loop body, and abort the execution if the given condition get false.
• Iteration execution: This statement gets executed after the loop body,
ahead of the next condition evaluated, unless the for loop is aborted in the
body (by break, goto, return or an exception being thrown.)
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.
Example1:
for(int i = 0; i < n; i++)
{
// BODY
}
Example2:
for(auto element:arr)
{
//BODY
}
Flow Diagram of for loop:
Example1:
• C++
#include <iostream>
return 0;
Output
Hello World
Hello World
Hello World
Hello World
Hello World
• C++
#include <iostream>
return 0;
Output
40 50 60 70 80 90 100
• C++
#include <iostream>
j++, k--, i += k) {
cout << i << " " << j << " " << k << "\n";
return 0;
Output
0 10 20
19 11 19
37 12 18
54 13 17
#include <iostream>
int main()
{
int i = 99;
return 0;
Output
0 1 2 3 4
99
• C++
#include <iostream>
int main()
{
int i = 99;
return 0;
Output
0 1 2 3 4
5
• C++
#include <bits/stdc++.h>
int main()
{
vector<int> v = { 1, 2, 3, 4, 5 };
it != v.end(); it++) {
return 0;
Output
1 2 3 4 5
update_expression;
}
Flow Diagram of while loop:
Example:
• C++
// C++ program to Demonstrate while loop
#include <iostream>
int main()
// initialization expression
int i = 1;
// test expression
while (i < 6) {
// update expression
i++;
return 0;
Output
Hello World
Hello World
Hello World
Hello World
Hello World
#include <iostream>
using namespace std;
int main()
do {
// loop body
// update expression
i++;
return 0;
Output
Hello World
#include <iostream>
int main()
int i;
// expression is blank
for (;;) {
/*
while (i != 0)
i-- ;
*/
/*
while (true)
*/
}
Output:
This loop will run forever.
This loop will run forever.
...................
Time complexity: O(infinity) as the loop will run forever.
Space complexity: O(1)
Using While loop:
• C++
#include <iostream>
int main()
while (1)
return 0;
Output:
This loop will run forever.
This loop will run forever.
...................
Time complexity: O(infinity) as the loop will run forever.
Space complexity: O(1)
Using the Do-While loop:
• C++
#include <iostream>
int main()
do {
} while (1);
return 0;
Output:
This loop will run forever.
This loop will run forever.
...................
Time complexity: O(infinity) as the loop will run forever.
Space complexity: O(1)
Now let us take a look at decrementing loops.
Sometimes we need to decrement a variable with a looping condition.
Using for loop
• C++
#include <iostream>
int main() {
for(int i=5;i>=0;i--){
cout<<i<<" ";
return 0;
Output
5 4 3 2 1 0
#include <iostream>
int i=5;
while(i--){
cout<<i<<" ";
cout<<endl;
i=5;
while(i){
cout<<i<<" ";
i--;
return 0;
Output
4 3 2 1 0
5 4 3 2 1
int main() {
int i=5;
do{
cout<<i<<" ";
}while(i--);
Output
5 4 3 2 1 0
#include <iostream>
#include <vector>
// For loop
cout << "For loop: The value of i is: " << i << endl;
// While loop
int j = 1;
while (j <= 5) {
cout << "While loop: The value of j is: " << j << endl;
j++;
// Do-while loop
int k = 1;
do {
cout << "Do-while loop: The value of k is: " << k << endl;
k++;
cout << "Range-based for loop: The value of element is: " << element <<
endl;
return 0;
Output
For loop: The value of i is: 1
For loop: The value of i is: 2
For loop: The value of i is: 3
For loop: The value of i is: 4
For loop: The value of i is: 5
While loop: The value of j is: 1
While loop: The value of j is: 2
While loop: The value of j is: 3
While loop: The value of j is: 4
While loop: The value of j is: 5
Do-while loop: The value of k is: 1
Do-while loop: The value of k is: 2
Do-while loop: The value of k is: 3
Do-while loop: The value of k is: 4
Do-while loop: The value of k is: 5
Range-based for loop: The value of element is: 1
Range-based for loop: The value of element is: 2
Range-based for loop: The value of element is: 3
Range-based for loop: The value of element is: 4
Range-based for loop: The value of element is: 5
• C++
#include <iostream>
int main() {
int j = 0;
while(j < 5) {
j++;
int k = 0;
do {
k++;
return 0;
Output
For loop:
0
1
2
3
4
While loop:
0
1
2
3
4
Do-while loop:
0
1
2
3
4
Advantages :
1. High performance: C++ is a compiled language that can produce efficient
and high-performance code. It allows low-level memory manipulation and
direct access to system resources, making it ideal for applications that
require high performance, such as game development, operating systems,
and scientific computing.
2. Object-oriented programming: C++ supports object-oriented programming,
allowing developers to write modular, reusable, and maintainable code. It
provides features such as inheritance, polymorphism, encapsulation, and
abstraction that make code easier to understand and modify.
3. Wide range of applications: C++ is a versatile language that can be used
for a wide range of applications, including desktop applications, games,
mobile apps, embedded systems, and web development. It is also used
extensively in the development of operating systems, system software, and
device drivers.
4. Standardized language: C++ is a standardized language, with a
specification maintained by the ISO (International Organization for
Standardization). This ensures that C++ code written on one platform can be
easily ported to another platform, making it a popular choice for cross-
platform development.
5. Large community and resources: C++ has a large and active community
of developers and users, with many resources available online, including
documentation, tutorials, libraries, and frameworks. This makes it easy to
find help and support when needed.
6. Interoperability with other languages: C++ can be easily integrated with
other programming languages, such as C, Python, and Java, allowing
developers to leverage the strengths of different languages in their
applications.
Overall, C++ is a powerful and flexible language that offers many advantages
for developers who need to create high-performance, reliable, and scalable
applications.
More Advanced Looping Techniques
• Range-Based for Loop in C++
• for each Loop in C++
Important Points
• Use for a loop when a number of iterations are known beforehand, i.e. the
number of times the loop body is needed to be executed is known.
• Use while loops, where an exact number of iterations is not known but the
loop termination condition, is known.
• Use do while loop if the code needs to be executed at least once like in
Menu-driven programs