First, Process Control
L Sequential structure: the default process structure. Each statement is executed in the written order.
L Select structure: Judge the given condition and decide which code to execute according to the result of judgment.
L Loop Structure: Executes a piece of code repeatedly when a given condition is true.
Second, choose the structure-IF1. Simple to use
L if (expression) statement 1;
u if (count>50) classes; Class
L if (expression) statement 1; Else Statement 2;
U f (count>50) classes; else no classes;
L if (expression) {}
u if (count>50) {class, Layout classroom;} else {}
Scope of the U {}
L If-else If-else
u if (a==0) else if (a>0) Else
U features: Only one parenthesis will be executed
L Compound Condition
U value range for class time (9~12 | | 14 ~17)
L Traps
u if (a!=0) {A is not 0;} else {A is 0;}
U if (a); { }
u if (a=0)
u a = = 0;
U if (ten) int a = 9;
2. Exercises
1> Enter an integer day to represent the day of the week, according to the value of day to output the corresponding day of the week, such as day==1, the output "Monday"
2> Enter an integer, month, to represent the month, according to the month output corresponding to the season.
Spring: 3, 4, 5
Summer: 6, 7, 8
Autumn: 9, 10, 11
Winter: 12, 1, 2
3> Enter an integer score to represent the score, based on the score output level (A-E) (in two ways)
a:90~100
b:80~89
c:70~79
d:60~69
E:0~60
Third, choose the structure-switch1. Simple to use
L-switch (expression) {case numeric 1:break; ... default:break;}
l Example
int a = 10;
Switch (a) {
Case 0:
printf ("This is a 0");
Break
Case 5:
printf ("This is a 5");
Break
Case 10:
printf ("This is a 10");
Break
Default
printf ("nothing");
Break
}
L briefly describe the role of break
L Define variables in case
2. Comparison of IF and switch
L Many situations can be interchanged
L If used more, more flexible, switch can only a single value
3. Exercises
Use switch instead of if to implement the problem in if
IV. cyclic structure-while1. Simple to use
L Continuous printf ("push-ups") 20 times;
L while (expression) {}
L Continue: Output 5 times take a break.
L Use of break: Stop the loop at some time
2. Features
The loop body is executed only if the condition is established.
3. Exercises
1> prompts the user to enter a positive integer n to calculate the 1+2+3+...+n and
2> prompts the user to enter a positive integer n to calculate the 1-2+3-4+5-6+...+n and
4. Traps
while (condition);
V. Cyclic structure-do while
L Features: Must be executed once the body of the loop
L while and do While loop comparison
int i=0;
while (i<0) {
i++;
}
int i=0;
do{
i++;
} while (i<0);
Six, circular structure-for1. Simple and practical
L Running Process
L Initialization statements can be multiple sentences (place printf in the for parentheses)
2. For loop nesting
Friends List 1
Friends 1
Friends 2
Friends List 2
Friends 1
Friends 2
Friends List 3
Friends 1
Friends 2
3. Traps
1> dead loop for (;;);
2> scope of internal variables for the for loop body
3> Scope Confusion
for (int i = 0; i<5; i++) int a = 10;
4. While loop and for loop comparison
L can be interchangeable
L for loop variables can be recovered in time
5. Exercises
1> prompts the user to enter a positive integer n, if N5, outputs the following graph, the other n values, and so on
2> Output 99 Multiplication table
Seven, break and continue1. Break
L Use occasion: switch\ cycle Structure
L Break under Nested loops
2. Continue
L Application: Cyclic structure
L Continue under cyclic nesting
07-c Language Process Control