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

Home
C++ Tutorial
1.Language Basics
2.Data Types
3.Operators statements
4.Array
5.Development
6.Exceptions
7.Function
8.Structure
9.Class
10.Operator Overloading
11.Pointer
12.File Stream
13.template
14.STL Introduction
15.string
16.vector
17.list
18.bitset
19.set multiset
20.valarray
21.queue stack
22.deque
23.map multimap
24.STL Algorithms Modifying sequence operations
25.STL Algorithms Non modifying sequence operations
26.STL Algorithms Binary search
27.STL Algorithms Sorting
28.STL Algorithms Merge
29.STL Algorithms Min Max
30.STL Algorithms Iterator
31.STL Algorithms Heap
32.STL Algorithms Helper
C / ANSI-C
C Tutorial
C++
Visual C++ .NET
C++ Tutorial » Operators statements » do while 
3.17.3.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.
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.