本节涉及到的相关代码建议大家可以按照for循环语句的逻辑,重新写相关的代码执行语句
1.for循环语句
作用:满足循环条件,执行循环语句
语法:for(起始表达式,条件表达式;末尾循环体){循环语句;}
#include <iostream> using namespace std; int main(){ //for 循环语句 for (int Tree =0; Tree < 10; Tree--) { cout << "种的距离为:" << Tree << endl; } system("pause"); return 0; }上面的代码是一个逻辑不太好的代码,不太建议用这个来练习
#include<iostream> using namespace std; int main(){ int a = 0; for (;; ) { if (a >= 10) { break; } cout << a << endl; a++; } system("pause"); return 0; }#include <iostream> using namespace std; int main(){ int sum = 0; for (int val = 1; val <= 10; ++val) { sum += val; std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl; } system("pause"); return 0; }#include<iostream> using namespac std; int main(){ int sum = 0; for (int val = -100; val <= 100; ++val) { sum += val; std::cout << sum << std::endl; } system("pause"); return 0; }敲桌子case:
#include<iostream> using namespace std; int main(){ for(int i = 1; i <= 100; i++) { if ( i%7 ==0 || i %10 == 7 || i/10 ==7 ) { cout << "敲桌子" << endl; } else { cout << i << endl; } } system("pause"); return 0; }