for loop

Loops in Cpp programming language

Loops in Cpp programming language

In this tutorial, we will discuss Loops in the cpp programming language

In some cases, if you want to execute a block of the statement multiple times according to a condition, such case, loops are used for performing this task.

a loop statement in programming languages is a very important feature which helps to execute a block of code (continuous instruction or task) repeatedly while some conditions and evaluate until becomes false.

 

Loops in Cpp programming language

C++ language contains some loops which are functioning similarly. it used performing some continuous task until the given condition is fulfilled.

We will discuss in this part, all Loops in Cpp programming language

 

1) While loop -A “while loop” is a Loop statement. It allows the code( flow of control ) to be executed repeatedly to perform based on the given Boolean expression for a certain number of times.

Syntax

initilaization;
while(boolean_Expression)
{
//loop body
looping statements

increment/decrement statements
}

 

Flowchart

The flow of control in while loop

First the “while loop” starts with the initialization of statement, then it checks the Boolean expression. If it is evaluated as true then the loop body of statements are executed. If it is evaluated as false, the loop control exits from the loop body and moves to the end of the loop.

When the loop is evaluated true. the body of statements are executed and the loop control goes to updating statements and then, test boolean expression is evaluated again to executed the body of the statement.

This process is followed until the boolean expression returns false.

Example

//Java program to illustrate while loop
class Demo_While{
public static void main(String args[]){
int i=0;//declare variable "i" and initialize  as zero
while(i<=5){
System.out.println("Value of i: "+i);
//this statement execute until this condition(become greater than 5) performed
i++;
//increment the value of i for next ikteration
}
}

}

When the above code is executed, it produces the following result

value of i=0
value of i=1
value of i=2
value of i=3
value of i=4
value of i=5

Know more about this

while loop in C++ language

Nested while loop in C++ language

 

2) for loop – The for loop is a loop statement in C++. It allows their code (flow of control) to be executed repeatedly to perform based given boolean expression for a certain number of times, as same as while loop but with some differences.

Although for loop and while loop both have the same flow of control, but the for loop structure distinguished with the while loop.

In the for loop, initialization statements, boolean expression, updating statements are defined as the same line

Syntax

for(initialization_statements; boolean_Expression; increment/decrement){
 //loop body looping statements 
}


Flowchart

Loop control in do-while loop

First, the “for loop” starts with the initialization statement. and then checks the boolean-expression. If it is evaluated as true then the loop body of statements are executed or If it evaluated as the false loop control exits from the loop body and goes to the end of the loop.

When the loop is evaluated true. the body of statements is executed, then the loop control goes for updating statements and test boolean expression is evaluated again to executed the body of the statement.

This process follows until the boolean expression returns false.

Example

//Java program to illustrate for loop
class Demo_For1{
public static void main(String args[]){
int i;//declare variable "i";
for(i=0; i<=5; i++){
System.out.println("Value of i: "+i);
//this statement execute until this condition(become greater than 5) performed
//increment the value of i for next ikteration
}
}

}

When the above code is executed, it produces the following result

value of i=0
value of i=1
value of i=2
value of i=3
value of i=4
value of i=5

 

Know more about this

for loop in C++ language

Nested for loop in C++ language

 

3) do-while loop – The do while loop is similar to while loop with the only difference that it starts with the execution of statements before the test condition is checked. If the test expression returns false as the first time, the statements execute only once

do while is a loop statement in C++, it allows their loop control access repeatedly to perform based given boolean expression as same as while loop but with some differences. The do-while loop structure distinguishes with the while loop structure.

In the do -while loop, initially, statements are executed only once. Then it starts to evaluate test condition and then flow of control moves to updating-statements. then it (flow of control )begins to work as a normal while loop.

Syntax

for(initialization_statements; boolean_Expression; increment/decrement){
 //loop body looping statements 
}


Flowchart

Java programming Loop control do-while loop

Example

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

int main()
{
    int i=1;
    do{
       cout << i << endl;
       i++;
    }while(i<=10);
getch();
    return 0;
}

When the above code is executed, it produces the following result

1
2
3
4
5
6
7
8
9
10

 

 

Know more about this

do-while loop in C++ language

Nested do-while loop in C++ language

 

Suggested for you

Loops in C language

Loops in Java language

Loops in C programming language
Java program to Integrated triangle patterns using for loop
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…

2 months 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…

10 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,…

10 months ago