Use a simple while loop with compound-statement syntax. - C++ Statement

C++ examples for Statement:while

Description

Use a simple while loop with compound-statement syntax.

Demo Code

#include <iostream>
using namespace std;
int main()//from   w w  w. j a  v a 2s  .  c o  m
{
   int  i = 0, n = 0;
   // Get number from the keyboard and initialize i.
   cout << "Enter a number and press ENTER: ";
   cin >> n;
   i = 1;
   while (i <= n) {  // While i less than or equal n,
      cout << i << " ";   //   Print i,
   i = i + 1;          //   Add 1 to i.
}
return 0;
}

Result


Related Tutorials