C++ while Loop

A while loop is a simple loop that will run the same code over and over as long as a given conditional is true. The condition is checked at the beginning of each run through the loop ( including the first one ). If the conditional is false for the beginning, the while loop will be skipped all together.

Flow of while Loop in C++ by Flow Diagram


C++ while Loop Flowchart Diagram
C++ while Loop Flowchart Diagram


Syntax: 

    while ( conditional ) {
        // loop body
    }

Example:


#include <iostream>
using namespace std;
 int main () {
   // while loop example
   int i=0;
  while(i < 5)
  {
      cout << "Apple: " << i << endl; // output
      i++; // increment counter
   }
   return 0;


}

Output:


<< C++ for Loop            --         C++ do-while Loop >> 

No comments:

Post a Comment