C++编程思想 第1卷 第3章 do while语句

do-while语句和while语句区别, do-while不管条件真假会执行一次,
while条件为假就可以一次都不执行

cin可以初始化变量


//: C03:Guess2.cpp
// From Thinking in C++, 2nd Edition
// Available at http://www.BruceEckel.com
// (c) Bruce Eckel 2000
// Copyright notice in Copyright.txt
// The guess program using do-while
#include <iostream>
using namespace std;

int main() {
  int secret = 15;
  int guess; // No initialization needed here
  do {
    cout << "guess the number: ";
    cin >> guess; // Initialization happens
  }   while(guess != secret);
  cout << "You got it!" << endl;
  fflush(stdin);
  getchar();
} ///:~

输出
只有输到15 就输出 You got it!然后退出


猜你喜欢

转载自blog.csdn.net/eyetired/article/details/80574957