C++ Loops

loops are important part of programming language. almost all programming language support loop concept. loop are used to executes group of given instruction and statement under certain situations. loop are reliable when need to perform same type operation in execution of multiple times.

Type of loops

Three type of loop are supported in c++ programming language

for loop

for(initialization;condition_expression;update_expression) statements;
Key Points Overview
for keyword
Initialization Initialization are used to set initial value of loop control variables. in this section are possible to declare variables and assign initial values.
Condition expression Expression to execute the loop
Update expression In this section we are modified the value of variable those are control the execution of loop. Mostly of case in use to (increment and decrements) operation
statements That is an optional to defined the body of loop. but most of case there are used to defined the contain of group of statements
For example

Executed for loop in 10 times.

//for loop example in c++
#include<iostream>
using namespace std;
int main(){
  /*
    initilization : index=0
    condition expression : index<10
    update expression: index++
  */
  for(int index=0;index<10;index++){
    cout<<index<<endl; //display index value
  }
  return 0;
}
Output
0
1
2
3
4
5
6
7
8
9

while loop

while loop are first checking of given expression. If expression are valid then executes the loop statement. This process are executed when terminated condition are not satisfied.

while(Expression){
//loop statements (body of loop)
//update Expression
}

/* while :keyword
  expression: test condition
  loop statements: actual code
*/
Key Points Overview
while keyword
Expression Expression to execute the loop
Update expression In this section we are modified the value of variable those are control the execution of loop.
Statements body of loop.
For example
//while loop example in c++
#include<iostream>
using namespace std;
int main(){

  int index=1;
  /*
  Expression : index<=10
  update Expression : index++
  */
  while(index<=10){
    //statements
    cout<<4*index<<endl;
    index++; //increment index by 1
  }
  return 0;
}
Output
4
8
12
16
20
24
28
32
36
40

do while loop

Do while loops are first execute given instructions and statements. After that checking are its terminating condition of this loop. This are provide a guarantee to execute loop at least one time.

do{
//statements
//Update Expression
}while(Expression);
Key Points Overview
do , while keyword
Expression Expression to execute the do-while loop
Update Expression In this section we are modified the value of variable those are control the execution of loop.
Statements Body of loop.
//while loop example in c++
#include<iostream>
using namespace std;
int main(){

  int index=1;
  /*
  Expression : index<=10
  update Expression : index++
  */
  do{
    //statements
    cout<<index<<endl;
    index++; //increment index by 1
  }while(index<=5);
  return 0;
}
Output
1
2
3
4
5

Loop Statements (continue,break)

There are two type of control statement are supporting of c++ programming.

continue statement are used to change execution follow of loop. This statement are used in inside a loop in specified condition. When continue statement are executed they are not execute next remaining statement of loop body. and control are jumping of beginning of the loop. note that this statement are used only inside a loop. they are used (for ,while and do while loop)

Example of continue statements

#include<iostream>
using namespace std;
int main(){

  for(int i=1;i<=10;i++){
    if(i%2==0){
      continue;//statements
    }
    //display i value
    cout<<i<<endl;
  }
  return 0;
}
Output
1
3
5
7
9

example in while loop

//continue within while loop
#include<iostream>
using namespace std;
int main(){
 int i=1;
  while(i++ <=10){
    if(i%2!=0){
      continue;//statements
    }
    //display i value
    cout<<i<<endl;
  }
  return 0;
}
Output
2
4
6
8
10

example of continue statement within the (do-while) loop.

//continue within do-while loop
#include<iostream>
using namespace std;
int main(){
 int i=0;
  do{
    i++;
    if(i==3){
      //statement
      continue;
    }
    cout<<i<<endl;
  }while(i<=4);
  return 0;
}
Output
1
2
4
5

break are used to terminate execution of given loop. This are also normally used in switch statement. sometime special case we are need to terminate loop before the given terminating condition. so break are useful to execute loop.

//break statement example in c++
#include<iostream>
using namespace std;
int main(){
  int i=0;
 for(i=0;i<=10;i++){
  if(i==4){
    break; //break for loop
  }
  cout<<" for i : "<<i<<endl;
 }

 while(i<=10){
  i++;
  if(i==8){
    break; //break while loop
  }
  cout<<"while i :"<<i<<endl;
 }

 do{
  i++;
  if(i==12){
    break; //break do while loop
  }
  cout<<"do while i :"<<i<<endl;
 }while(i<=15);

  return 0;
}
Output
 for i : 0
 for i : 1
 for i : 2
 for i : 3
while i :5
while i :6
while i :7
do while i :9
do while i :10
do while i :11

Nested Loops

loop within loop it is called nested of loop. syntax nested for loops.

//nested for loops
  for(initialization;condition_expression;update_expression) {
    //code
    for(initialization;condition_expression;update_expression) {
      //  : statements 
      //  : another nested loop
    }
    //code
  }  

Syntax of nested while loops.

while(Expression1){
  //loop statements (body of loop)
  while(Expression2){
  //loop statements (body of loop)
  //update Expression
    //another nested loop
  }
  //update Expression
}

Syntax of nested do-while loops.

do{
//statements 
  do{
  //statements
  //Update Expression
  //nested loop
  }while(Expression2);
//Update Expression
}while(Expression1);

There is an possible to use any number of nested loop in c++ programming. and there are also possible to used one loop inside another loop or same type of another loop in any sequence.

Infinite loops

There are possible to execute infinite in programming language. there is an certain situations are need to terminate the loop. if there are not valid then loop are run continuously (infinitely). Here given few condition.

while(true){
  //no break condition
}
do{
  //no break
}while(1);

for(;;){
  //no break statement
  //infinite loop
}

try to give valid condition to terminate the loops.

© 2019, regularcodes.com, All rights reserved