In the programming language, if-else allows us to make different responses according to different situations, loop allows the computer to do a large number of tasks of similar nature to automate operations, and flow control allows us in the loop, you can fast forward to next iteration, or even leave the loop early.
Below I will briefly record the C++ grammar of the above items
if-else
if else, as the name implies, is to use if
to set the condition. If the condition is true, the program under the condition will be executed; if other conditions are set else if
, the judgment will automatically start if the first if condition does not match.
If there are no any conditions are matched, the program under the last else
will be executed (If the else
block is not set, this program will be skipped this block).
The following is a simple sample code:
#include <iostream> using namespace std; int main() { int score = 98; if (score <= 30) { cout << "Your score <= 30" << endl; } else if (score > 30 and score <= 60) { cout << "Your score <= 60" << endl; } else { cout << "Your score > 60" << endl; } return 0; }
Output:
Your score > 60
Since the first two condition judgment do not match, the program of the else
block is finally executed. By the way, if we have multiple conditions that need to be judged at the same time, we can use logical symbols like and
(&&
) or or
(||
)
switch
The switch
statement can also be used to make judgments. The value to be judged is taken from the brackets after the switch
and matches the value in the case. If none of them match, the program block under default is executed.
The following is a small rewrite code of if-else, used to make similar score judgments.
#include <iostream> using namespace std; int main() { int score = 53; switch (score / 10) { case 10: case 9: cout << "Grade is A." << endl; break; case 8: cout << "Grade is B." << endl; break; case 7: cout << "Grade is C." << endl; break; default: cout << "Big trouble!" << endl; break; } return 0; }
Output:
Big trouble!
loop
Loops can be roughly divided into two types, for-loop and while-loop.
for-loop
for-loop, three conditions such as initial value, execution range, and change value must be set so that the computer can execute the program in the loop repeatedly.
The following is a classic for-loop code:
#include <iostream> using namespace std; int main() { for (int i=0; i<10; i++) { cout << i << endl; } return 0; }
Output:
0
1
2
3
4
5
6
7
8
9
The loop start with i=0
, and i++
is followed, so each iteration will add 1 to it. Finally, when i
is equal to 10, the loop condition is not matches and terminates, so 10 numbers from 0-9 will be printed at the end.
while-loop
while-loop only needs to enter a simple “condition“, as long as the condition is met, it will continue to execute; relatively, if the condition is not met, the program in the loop will not be executed.
The following will rewrite the for-loop program into while-loop version.
#include <iostream> using namespace std; int main() { int i = 0; while (i<10) { cout << i << endl; ++i; } return 0; }
Output:
0
1
2
3
4
5
6
7
8
9
If you want the program to execute endlessly, just write the condition as while (true), but if you want to jump out of the loop after meeting certain conditions, you need the “flow control” command.
flow control
Basically, only the applications of break
, continue
, etc. in the loop are recorded here, and goto
is not mentioned for the time being.
To put it simply, continue
means skip one iteration in the loop and go directly to the next iteration; while break
means simply jump out of the entire loop.
The following is a simple sample code:
#include <iostream> using namespace std; int main() { int i = 0; while (i <= 100) { i++; if (i < 90) { continue; } else if (i == 97) { break; } cout << i << endl; } return 0; }
Output:
90
91
92
93
94
95
96
This is rewritten from the while-loop example program above. When i is less than 90, this round of iteration will be automatically skipped, so nothing will be printed; and when i is equal to 97, the entire loop will be jumped out, so the program It ends after 96 is printed.
References
- https://www.w3schools.com/cpp/cpp_conditions.asp
- https://www.w3schools.com/cpp/cpp_for_loop.asp
- https://www.w3schools.com/cpp/cpp_break.asp
Read More
- [Linux][C++] How to compile and execute a C++ program
- [C++] Tutorial(1): Data Type
- [C++] Tutorial(2): Arithmetic Expression
- [C++] Tutorial(3): Data Input and Output
- [C++] Tutorial(4): if-else, switch, loop and flow control
- [C++] Tutorial(5): File Read and Write
- [C++] Tutorial(6): Pointer and Reference
- [C++] Tutorial(7): Functions