AppDividend
Latest Code Tutorials

C++ For loop: Complete Guide

There are three types of loops: for loop, while loop, and do-while loop. In this tutorial, we will learn about for loop.

To understand for loop in C++, we must have prior knowledge of loops in C++. Loops are used when we want a particular piece of code to run multiple times. We use loops to execute the statement of codes repeatedly until a specific condition is satisfied. It eases the work of the programmer and also shortens the code length.

For loop in C++

A for loop in C++ is the repetition control structure generally used to write a code more efficiently, which is supposed to be executed a specific number of times.

For example, if we want to print numbers from 1 to 1000, then if we don’t use loops, we have to write 1000 different print statements for printing numbers from 1 to 1000. With the help of loops, we can write this code in 2 lines. But, first, we need to run the loop and give iteration conditions.

How for loop works in C++

  1. An initialization statement is executed only once at the beginning.
  2. Then, the test expression is evaluated.
  3. If a test expression is false, then for loop is terminated. But if a test expression is true, codes inside the body of for loop are executed, and the update expression is updated.
  4. A test expression is evaluated, and this process repeats until the test expression is false.

Syntax of for loop

for(initialization; condition; increment/decrement)
{
	//statements
}

See the following example.

for(int i=1;i<=100;i++)
{
	cout<<i<<endl;
}

Explanation: The above code will print numbers from 1 to 100.

Flow chart of for loop

For loop in C++ Program

Parameters of for loop

for(initialization; condition; increment/decrement)
{
	// Statements
}

The initialization step is executed to initialize the program counter; it is done once only. The program counter is also known as loop control variables.

The next parameter is the condition where the condition is checked for which the loop is supposed to run.

After that, there is an increment/decrement counter which increases or decreases the counter once the statements are executed.

Example program of for loop in C++

Q1- Write a program to show the mechanism of the for loop.

#include<iostream>
using namespace std;
int main()
{
	cout<<"We will print numbers from 1 to 20 using for loop"<<endl;
	for(int i=1;i<=20;i++)
	{
		cout<<"The number is:"<<i<<endl;
	}
	return 0;
	
}

See the output.

Example program of for loop in C++

Q2-Write a program to print all the even numbers from 1 to 20 using for loop.

#include<iostream>
using namespace std;
int main()
{
	cout<<"We will print even numbers from 1 to 100 using for loop"<<endl;
	for(int i=2;i<=100;i=i+2)
	{
		cout<<"The even number is:"<<i<<endl;
	}
	return 0;
	
}

See the output.

C++ For Loop

Q3- Write a program to print all the odd numbers from 1 to 100 using for loop.

#include<iostream>
using namespace std;
int main()
{
	cout<<"We will print odd numbers from 1 to 100 using for loop"<<endl;
	for(int i=1;i<=100;i=i+2)
	{
		cout<<"The odd number is:"<<i<<endl;
	}
	return 0;
	
}

See the output.

C++ For Loop Example

Infinite for loop in C++

The loop is infinite when it executes repeatedly and never stops. It usually happens by mistake. When you set a condition in for loop so that it never returns false, it becomes the infinite loop.

See the following example.

#include <iostream>
using namespace std;
int main(){
   for(int i=1; i>=1; i++){
      cout<<"Value of variable i is: "<<i<<endl;
   }
   return 0;
}

It is an infinite loop as we increment a value of i, so it would always satisfy the condition i>=1; the condition would never return false.

Here is another example of an infinite loop.

// infinite loop
for ( ; ; ) {
   // statement(s)
}

Display elements of an array using for loop

See the following program that displays items of an array using for loop.

#include <iostream>
using namespace std;
int main(){
   int arr[]={21,9,56,99, 202};
   /* We have set the value of variable i
    * to 0 as the array index starts with 0
    * which means the first element of array 
    * starts with zero index.
    */
   for(int i=0; i<5; i++){
      cout<<arr[i]<<endl;
   }
   return 0;
}

See the following output.

21
9
56
99
202

That’s it for this tutorial.

Recommended Posts

If-else Statements in C++

C++ Recursion

C++ Inline Functions

Access Specifiers in C++

Scope Resolution Operator in C++

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.