gpt4 book ai didi

c++ - 输出的差异取决于 for 循环中省略花括号 (c++)

转载 作者:行者123 更新时间:2023-11-28 00:07:17 26 4
gpt4 key购买 nike

<分区>

我一直按照本网站引用指南的建议阅读 c++ 入门书,我注意到作者省略了 for 循环的大括号。我检查了其他网站,大括号通常应该放在里面。加花括号和不加花括号的输出是不一样的,代码如下

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;
}

如果有人能解释输出的差异,我将不胜感激。提前致谢!

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com