以下是C ++代码,可能很重要!
例如,我的意思是,说我有:
bool this = true;
if(this)
{
this = false;
//other code here
}
当“this”变为false时,这是否意味着条件中的语句将不再执行,因为不再满足条件,或者“//其他代码”的其余部分是否仍然执行?我一直小心不要改变条件的状态,直到我完成了我需要做的任何条件,但我很好奇,如果这是必要的。谢谢你的时间。
答案 0 :(得分:2)
不,语句中的条件在if(this)
[1]的位置进行测试 - 之后,您可以随意更改值,代码将继续。
请注意,这个概念也适用于while(condition) { ... }
- condition
仅在循环开始时被清除,因此代码从那里开始顺序继续,直到它返回到开头再次循环。
这是一种相当常见的模式:
need_print_heading = true;
lines = 0;
while(more_data)
{
if (need_print_heading)
{
need_print_heading = false;
print_heading();
}
print_data();
linex++;
if (lines > 50) need_print_heading = true;
}
[1]在您显示的代码中使用this
意味着它是无效的C ++,因为this
是C ++中的保留字。您需要更改名称以使此代码编译。