Control structure of C ++

Source: Internet
Author: User
Document directory
  • 2.2.1 If statement
  • 2.2.2 If... else statement
  • 2.2.3 swich... case statement
1. The statement is the minimum unit that can be executed independently in the program. The statement ends with a semicolon. The statement type can be: one statement: 1. assignment Statement: A = a + B; 2. null statement:; // 3. description statement: int I = 5; (the variable indicates where any statement in the program can appear, improving the flexibility of the variable. Different from C) block statements: multiple statements enclosed by a pair. {Int I = 5; I = (I + 5)/2; cout <I <Endl;} 2. control structure to describe the process of the program, people use a flowchart tool. A flowchart is a tool used to describe algorithms (programs). It is concise, intuitive, and accurate. 2.1 Sequence 2.2 selecting and judging the selection structure, also known as the condition branch structure, is a basic program structure type. Conditional branch statements include the if statement, if... else statement, and switch... case statement. 2.2.1 If statement if (expression) Statement; Description: 1. An expression is usually a relational or logical expression. 2. A statement can be a single statement, a block statement, or even an empty statement. Execution Process: Calculate the expression value first. If the expression value is true, execute the statement segment. Otherwise, skip the statement and directly execute the statement following the if statement.
2.2.2 If... else statement if (expression) Statement 1; else Statement 2; Execution Process: Calculate the expression value first. If the expression value is true, execute Statement 1; otherwise, execute Statement 2. If statements can be nested: It is often used for multiple judgment and selection.
2.2.3 swich... case statement switch (expression) {Case constant expression 1: [Statement Block 1] [break;] case constant expression 2: [Statement Block 2] [break;] ...... case constant expression N: [Statement Block N] [break;] [Default: Statement Block N + 1]}
Note: 1. The expression value and constant expression value can only be string or integer. 2. [] indicates that the content is optional. 3. The break and default statements are also optional. Execution Process: 1. evaluate the value of the expression 2. compare the expression value with the constant expression value after case in sequence. If the expression value is equal, the sequence of statements after the case statement is executed. It is known that the break or switch statement is enclosed by curly brackets. 3. If the expression value is not the same as the value of any constant expression after the case statement, if there is a default statement, it will be executed; otherwise, nothing will be executed.
2.3 cyclic statements include the for statement, while statement, and do... while statement 2.3.1 for statement for (expression 1; expression 2; expression 3) statement can be seen as: For (initial value assigned to the loop variable; loop condition; Value-Added loop variable) Execution Process of the loop body: 1. calculates the value of expression 1. 2. calculates the value of expression 2. If expression 2 is 0 (false), It exits the loop and runs the statement following the loop body. If expression 2 is not 0 (true ), 3. 3. execute the Statement of the loop body. 4. Calculate the value of expression 3. 5. Go to 2. The for statement contains another complete loop structure.
2.3.2 while statement while (condition expression) statement execution process: Calculate the condition expression first. If the value of this formula is not 0 (true), the statement of the loop body is executed; otherwise, the loop is exited, if you do not execute the loop body, execute the statement behind the loop body. The while statement is also called a "when" loop.
2.3.3 do... while statement do statement while (conditional expression );
Execution Process: first execute the loop body and then judge the value of the conditional expression. If the expression value is true, the loop body is executed repeatedly until the expression value is false. Do... while loop is also called "until" Type Loop.
While loop and do... while loop difference: under certain conditions, while loop may not be executed once, but do... the while loop is executed at least once under any conditions. 2.4 break, continue, and GOTO statement 2.4.1 break statement: break; Description: The loop body where the interruption is located or switch... case statement block, jump to the current layer to circulate in vitro.
2.4.2 continue statement: continue; Description: jump from the current position in the loop body to the beginning of the loop and continue to execute the loop body. During the execution of the loop, if the continue statement is encountered, the program ends the loop and starts the next loop.
2.4.3 GOTO statement: indicates the number of the GOTO statement. statement number: indicates the statement following the unconditional turning statement number of the program. The GOTO statement jumps out of the loop body, but the GOTO statement is not allowed to jump out of the loop body.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.