Advertisement

For Loop , While Loop & Do-while Loop || Code with abuzar

 

 For Loop 

 Loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of Times.

Syntax :

for ( init ; condition ; increment ) {

statements(s);

}

Explain  :

The init step is excited first , and only once . This step allows you to declare and initialize any loop any loop control variables . you are not required to put a statements here , as a long as a semicolon.

Next , the Condition is evaluated . if it is true , the body of the loop is executed . if it is false , the body of the loop does not execute and flow of control jump to the next statement.

After the body of the loop executed , the flow of control jumps back up the increment statements . This statements can be left blank , as long as a semicolon appears after the condition

The condition is now evaluated again . if it is true , the loop executes and the process repeat itself ( body of the loop , then increment step , and then again condition ). After the condition becomes false , the for loop terminates .

Example :

#include <iostream>
using namespace std;
int main() {
for (int i = 1i <= 5; ++i){
 cout << i << " ";
 }
  return 0;
}

Output :

1 2 3 4 5
While- Loop 

In this loop , first the condition is evaluated first and if return true then the statements inside while loop executed , this happen repeatedly until the condition return false . When condition return false , the control comes out of the loop and jumps to the next statements in the program after while loop .


Note : The important point in while-loop we use the Increment and Decrement operator statements inside the loop so that the loop variables gets changed on each iteration , and at some points condition return false.

// C++ Program to print numbers from 1 to 5

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

 // while loop from 1 to 5
 while (i <= 5){
  cout << i << " ";
  ++i;
 }
 return 0;
}


Output :

1 2 3 4 5


Infinite while Loop 

The loop which is never  stop is said that to be infinite while loop , when we give a condition which is never return false , then the loop become infinite and repeat itself.

#include <iostream>
using namespace std;
int main(){
int i=1while(i<=6){
 cout<<"Value of variable i is: "<<i<<endli--;
}
}
Do-While- Loop 

Do-while is used to iterate a part of the program several times . if the number of iteration is not fixed and you and you must have to execute the loop at least once , it is recommended to use do-while loop.

The C++ do-while executed at least once because condition is checked after loop body 

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

 //do...while loop from 1 to 5
 do {
 cout << i << " ";
  ++i;
  }
 while (i <= 5);
  return 0;
}

Output :

1 2 3 4 5
 For Loop , While Loop , Do-while Loop : Click Here

 This is my YouTube channel : Click Here
  
 Playlist of C++(Full Course) : Click Here

 Playlist of C++(Program ) : Click Here

 
 Please ! write comment if you find anything incorrect in this website .

 This article is contributed by Mohd abuzar . If you want to like code with abuzar or want to contribute with us so mail at - abuzar.abuzar.mohd325@gmail.com


Post a Comment

0 Comments