c++ - 新手简单问题:while的跳出条件
高洛峰
高洛峰 2017-04-17 14:24:53
0
2
546
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(2)
洪涛

This code will keep looping to test whether the input stream is normal until the input stream reaches the end of the file, or an error occurs in the input stream.

  1. Shouldn’t the expression in while brackets result in a bool type? Why can it be an input statement?

    while括号中的表达式的确是bool类型,所以你可以纠结一下 `cin >> n`这个表达式的返回值到底是什么(`>>`正常读取,返回的是cin的地址值;读入错误时,则返回0)
  2. Q: Then how does it jump out? Is it possible to break out of the loop only when the input number is 0?

    跳出循环的两种输入方式:
    windows下 `ctrl+z`
    linux下 `ctrl+d`
    

Yeah. . . . In fact, I don't know much about it, but what I am more troubled about is what you said "can't find". If you really can't find the answer, I want to share my experience.
First of all, for a senior novice like me, I have always firmly believed that there are only unimaginable novice questions and no answers that cannot be found.
Then, you can check whatever you want. For example, for this question, you can ferry it around while(cin>>n) and you will find a bunch of answers you want (eg. when can you break out of the loop);
Then, if you are not only obsessed with when to stop, but also consider the expression value, you can check the return values ​​​​of while and cin >> n to see what secrets are hidden.
It’s easy for beginners to do it, but it’s obviously not as interesting as tracing back a problem layer by layer by yourself (this is to encourage myself XD)

Another great answer to share: cin, cout, and their return values

阿神


Correct answer upstairs

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!