Li Hongqiang-C language Control structure

Source: Internet
Author: User
Tags variable scope

 

C-Language Process Control

First, the Process control structure

(1) Sequential structure: Executes each statement in writing order.

(2) Select structure: Judge the given condition and decide which piece of code to execute according to the judging result.

(3) Loop structure: In the case of a given condition, repeated execution of a piece of code.

Ii. Choice of Structure-if

(i) if simple to use

1) The first type of structure:

A) If the condition is true, the following statement is executed, otherwise it is not executed.

if (condition)

{

Statement 1;

Statement 2;

····

}

2) The second type of structure:

A) If the condition is true, execute statement 1, otherwise execute statement 2.

if (condition)

{

Statement 1;

}

Else

{

Statement 2;

}

3) The third type of structure:

A) First judge the condition 1, if set up to execute statement 1, others do not, if the condition 1 is not established, then check the condition 2, "note" If the condition 3 is established, then the preceding is not established. Only one of all statement blocks will be executed.

if (condition 1)

{

Statement 1;

}

else if (condition 2)

{

Statement 2;

}

else (condition 3)

{

Statement 3;

}

4) The fourth type of structure:

A) In this case, when the condition is true, only statement 1 is executed, statement 2 does not belong to the sub-conditional structure, but it is not recommended.

If (condition)

Statement 1;

Statement 2;

(ii) If use note

①. When comparing size, you can put the constant value on the left, and the value of the variable on the right side to avoid errors. Because if (a==0) is also easy to write if (a=0), and in this case, the compiler does not error, it is recommended to use if (0==a) this notation.

②. Note the assignment operator, do not write = = =.

③. Do not add a semicolon after the If (condition), which represents an empty statement, and the subsequent code block becomes a separate block of code. such as if (condition); {...} Need to be careful

④. Note the scope of the problem, if you want to define a new variable after the IF, you must use curly braces.

if (10>6)

{

The scope of the INT a;//a expires after the code block ends

}

Printf ("a=%d", a);//Compiler error, identifier not found

if (10>6)

Int A; Error in this case, the scope of a is not clear

Printf ("a=%d", a); Compiler error, identifier not found

⑤. If (0) {...} Just pick a number and you can do the condition.

Three If structure exercises

Issue: Enter an integer seore to represent the score, based on the fractional output level (A-E).

A 90-100 b80-89 c70-79 d60-69 e0-60

Code:

View Code

Iii. selection of structural-switch

(i) Switch structure

Switch (value)//is usually a variable

{

Case value 1:

Statement 1;

break;

Case Value 2:

Statement 2;

break;

Case Value 3:

Statement 3;

break;

Default:

Statement 4;

break;

}

Explanation: The structure compares the value to the value 1, and if it is equal, executes all subsequent statements until the break statement jumps out of the entire loop, and if none of the preceding conditions is satisfied, the statement following the default is executed. If the break statement is not written, subsequent statements are executed consecutively until the break statement is encountered or all statements are executed, and the subsequent judgment is ignored if the preceding conditions are true.

Two Switch usage note

Char c= ' A ';

Switch (c)

{

Case ' A ':

Statement 1;

break;

Case 65://cannot write this, because the ASCII value of ' A ' is 65, it will error

Statement 2;

break;

Case Value 3:

Int a=10;

A's scope is not clear error, C language Check the scope of the variable according to {}, here you can add {}

break;

Default:

Statement 4;

break;

}

Note: If you want to define a variable in the statement following the case, you must enclose it in curly braces.

(c) Switch structure exercises

Issue: Enter an integer seore to represent the score, based on the fractional output level (A-E).

A 90-100 b80-89 c70-79 d60-69 e0-60

Code:

View Code

"Compare" if with switch:

1. If statement is capable of completing functions, switch is sometimes not able to complete, such as judging if (a>100)

2. In some cases, the IF statement and the switch statement can be interchanged

3. The switch statement can complete the function, if statement can be completed

IV. Cyclic structure-while

(a) Simple to use

while (condition)

{

Statement

}

Explanation: First determine whether the condition is established, the establishment of the execution, after the execution of the code again to determine whether the condition is established (judging conditions-"code block-" Judgment conditions-"execution code block ...) 1. First determine what the loop is doing, 2. In determining the constraints, that is, how many times to loop, 3. Define a variable to record the number of cycles.

Tip: Use If...break (jump out of the loop) or If...continue statement in the while loop (jump out of the loop to continue the next loop).

The operating principle of the while structure:

①. If the initial condition is not true, the loop body will never be executed.

②. If the condition is true, the loop body is executed once and the condition is determined once the execution is complete.

③. Break: Exits the entire loop directly.

④. Continue: Ends the current loop body, making the next round of cycle judgment.

(ii) Use of attention

1) while (1) {...} is a dead loop

2) while (a<10); {...} A comma is followed by a condition to indicate that the loop body is an empty statement, not a {} part

3) The {} is recommended after the while condition statement, otherwise the loop body is only the first statement

4) Simplest dead loop while (1);//Always execute empty statement, let program crash

Three While practice

Problem: Programmatic implementation, prompting for a positive integer n, calculates the value of the 1+2+3+...N.

Code:

View Code

V. Cyclic structure-do while

Do

{

Statement

}while (condition)

Features: A Do While loop performs at least one loop body

VI. Cyclic structure-for

(i) Basic use

For (statement 1, condition, statement 2)

{

Loop body

}

Statement 1: Typically an initialization statement

Statement 2: Typically an incremental statement (a statement executed after the loop body is executed)

For loop principle:

1) The FOR Loop executes statement 1 at the beginning (the entire for loop executes only once)

2) Determine whether the condition is established, if established, then execute a loop body, and then execute the statement 2, again determine whether the condition is set up, if the condition is not established, the end cycle

3) The Order is: (Statement 1-"Judging conditions-" loop body-"Statement 2-" Judging conditions-"cycle body ...) )

"Contrast" for and while:

On the performance, the for loop slightly better because the while loop can use only external variables, and for loops to recycle variables in a timely manner.

(ii) for use note

①. Do not write a semicolon after the for statement, such as for (); {...}

②. Defining a variable's scope ambiguous error

for (int i=0;i<5;i++)

Int a=10; Compiler error, variable scope ambiguous

The simplest dead loop in a ③. For statement for (;;);

④. Need to be aware of the scope of variable definitions

Int a=10;

for (int i=0,int a=9;i<5;i++)

{

Int a=10;

Printf ("a=%d\n", a);//What is the value of a printed here?

}

Three For loop nesting

To export content in a specific format, such as a list of QQ friends, to improve scalability.

for (in i=0;i<5;i++)

{

Number of loop control lines outside

Printf ("Buddy list%d\n", i);

Internal loop control Number of columns

for (int j=1;j<=6;j++)

{

Printf ("Friend%d\n", J);

}

}

Four For practice

Use the For loop to output the 99 multiplication table. First control the number of rows, in the number of control columns

Code:

Li Hongqiang-C language Control structure

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.