Nested if statement in a do while loop : do while « Operators statements « C++ Tutorial






#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.








3.17.do while
3.17.1.do while loop with int counter
3.17.2.Use do while loop to read a number
3.17.3.Nested if statement in a do while loop
3.17.4.An improved Help system that uses a do-while to process a menu selection