输出的差异取决于 for 循环中省略花括号 (c++)
Diffrientiation in outputs depending on the ommition of curly braces in for loops (c++)
我一直在按照本网站参考指南的建议阅读 c++ 入门书,我注意到作者省略了花括号,因为 loop.I 检查了其他网站,大括号应该放在在通常。放置大括号并省略 it.The 代码在下方时会有不同的输出
int sum = 0;
for (int val = 1; val <= 10; ++val)
sum += val;
std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
// This pair of code prints the std::cout once
for (int val = 50; val <=100;++val)
sum += val;
std::cout << "Sum of 50 to 100 inclusive is " << sum << std::endl;
// -------------------------------------------------------------------------
for (int val = 1; val <= 10; ++val) {
sum += val;
std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
}
// This pair of code prints the std::cout multiple times
for (int val = 50; val <=100;++val) {
sum += val;
std::cout << "Sum of 50 to 100 inclusive is " << sum << std::endl;
}
如果有人能解释输出的差异,我将不胜感激。提前致谢!
对于大括号,大括号中的所有内容都由 for 循环执行
for (...;...;...)
{
// I get executed in the for loop!
// I get executed in the for loop too!
}
// I don't get executed in the for loop!
然而,如果没有大括号,它只会直接执行后面的语句:
for (...;...;...)
// I get executed in the for loop!
// I don't get executed in the for loop!
// I don't get executed in the for loop either!
for语句具体定义如下
for ( for-init-statement conditionopt; expressionopt) statement
^^^^^^^^^
其中语句可以是任何语句,包括复合语句
compound-statement:
{ statement-seqopt}
因此在这个 for 语句的例子中
for (int val = 1; val <= 10; ++val)
sum += val;
声明是
sum += val;
而在这个 for 语句的例子中
for (int val = 1; val <= 10; ++val) {
sum += val;
std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
}
声明是
{
sum += val;
std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
}
我一直在按照本网站参考指南的建议阅读 c++ 入门书,我注意到作者省略了花括号,因为 loop.I 检查了其他网站,大括号应该放在在通常。放置大括号并省略 it.The 代码在下方时会有不同的输出
int sum = 0;
for (int val = 1; val <= 10; ++val)
sum += val;
std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
// This pair of code prints the std::cout once
for (int val = 50; val <=100;++val)
sum += val;
std::cout << "Sum of 50 to 100 inclusive is " << sum << std::endl;
// -------------------------------------------------------------------------
for (int val = 1; val <= 10; ++val) {
sum += val;
std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
}
// This pair of code prints the std::cout multiple times
for (int val = 50; val <=100;++val) {
sum += val;
std::cout << "Sum of 50 to 100 inclusive is " << sum << std::endl;
}
如果有人能解释输出的差异,我将不胜感激。提前致谢!
对于大括号,大括号中的所有内容都由 for 循环执行
for (...;...;...)
{
// I get executed in the for loop!
// I get executed in the for loop too!
}
// I don't get executed in the for loop!
然而,如果没有大括号,它只会直接执行后面的语句:
for (...;...;...)
// I get executed in the for loop!
// I don't get executed in the for loop!
// I don't get executed in the for loop either!
for语句具体定义如下
for ( for-init-statement conditionopt; expressionopt) statement
^^^^^^^^^
其中语句可以是任何语句,包括复合语句
compound-statement:
{ statement-seqopt}
因此在这个 for 语句的例子中
for (int val = 1; val <= 10; ++val)
sum += val;
声明是
sum += val;
而在这个 for 语句的例子中
for (int val = 1; val <= 10; ++val) {
sum += val;
std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
}
声明是
{
sum += val;
std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
}