- while语句
while语句反复执行一段代码,直至给定条件为假为止。
#include <iostream>
int main() {
int a = 0, b = 1;
while (b <= 10) {
a += b;
++b;
}
std::cout << "1至10的累和为:" << a << std::endl;
return 0;
}
- for语句
#include <iostream>
int main() {
int a = 0;
for (int b = 1; b <= 10; ++b) {
a += b;
}
std::cout << "1至10的累和为:" << a << std::endl;
return 0;
}