for loop statement:Code for the “Knock the Table” game:
#include <iostream>using namespace std;
int main(){int counter = 0;for (int i = 1; i <= 100; i++) {int ge = i % 10;int shi = i / 10 % 10;
if (ge == 7 || shi == 7 || i % 7 == 0) {cout << "Knock the Table" << endl;counter++; }else {cout << i << endl;}
//cout << i << endl;
}cout << "Knock the Table appeared a total of " << counter << " times" << endl;
system("pause");return 0;}
Running result:
Nested loops:Function: To nest another loop within the loop body to solve some practical problems.
#include <iostream>using namespace std;
int main(){for (int i = 0; i < 10; i++) {for (int j = 0; j < 10; j++) {cout << "* ";}
cout << "\n";}
system("pause");return 0;}
Running result:
Multiplication Table:
#include <iostream>using namespace std;
int main(){for (int i = 1; i < 10; i++) {for (int j = 1; j <= i; j++) {cout << j << "*" << i << "=" << i * j << "\t";}
cout << endl;}
system("pause");return 0;}
Running result:
Jump statements:break statement:Function: Used to exit a selection structure or loop structure.When to use break:1) Appears in a switch statement, its function is to terminate the case and exit the switch.2) Appears in a loop statement, its function is to exit the current loop statement.3) Appears in a nested loop, exits the nearest inner loop statement.
#include <iostream>using namespace std;
int main(){// When to use break
cout << "Please select the difficulty level" << endl;
cout << "1. Easy" << endl;
cout << "2. Medium" << endl;
cout << "3. Hard" << endl;
int select = 0;
cin >> select;
switch (select) {case 1: cout << "Easy" << endl;break;case 2: cout << "Medium" << endl;break;case 3: cout << "Hard" << endl;break;default: cout << "Input error" << endl;break;}
system("pause");return 0;}
Running result:
continue statement:Function: In a loop statement, skip the current loop and continue executing the next loop.
#include <iostream>using namespace std;
int main(){
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) {
continue;
}
cout << i << endl;
}
system("pause"); return 0;}
Running result:
goto statement:Function: Can unconditionally jump to a statement.Syntax: goto label;Explanation: If the label name already exists, executing the goto statement will jump to the label position.
#include <iostream>using namespace std;
int main(){
cout << 1 << endl;
goto FLAG;
cout << 2 << endl;
cout << 3 << endl;
cout << 4 << endl;
FLAG:
cout << 5 << endl;
system("pause"); return 0;}
Running result: