C++/Language/While

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Make the condition of the while loop always true

<source lang="cpp">

  1. include <iostream>

using namespace std; int main(void) {

  int num;
  char choice;
  bool quit = false;
  while (true)
  {
     cout << "Enter a positive number: ";
     cin >> num;
     if (num > 0)
        break;
     else
     {
        cout << "Number must be positive; try again (Y/N): ";
        cin >> choice;
        if (choice != "Y")
        {
           quit = true;
           break;
        }
     }
  }
  if (quit == false)
     cout << "The number you entered is " << num << " ";
  else
     cout << "You did not enter a positive number";
  return 0;

}


      </source>


Nesting While Loops

<source lang="cpp">

  1. include <iostream>

using namespace std; int main(void) {

  int x = 0;
  while (x++ < 5)
  {
     int y = 0;
     while (y++ < 5)
        cout << "X";
     cout << "\n";
  }
  return 0;

}


      </source>


Update of the value of num was done within the body of the loop

<source lang="cpp">

  1. include <iostream>

using namespace std; int main(void) {

  int num = 0;
  while (num++ < 10)
     cout << num << " ";
  return 0;

}

      </source>


Use the break keyword to provide the user with the option of quitting the data entry

<source lang="cpp">

  1. include <iostream>

using namespace std; int main(void) {

  int num;
  char choice;
  cout << "Enter a positive number: ";
  cin >> num;
  while (num <= 0)
  {
     cout << "Number must be positive; try again (Y/N): ";
     cin >> choice;
     if (choice == "Y")
     {
        cout << "Enter number: ";
        cin >> num;
     }
     else
        break;
  }
  cout << "The number you entered is " << num << " ";
  return 0;

}

      </source>


While loop Demo

<source lang="cpp">

  1. include <iostream>

using namespace std; int main(void) {

  int num;
  cout << "Enter a positive number: ";
  cin >> num;
  while (num <= 0)
  {
     cout << "Positive number: ";
     cin >> num;
  }
  cout << "The number you entered is " << num << " ";
  return 0;

}

      </source>


While loop: outputs the numbers between 1 and 10

<source lang="cpp">

  1. include <iostream>

using namespace std; int main(void) {

  int num = 1;
  while (num <= 10)
  {
     cout << num << " ";
     num++;
  }
  return 0;

}

      </source>


While loop: two conditions

<source lang="cpp">

  1. include <iostream>

using namespace std; int main(void) {

  int num;
  char choice;
  bool quit = false;
  cout << "Enter a positive number: ";
  cin >> num;
  while (num <= 0 && quit == false)
  {
     cout << "Number must be positive; try again (Y/N): ";
     cin >> choice;
     if (choice != "Y")
     {
        cout << "Enter number: ";
        cin >> num;
     }
     else
        quit = true;
  }
  if (quit == false)
     cout << "The number you entered is " << num << " ";
  else
     cout << "You did not enter a positive number";
  return 0;

}


      </source>


While statement

<source lang="cpp">

  1. include <iostream>

using namespace std; int main() {

  int x, count = 0;
  float sum = 0.0;
  cout << "Please enter some integers: " << endl;
  while( cin >> x ){
     sum += x;
     ++count;
  }
  cout << "The average of the numbers: " << sum / count << endl;
  return 0;

}

      </source>