C ++ Primer Quick Start 3: several common control statements, primer Quick Start
Statements are always executed in sequence: the first statement is executed, followed by the second statement, and the third statement. This is the simplest case. to better control the running of statements, the programming language provides multiple control structures to support more complex statement execution. Let's take a look at the control method provided by C ++.
1. while statement
The while statement provides the cyclic execution function. You can use the while statement to write a fun number game from 1 to 5 (including 5): 1 + 2 + 3 + 4 + 5
# Include <iostream>
Int main (){
Int game_num = 0, val = 1;
// Continue until the value is greater than 5:
While (val <= 5 ){
// Game_num + val is followed by game_num:
Game_num + = val;
+ + Val; // val plus 1
}
Std: cout <"the sum of the five numbers from 1 to 5 is:" <game_num <std: endl;
Return 0;
}
After compilation and execution, the output is:
The sum of the 5 numbers from 1 to 5 is: 15
How is it? Is the while structure very easy? Simple and powerful!
II. for statements
For statements appear because while statements have disadvantages: while loops use variables to control the number of cycles. Every time you execute the while statement, you must check the value of the variable, execute the loop body, change the value of the variable, and check the value of the variable again and again.
It is troublesome to control the loop by changing the variable value in the function body. The C ++ language defines the second control structure: for statement, which simplifies the control logic of loop variables. Let's look at the example above:
# Include <iostream>
Int main (){
Int game_num = 0;
For (int val = 1; val <= 5; ++ val)
Game_num + = val;
Std: cout <"the sum of the five numbers from 1 to 5 is:" <game_num <std: endl;
Return 0;
}
Can you see it? The for statement header consists of three parts: An initialization, a condition, and an expression. In this example, the initialization statement is:
Int val = 1;
The initialization statement is executed only once when the for statement is entered.
Condition: val <= 10
Expression: ++ val
To sum up the usefulness of this cool:
1. initialize val to 1.
2. Test whether val is smaller than or equal to 5.
3. If val is less than or equal to 5, execute the for loop body and add val to game_sum. If val is greater than 5, the loop is exited and the first statement after the for statement body is executed.
4. val and 1.
5. Repeat Step 1 and continue to execute the for loop body as long as the condition is true.
Good time? Simple? These two structures are useful and are the most common control structures. Everyone must master it.
This article is original. Please indicate the source for reprinting and mark the following content. Thank you!
Public Account: coder_online)
- New programmers enter the number 12345 to obtain the QQ group. Join the interest group and let laruence get you started.
- Let's talk about the technology.
More experts (java/C ++/Linux/Android) help you solve problems, interact with you, and discuss the future of programming.
Long press the QR code to identify and follow the programmer InterAction alliance
After paying attention to it, I am not in a hurry to become friends with tech experts!
Disclaimer: The image used in this article is partly from the network
To read more original technical articles, click "read the original article"
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.