C++ Tutorial/Operators statements/do while

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

An improved Help system that uses a do-while to process a menu selection

#include <iostream> 
using namespace std; 
 
int main() { 
  char choice; 
 
  do { 
    cout << "Help on:\n"; 
    cout << "  1. if\n"; 
    cout << "  2. switch\n"; 
    cout << "  3. for\n"; 
    cout << "  4. while\n"; 
    cout << "  5. do-while\n"; 
    cout << "Choose one: "; 
 
    cin >> choice; 
 
  } while( choice < "1" || choice > "5"); 
 
  cout << "\n\n"; 
  
  switch(choice) { 
    case "1": 
      cout << "The if:\n\n"; 
      break; 
    case "2": 
      cout << "The switch:\n\n"; 
      break; 
    case "3": 
      cout << "The for:\n\n"; 
      break; 
    case "4": 
      cout << "The while:\n\n"; 
      break; 
    case "5": 
      cout << "The do-while:\n\n"; 
      break; 
  } 
  return 0; 
}
Help on:
  1. if
  2. switch
  3. for
  4. while
  5. do-while
Choose one: 2

The switch:

do while loop with int counter

#include <iostream>
 
 int main()
 {
     int counter;
     std::cout << "How many hellos? ";
     std::cin >> counter;
     do
     {
         std::cout << "Hello\n";
         counter--;
     } while (counter >0 );
     std::cout << "counter is: " << counter << std::endl;
     return 0;
 }
How many hellos? 5
Hello
Hello
Hello
Hello
Hello
counter is: 0

Nested if statement in a do while loop

#include <iostream> 
#include <cstdlib> 
using namespace std; 
 
int main() 
{ 
  int magic; 
  int guess;
 
  magic = rand(); // get a random number 
   
  do { 
    cout << "Enter your guess: "; 
    cin >> guess; 
    if(guess == magic) { 
      cout << "Right"; 
      cout << magic << " is the magic number.\n"; 
    } 
    else { 
      cout << "...Sorry, you"re wrong."; 
      if(guess > magic) 
         cout << " Your guess is too high.\n"; 
      else cout << " Your guess is too low.\n"; 
    } 
  } while(guess != magic); 
 
  return 0; 
}
Enter your guess: 3
...Sorry, you"re wrong. Your guess is too low.
Enter your guess: 2
...Sorry, you"re wrong. Your guess is too low.
Enter your guess: 3
...Sorry, you"re wrong. Your guess is too low.
Enter your guess: 4
...Sorry, you"re wrong. Your guess is too low.
Enter your guess: 7
...Sorry, you"re wrong. Your guess is too low.
Enter your guess: 8
...Sorry, you"re wrong. Your guess is too low.
Enter your guess: 12
...Sorry, you"re wrong. Your guess is too low.
Enter your guess: 30
...Sorry, you"re wrong. Your guess is too low.
Enter your guess: 500
...Sorry, you"re wrong. Your guess is too high.
Enter your guess: 250
...Sorry, you"re wrong. Your guess is too high.
Enter your guess: 125
...Sorry, you"re wrong. Your guess is too high.
Enter your guess: 60
...Sorry, you"re wrong. Your guess is too high.
Enter your guess: 30
...Sorry, you"re wrong. Your guess is too low.
Enter your guess: 45
...Sorry, you"re wrong. Your guess is too high.
Enter your guess: 40
...Sorry, you"re wrong. Your guess is too low.
Enter your guess: 43
...Sorry, you"re wrong. Your guess is too high.
Enter your guess: 42
...Sorry, you"re wrong. Your guess is too high.
Enter your guess: 41
Right41 is the magic number.

Use do while loop to read a number

#include <iostream> 
using namespace std; 
 
int main() 
{ 
  int num; 
 
  do { 
    cout << "Enter a number (100 to stop): "; 
    cin >> num; 
  } while(num != 100); 
 
  return 0; 
}
Enter a number (100 to stop): 1
Enter a number (100 to stop): 2
Enter a number (100 to stop): 3
Enter a number (100 to stop): 100