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) { classes ; Classroom Arrangement ;} 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 | | ~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,ten, One
Winter: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 value 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 ten");
Break
Default
printf (" nothing ");
Break
}
L briefly describe the role of break
L define variables in case
2. Ifand theSwitchthe contrast
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") for three 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, computes 1+2+3+... +n and
2> prompts the user to enter a positive integer n, computes 1-2+3-4+5-6+... +n and
4. Traps
while ( condition );
V. Structure of circulation-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);
VI. Structure of circulation-for1. Simple and practical
L Running Process
L Initialization statements can be multiple sentences (place printf in the for parentheses)
2. ForLoop 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. WhileLoops and forComparison of Loops
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, breakand theContinue1. Break
L Use occasion:switch\ cycle Structure
L break under Nested loops
2. Continue
L Application: Cyclic structure
L continue under cyclic nesting
Process Control in C