Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

for loop in C++ programming language

Posted on July 20, 2016January 29, 2020

Table of Contents

  • for loop in C++ programming language
    • The syntax of for loop
    • Flow diagram
    • How while loop works
      • Related Articles

for loop in C++ programming language

In this tutorial, we will discuss the for loop in C++ programming language

In the programming language, looping statements are used to repeat a particular block of code.

In the C++ programming language, “for statement” is used to repeat a block of code until the particular condition is satisfied.

 

In the programing language, Generally , loops are used to repeat a specific group of code until some condition is satisfied

 

There are three types of loops used in C++ language

  • for loop
  • while loop
  • do-while loop

 

here, we are going to learn how to use the for loop in C++ language

 

The syntax of for loop

for(init-expression; condition-expression; update-expresion;)

{
    //statement(s);}

 

Flow diagram

for loop C++ programming language
Flow diagram
For loop, flow chat in C++

How while loop works

  • Firstly, the test expression evaluates the test expression only once
  • When the test expression is true. statements inside the body of while loop is executed.
  • this process happening on until the test expression is false
  • When the test expression is false, The control exits from the loop body and while-loop is terminated
program 1
 

#include <iostream>

using namespace std;

int main()
{
int counter;
for(counter=1; counter<=10; counter++)
{
cout <<counter<< “—> Hello this is for loop” << endl;
}

return 0;
}

When the above code is compiled and executed, it produces the following result:

program 2

#include <iostream>

using namespace std;

int main()
{
for(int i=0; i<=2; i++)
{
cout<<i<<endl;
}
// Output: 0

int i;

for(i=0; i<=2; i++) {

cout<<i<<endl;

}

}

 

 

When the above code is compiled and executed, it produces the following result:



program 3

using namespace std;

int main()
{
int i;  //define a loop variable
for(i=0; i<=5; i++)  //starting value. condition 0 to 5. increment
{
cout << i <<” “; //print var i and space

//cout<<endl; //print new line

}

return 0;
}

When the above code is compiled and executed, it produces the following result:

program 4

This program used ton print – 5 number serious

using namespace std;

int main()
{
int i;  //define a loop variable
for(i=0; i<=5; i++) //starting value. condition 0 to 5. increment

{
cout << i <<” “; //print var i and space
cout<<endl; //print new line
}

return 0;
}

 

When the above code is compiled and executed, it produces the following result:

 

 
 

 

Program 5

This program used to helps to print square numbers
#include <iostream>
using namespace std;
int main()
{
int j;//variable initialization
for(j=1; j<=12; j++) // for loop
    cout << j*j<<” “; //display statements
    return 0;
}

 


When the above code is compiled and executed, it produces the following result:

 

 


Related Articles

 
While Loop in Java      while Loop in C++     While Loop In C

Do-while Loop in Java    Do-while Loop C++     Do-while loop in C 

For statements in C++  For statements in C  For statements in java

If condition C++       If condition in Java   If condition in C

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes