-
Process Control
-
Sequential structure: The default process structure. Each statement is executed in the written order.
-
Select structure: Judge a given condition and then decide which piece of code to execute based on the result of the decision.
-
Loop structure: Executes a piece of code repeatedly when a given condition is true.
-
Select Structure-if
-
Simple to use
-
Exercises
-
Enter an integer day to represent the day of the week, output the corresponding day of the week according to the value of day, such as day==1, to output "Monday"
-
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
-
Enter an integer score to represent the score, based on the fractional output level (A-E) (in two ways)
a:90~100
b:80~89
c:70~79
d:60~69
E:0~60
-
Select Structure-switch
-
Simple to use
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
}
-
Comparison of IF and switch
-
A lot of things are interchangeable.
-
If used more, more flexible, switch can only a single value
-
Exercises
Use switch instead of if to implement the problem in if
-
Cyclic structure-while
-
Simple to use
-
Continuous printf ("push-ups") 20 times;
-
while (expression) {}
-
Continue: Output 5 times take a break.
-
Use of break: Stop a loop at a time
-
Characteristics
The loop body is executed only if the condition is established.
-
Exercises
-
Prompts the user to enter a positive integer n, computes the 1+2+3+...+n and
-
Prompts the user to enter a positive integer n, computes the 1-2+3-4+5-6+...+n and
-
Trap
while (condition);
-
Loop structure-do while
int i=0;
while (i<0) {
i++;
}
int i=0;
do{
i++;
} while (i<0);
-
Cyclic structure-for
-
Simple and practical
-
For loop nesting
Friends List 1
Friends 1
Friends 2
Friends List 2
Friends 1
Friends 2
Friends List 3
Friends 1
Friends 2
-
Trap
-
Dead loop for (;;);
-
Scope of internal variables for the for loop body
-
Scope obfuscation
for (int i = 0; i<5; i++) int a = 10;
-
Comparison of While loops and for loops
-
Exercises
-
Prompts the user to enter a positive integer n, if N5, outputs the following graph, other n values, and so on
-
Output 99 multiplication table
-
Break and Continue
-
Break
-
Continue
iOS Development Knowledge (v)