# #C Programming (I) - 6
## Nested loop
English: Lei KuoLiang
nicolaslouis@mail.fcu.edu.tw
Chinese(TW): Wang M.H
---
### This week's course catalog
0. Review
1. Nested loop
2. flag application
---
## Last week's course review
----
### Loop
* while
* do while
* for
----
#### while
```c
while ( Judgment sentence ) {
Narrative sentence 1;
Narrative sentence 2;
......
}
```
```flow
st=>start: Enter the while loop
e=>end: Leave the while loop
stat=>operation: Narrative sentence
cond=>condition: Judgment sentence
st->cond(yes,right)->stat(top)->cond
cond(no)->e
```
----
#### do while
```c
do {
Narrative sentence 1;
Narrative sentence 2;
......
} while ( 判斷句 ); //Attention!!!!Semicolon Is Needed(;)!!!
```
```flow
st=>start: Enter the do while loop
e=>end: Leave the while loop
stat=>operation: Narrative sentence
cond=>condition: Judgment sentence
st->stat->cond
cond(yes, right)->stat
cond(no)->e
```
----
#### for
```c
for ( Enter the loop narrative sentence; judgment sentence; loop narrative sentence ){
Narrative sentence 1;
Narrative sentence 2;
...
}
```
```flow
st=>start: Enter for loop
e=>end: Leave for the loop
init=>operation: Enter the loop narrative
loop=>operation: Circular narrative
stat=>operation: Narrative sentence
cond=>condition: Judgment sentence
st->init->cond
cond(no,bottom)->e
cond(yes)->stat(right)->loop(right)->cond
```
---
## Nested loop
Because the bird's nest is wrapped in a circle, the nested circle means that the layer of the circle is surrounded by a layer of loops.
----
A code that can print two rectangular triangles of 5.
```c
for(int i = 1; i <= 5; i++){
for(int j = 0; j < i; j++){
printf("*");
}
printf("\n");
}
```
Results of the
```
*
**
***
****
*****
```
----
For the counter of the for loop, the outermost layer is i, the second layer is j, the third layer is k, and the fourth layer is m.
---
### Exercise 1
Enter 1 integer N to print an isosceles triangle with a base of (N * 2 - 1) and a height of N
```
5
*
***
*****
*******
*********
```
---
### Exercise 2
Use the nested for loop to print the 99 multiplication table (note the number alignment)
```
1*1= 1 2*1= 2 3*1= 3 4*1= 4 5*1= 5 6*1= 6 7*1= 7 8*1= 8 9*1= 9
1*2= 2 2*2= 4 3*2= 6 4*2= 8 5*2=10 6*2=12 7*2=14 8*2=16 9*2=18
1*3= 3 2*3= 6 3*3= 9 4*3=12 5*3=15 6*3=18 7*3=21 8*3=24 9*3=27
1*4= 4 2*4= 8 3*4=12 4*4=16 5*4=20 6*4=24 7*4=28 8*4=32 9*4=36
1*5= 5 2*5=10 3*5=15 4*5=20 5*5=25 6*5=30 7*5=35 8*5=40 9*5=45
1*6= 6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 7*6=42 8*6=48 9*6=54
1*7= 7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 8*7=56 9*7=63
1*8= 8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 9*8=72
1*9= 9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
```
---
## Flag application
In the loop, flag can be used as the basis for jumping out of the loop.
----
Below is a piece of code that "prints the first 10 prime numbers within 1000"
<!--

-->
```c
int count_prime = 0;
for (int i = 2; i < 1000; i++){
int is_prime = 1; //State initialization
for (int j = 2; j < i; j++)
if (i % j == 0){
is_prime = 0;
break;
}
//If it is a prime number, it will be printed and counted.
if(is_prime){
printf("%d", i);
count_prime++;
//If 10 numbers have been printed, the loop will be jumped out (and the end will not be blank), otherwise continue
if(count_prime >= 10)
break;
printf(" ");
}
}
printf("\n");
```
----
In the code of a round battle, record the flag of the current round state:
```c
int char1_hp = 30, char1_atk = 8;
int char2_hp = 33, char2_atk = 7;
int now_turn = 1;
printf("Character 1\nHP:%d\tATK:%d\n"
"Character 2\nHP:%d\tATK:%d\n\n",
char1_hp, char1_atk, char2_hp, char2_atk);
while(char1_hp > 0 && char2_hp > 0){
if (now_turn == 1){
char2_hp -= char1_atk;
printf("Character 1 launches an attack on character 2, causing %d damage.\n"
"Role 2 remaining %d point HP\n\n",
char1_atk, char2_hp);
now_turn = 2; //Offensive and defensive exchange
}
else if (now_turn == 2){
char1_hp -= char2_atk;
printf("Character 2 launches an attack on character 1, causing %d damage.\n"
"Role 1 remaining %d point HP\n\n",
char2_atk, char1_hp);
now_turn = 1; //Offensive and defensive exchange
}
}
printf("Role %d loses combat ability\n", now_turn);
```
---
## Homework
----
One day, Xiaomei wanted to design the pattern on the napkin, so she asked his engineer friend Xiao Ming to write a program that could output a bunch of diamonds to make Xiaomei photocopy.
Let the user enter the diagonal length of the diamond, the number of columns, and the number of rows, and print a matching pattern after a blank line.
----
* Diagonal length input range of diamonds is 3, 5, 7, 9
* The number of rows (columns) and the number of columns (rows) is 2~10.
* If the user enters an error, he/she needs to prompt for an error and then re-enter
* Each left and right adjacent diamonds need to be separated by a blank key
* Each of the upper and lower adjacent diamonds needs to be separated by one line
----
 
---
###### tags: `108 Ai-Mod-Eng-LKL`
{"metaMigratedAt":"2023-06-15T01:38:32.868Z","metaMigratedFrom":"YAML","title":"W6 - Nested loop","breaks":true,"slideOptions":"{\"transition\":\"slide\"}","contributors":"[{\"id\":\"befaa4d9-75b6-4c05-baa7-7949e0ffa1e2\",\"add\":6289,\"del\":727}]"}