Statements are always executed sequentially: The first statement is finished, followed by the second, third, and so on. This is the simplest case, in order to better control the operation of the statement, the programming language provides a variety of control structures to support more complex statement execution. Let's take a look at the control methods provided by C + +.
One, while statement
The While statement provides loop execution functionality. You can use the while statement to write a fun number of games from 1 to 5 (including 5): 1+2+3+4+5
#include <iostream>
int main () {
int game_num= 0, val = 1;
Continue execution until value is greater than 5:
While (Val <= 5) {
Game_num + val Then plugs to Game_num:
Game_num + = val;
+ + val; Val plus 1
}
Std::cout << "1 to 5 the sum of 5 numbers is:" << game_num<< Std::endl;
return 0;
}
After compiling and executing, the output will be:
1 to 5 The sum of these 5 numbers is: 15
What do you think? is the while structure very easy? Both simple and powerful!
Second, for statement
The For statement appears because the while statement has drawbacks: While loops use variables to control the number of loop executions. Each time you execute the while statement, look at the value of the variable, execute the loop body, then change the value of the variable, check the value of the variable again and again.
Because it is always difficult to control the loop by changing the value of the variable in the function body. The C + + language defines the second control structure: For statement, she simplifies the control logic of the loop variable, and looks at the same example in the example above:
#include <iostream>
int main () {
int game_num = 0;
for (int val = 1; Val <= 5; ++val)
Game_num + = val;
Std::cout << "1 to 5 the sum of 5 numbers is:" << game_num<< Std::endl;
return 0;
}
Did you see it? The For statement head consists of three parts: an initialization, a condition, and an expression . In this example, the initialization statement is:
int val = 1;
The initialization statement executes only once when the For statement is entered.
Condition: Val <= 10
Expression: + + Val
Summarize the usefulness of this three:
1. Initialize Val to 1.
2. Test if Val is less than or equal to 5.
3. If Val is less than or equal to 5, then the FOR loop body is executed and Val is added to the game_sum. If Val is greater than 5, it exits the loop and executes the first statement after the for statement body.
4. Val plus 1.
5. Repeat the 2nd step, as long as the condition is true, continue executing the For loop body
Fun, huh? Simple, huh? Both of these structures are useful and are the most common control structures. We must master yo ^_^
This article is original, reproduced please indicate the source, and mark the following content, thank you!
Follow the public number: Programmer Interaction Alliance (coder_online)
- Programming Novice Input Digital 12345 get QQ group. Join the interest group and let the veteran get you started.
- Programming the birds enter the number 2 Let's talk about technology together.
More (Java/c/c++/linux/android) experts help you solve problems and interact with you to discuss the future of programming.
Long press QR code recognition focus on programmer Interaction Alliance
After the attention and technology Daniel to be friends, not hurry up!
Disclaimer: The picture part of this article is from the network
To read more master original technical articles, click on "Read the original"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + Primer Quick START III: several common control statements