0% found this document useful (0 votes)
21 views12 pages

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

Uploaded by

yonisha93
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)
21 views12 pages

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

Uploaded by

yonisha93
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

C++ Loops

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++.

• for loop
• while loop
• do...while loop
1) C++ for loop
The syntax of for-loop is:

for (initialization; condition; update) {


// body of-loop
}

Here,

• initialization - initializes variables and is executed only once


• 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

1
Flowchart of for Loop in C++

Flowchart of for loop in C++

Example 1: Printing Numbers From 1 to 5


#include <iostream>

using namespace std;

int main() {
for (int i = 1; i <= 5; ++i) {
cout << i << " ";
}
return 0;
}

Output

2
1 2 3 4 5

Here is how this program works

Example 2: Display a text 5 times


Iteration Variable i <= 5 Action

1st i=1 true 1 is printed. i is increased to 2.

2nd i=2 true 2 is printed. i is increased to 3.

3rd i=3 true 3 is printed. i is increased to 4.

4th i=4 true 4 is printed. i is increased to 5.

5th i=5 true 5 is printed. i is increased to 6.

6th i=6 false The loop is terminated

// C++ Program to display a text 5 times

#include <iostream>

using namespace std;

int main() {
for (int i = 1; i <= 5; ++i) {
cout << "Hello World! " << endl;
}
return 0;
}
Run Code

Output

Hello World!
Hello World!
Hello World!
Hello World!
Hello World!

Here is how this program works


3
Iteration Variable i <= 5 Action

1st i = 1 true Hello World! is printed and i is increased to 2 .

2nd i = 2 true Hello World! is printed and i is increased to 3 .

3rd i = 3 true Hello World! is printed and i is increased to 4 .

4th i = 4 true Hello World! is printed and i is increased to 5 .

5th i = 5 true Hello World! is printed and i is increased to 6 .

6th i = 6 false The loop is terminated

Example 3: Find the sum of first n Natural Numbers


// C++ program to find the sum of first n natural numbers
// positive integers such as 1,2,3,...n are known as natural numbers

#include <iostream>

using namespace std;

int main() {
int num, sum;
sum = 0;

cout << "Enter a positive integer: ";


cin >> num;

for (int i = 1; i <= num; ++i) {


sum += i;
}

cout << "Sum = " << sum << endl;

return 0;
}
Run Code

4
Output

Enter a positive integer: 10


Sum = 55

In the above example, we have two variables num and sum . The sum variable is
assigned with 0 and the num variable is assigned with the value provided by the user.
Note that we have used a for loop.

for(int i = 1; i <= num; ++i)

Here,

• int i = 1 : initializes the i variable


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

• ++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 .

2) C++ while Loop


The syntax of the while loop is:

while (condition) {
// body of the loop
}

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.

5
Flowchart of while Loop

Flowchart of C++ while loop


Example 1: Display Numbers from 1 to 5
// C++ Program to print numbers from 1 to 5

#include <iostream>

using namespace std;

int main() {
int i = 1;

// while loop from 1 to 5


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

return 0;
}
Run Code

6
Output

1 2 3 4 5

Here is how the program works.

Iteration Variable i <= 5 Action

1st i = 1 true 1 is printed and i is increased to 2 .

2nd i = 2 true 2 is printed and i is increased to 3 .

3rd i = 3 true 3 is printed and i is increased to 4

4th i = 4 true 4 is printed and i is increased to 5 .

5th i = 5 true 5 is printed and i is increased to 6 .

6th i = 6 false The loop is terminated

Example 2: Sum of Positive Numbers Only


// program to find the sum of positive numbers
// if the user enters a negative number, the loop ends
// the negative number entered is not added to the sum

#include <iostream>
using namespace std;

int main() {
int number;
int sum = 0;

// take input from the user


cout << "Enter a number: ";
cin >> number;

while (number >= 0) {


// add all positive numbers
sum += number;

// take input again if the number is positive


cout << "Enter a number: ";
cin >> number;
}
7
// display the sum
cout << "\nThe sum is " << sum << endl;

return 0;
}
Run Code

Output

Enter a number: 6
Enter a number: 12
Enter a number: 7
Enter a number: 0
Enter a number: -2

The sum is 25

3) C++ 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.
Its syntax is:

do {
// body of loop;
}
while (condition);

Here,

• The body of the loop is executed at first. Then the condition is evaluated.
• If the condition evaluates to true , the body of the loop inside the do statement is
executed again.
• The condition is evaluated once again.
• If the condition evaluates to true , the body of the loop inside the do statement is
executed again.
• This process continues until the condition evaluates to false . Then the loop stops.

8
Flowchart of do...while Loop

Flowchart of C++ do...while loop


Example 3: Display Numbers from 1 to 5
// C++ Program to print numbers from 1 to 5

#include <iostream>

using namespace std;

int main() {
int i = 1;

// do...while loop from 1 to 5


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

return 0;
}
Run Code

9
Output

1 2 3 4 5

Here is how the program works.

Iteration Variable i <= 5 Action

i = 1 not checked 1 is printed and i is increased to 2

1st i = 2 true 2 is printed and i is increased to 3

2nd i = 3 true 3 is printed and i is increased to 4

3rd i = 4 true 4 is printed and i is increased to 5

4th i = 5 true 5 is printed and i is increased to 6

5th i = 6 false The loop is terminated

Example 4: Sum of Positive Numbers Only


// program to find the sum of positive numbers
// If the user enters a negative number, the loop ends
// the negative number entered is not added to the sum

#include <iostream>
using namespace std;

int main() {
int number = 0;
int sum = 0;

do {
sum += number;

// take input from the user


cout << "Enter a number: ";
cin >> number;
}
while (number >= 0);

// display the sum


cout << "\nThe sum is " << sum << endl;

return 0;
}
10
Run Code

Output 1

Enter a number: 6
Enter a number: 12
Enter a number: 7
Enter a number: 0
Enter a number: -2

The sum is 25

Here, the do...while loop continues until the user enters a negative number. When
the number is negative, the loop terminates; the negative number is not added to
the sum variable.
Output 2

Enter a number: -6
The sum is 0.

The body of the do...while loop runs only once if the user enters a negative
number.

Infinite while loop


If the condition of a loop is always true , the loop runs for infinite times (until the
memory is full). For example,

// infinite while loop


while(true) {
// body of the loop
}

Here is an example of an infinite do...while loop.

// infinite do...while loop

int count = 1;

do {
// body of loop
}
while(count == 1);

11
In the above programs, the condition is always true . Hence, the loop body will run
for infinite times.
for vs while loops
A for loop is usually used when the number of iterations is known. For example,

// This loop is iterated 5 times


for (int i = 1; i <=5; ++i) {
// body of the loop
}

Here, we know that the for-loop will be executed 5 times.

However, while and do...while loops are usually used when the number of
iterations is unknown. For example,

while (condition) {
// body of the loop
}

12

You might also like