Contents
- Process Control
- Select structure-if
- Select structure-Switch
- Loop Structure-while
- Loop Structure-do while
- Loop Structure-
- Break and continue
Back to Top 1. Process Control
1> Sequence Structure: Default flow structure. Execute each statement in the writing order.
2> select structure: Determine the given conditions and determine the code to be executed based on the judgment results.
3> loop structure: execute a piece of code repeatedly when a given condition is set.
Go Back to Top 2, select structure-if1. simple to use
1> If (expression) Statement 1;
1) if (count> 50) Class;
2> If (expression) Statement 1; else Statement 2;
1) f (count> 50) starts; else does not start;
3> If (expression ){}
1) if (count> 50) {start; classroom arrangement;} else {}
2) Scope {}
3> If-else if-Else
1) if (a = 0) else if (a> 0) else
2) features: Only one bracket is executed.
1> Compound Conditions
* The value range of the course time (9 ~ 12 | 14 ~ 17)
2> traps
1) if (! = 0) {A is not 0;} else {A is 0 ;}
2) if ();{}
3) if (a = 0)
4) A = 0;
5) if (10) int A = 9;
2. Exercise
1> input an integer "day" to indicate the day of the week, and output the day of the week based on the value of day. For example, if day = 1, "Monday" is output"
2> enter an integer month to represent the month. The corresponding season is output based on the month.
Spring: 3, 4, 5
SUMMER: 6, 7, 8
AUTUMN: 9, 10, and 11
Winter: 12, 1, 2
3> enter an integer score to represent the score, according to the score output level (A-E) (in two ways)
A: 90 ~ 100
B: 80 ~ 89
C: 70 ~ 79
D: 60 ~ 69
E: 0 ~ 60
Go back to top 3. Select structure-switch1. easy to use
1> switch (expression) {Case value 1: break ;... Default: break ;}
Example
Int A = 10;
Switch (){
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;
}
1) briefly describe the role of break
2) define variables in case
2. Comparison between If and switch
1) In many cases, they can be exchanged.
2) If is used more often and more flexible, switch can only be a single value
3. Exercise
Use switch instead of if to implement the exercises in if
Back to Top 4. Loop Structure-while1. easy to use
1> continuous printf ("push-ups") 20 times;
2> while (expression ){}
3> continue: output 5 breaks
4> Use of break: Stop the loop at a time
2. Features
The loop body is executed only when the condition is true.
3. Exercise
1> prompt the user to enter a positive integer N, calculate 1 + 2 + 3 +... + N's sum
2> prompt the user to enter a positive integer N, calculate 1-2 + 3-4 + 5-6 +... + N's sum
4. traps
While (condition );
Back to Top 5. Loop Structure-do while
1> features: a loop body will be executed
2> comparison of while and do while Loops
Int I = 0;
While (I <0 ){
I ++;
}
Int I = 0;
Do {
I ++;
} While (I <0 );
Back to Top 6. Loop Structure-for1. simple and practical
1> running process
2> the initialization statement can be multiple sentences (put printf in the brackets of)
2. For loop nesting
Friend list 1
Friend 1
Friend 2
Friend list 2
Friend 1
Friend 2
Friend list 3
Friend 1
Friend 2
3. traps
1> endless loop (;;);
2> scope of variables in the for loop body
3> scope Obfuscation
For (INT I = 0; I <5; I ++) int A = 10;
4. Comparison between a while loop and a For Loop
1> interchangeable
2> for loop variables can be recycled in a timely manner
5. Exercise
1> prompt the user to enter a positive integer n. If N5 is used, the following figure is output, and the other N values are similar.
2> output the 9-9 multiplication table
Return to Top 7, break, and continue1. break
1> usage: Switch \ Loop Structure
2> Break under nested loops
2. Continue
1> application scenarios: cyclic structure
2> continue under nested loops