Loop

for loop in Cpp programming language

for loop in Cpp programming language

In this tutorial, we will discuss for loop in Cpp programming language

In the C++ language, we can see three types of basic looping statements

For loop in C++

for loop in Cpp programming language

In the C++ language, for loop is used to executes and evaluates a set of c++ code repeatedly until the test expression is false. The for loop is executed when the test expression evaluated and the condition is satisfied.

for loop consist of three statements which typically use:

  • initialization statements
  • Test expression – Boolean condition(result may be true or false)
  • Updating statements(increment or decrement statements)

Declaration

Syntax

for(initialization; test_expression;  updating statements)
{
statement(s)
//here we can put suitable statements
}

Example

for(int x=0; x<=10; x++)
{
cout<<x;
}

Output

0 to 10

//parenthesis () – used to control the order of operations in an expression

//Curly braces {} – Braces are used to group the statements in the loop

Initiation – (Variable initialization eg – int  i=0;)

First, execution of the flow of control always originates from the initialization statement. The initialization statement is executed only once. Initialization is an entry point of the for loop and the initiation ends with a semicolon(;).

condition – (i<=10;)

Next, the for loop moves to the conditional statement for test condition. The condition is evaluated and if it is true, the statements in the body part of for loop are executed but, if the condition is false body of the loop does not execute.

updating statement – variable increment or decrement (i++ or i–/ i=i+1 or i=i-1)

Finally, the loop goes to part of updating statements and it allows you to update any loop control variables until the condition becomes false.

For loop flow diagram

For loop flow diagram in Java

The loop begins the initialization parts

Then the test expression is evaluated

When the Boolean expression is true, the loop executes and updates statement.

finally, when the Boolean expression is false, the loop exits from the loop.

Example

Print number 1 to 5

Program 1

#include <iostream>
#include <conio.h>

using namespace std;

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

 

When the above code is executed, it produces the following results:

1
2
3
4
5

 

Calculate the total numbers of 1 to 1000

Program 2

#include <iostream>
#include <conio.h>
//find total of 1+2+3+4+....+1000
using namespace std;

int main()
{
    int sum=0;//variable declaration
   for(int i=1; i<=1000; i++)//for loop to sum 1 to 1000
   { sum+=i;//equal to sum=sum+1
   }
   cout <<"Total of 1 to 1000 is :"<<sum<< endl;
   getch();
    return 0;//End of program
}

 

When the above code is executed, it produces the following results:

Total of 1 to 1000 is :500500

Program 3

Calculate factorial of given numbers

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{     //calculate factorial of given number
    int n;
    long fact=1;//variable declaration
    cout<<"Enter the number for calculate factorial\n";

    cin>>n;
    for(int i=1; i<=n; i++){
        fact*=i;
    }
        cout<<"Factorialvalue of:"<< fact<<endl;

    getch();
    return 0;
}

When the above code is executed, it produces the following results:

Enter the number for calculate factorial
5
Factorial value of :120

 

Infinite for loop in C++

Program 1

#include <iostream>

using namespace std;

int main()
{
    for(;;)
        {
            cout << "This loop is running continuously" << endl;
    }

    return 0;
}

 

When the above code is executed, it produces the following results:

This loop is running continuously
This loop is running continuously
This loop is running continuously
This loop is running continuously
This loop is running continuously
This loop is running continuously
This loop is running continuously
This loop is running continuously
This loop is running continuously
This loop is running continuously
..............
..............
To stop execution ctrl+c

 

Program 2

#include <iostream>

using namespace std;

int main()
{
    for(int i=1; i>=1; i++)
    cout << "Value is :"<<i<< endl;
    return 0;
}

 

When the above code is executed, it produces the following results:

value is :  1
value is :  2
value is :  3
value is :  4
value is :  5
value is :  6
value is :  7
............
............
To stop the execution ctrl +c

 

Display elements of the array

We can use to display a single dimension array element using for loop of c++ language

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int age[]={34,56,64,58,97,86};
    for(int i=0; i<=5; i++){
    cout <<age[i]<< endl;
    }
    getch();
    return 0;
}

 

When the above code is executed, it produces the following results:

34
56
64
58
97
86

 

Find the sum of elements of the single dimensional array

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int Number[10],n,i,sum=0;
    cout << "Enter number of elements you want" << endl;
    cin>>n;
    for(int i=0; i<n; i++){
       cout << "Enter element " <<i+1<<":";
       cin>>Number[i];
    }
    for(int i=0; i<n; i++){
            sum+=Number[i];
}
    cout << "\nSum of Numbers are :" <<sum;
    cout << "\nAvg of Numbers are :" <<sum/n;
    getch();
    return 0;
}

When the above code is executed, it produces the following results:

Enter number of elements you want :
6
Enter element 1:45
Enter element 2:65
Enter element 3:43
Enter element 4:45
Enter element 5:67
Enter element 6:78

Sum of Numbers are :343
Avg of numbers are : 57

In te above program, we can find sum of 6 numbers entered by user using for loop in C++

Suggested for you

Nested for in Java language

Nested for in C language

Nested for in C++ language

Nested for in Python language

For loop in Java programming language
Nested for loop in Cpp programming language
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

1 month ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

9 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

9 months ago