c++ - 为什么我们可以在 if 中使用分号,但在 while 循环中却没有
问题描述
为什么我可以这样做:
if (int result=getValue(); result > 100) {
}
但不能这样做:
while (int result=getValue(); result > 100) {
}
为什么要歧视while
?条件就是条件。为什么while
不能像if
可以一样评估它?
为了实现所需的行为while
,我必须以这种方式实现它:
int result = getValue();
while (result > 100) {
//do something
result = getValue();
}
解决方案
因为我们已经有了一个带有初始化器的while循环。它的拼写是:
for (int result=getValue(); result > 100;) {
}
推荐阅读
- opencart-3 - 删除描述首页产品框
- django - 重定向到 AJAX 中的根 url
- javascript - 单击文件时如何调出下载按钮?
- go - 如何在 gif.Encode() 中使用 golang palette.WebSafe?
- r - 如何从用户编写的函数中正确返回 SpatialPolygonDataFrame?
- vue.js - vue.js 1 个标签中的 3 个标签
- c# - 如何让重载的 == 运算符与 LINQ 和 EF Core 一起使用?
- sql - 选择数据,使其仅返回每个 ID 的最新行
- wordpress - 投资组合页面自动重定向
- erlang - Phoenix:无法编译依赖项:牧场