Moät soá kieán thöùc cô sôû veà laäp trình baèng ngoân ngöõ C

Phaâàn tröôùc | Muïc luïc | Phaâàn sau

VII. Caùc leänh bieåu thò söï laëp laïi

Noäi dung:

Vôùi phaàn naøy ta laøm quen vôùi caùc leänh bieåu thò caùc voøng laëp – söï laëp laïi cuûa moät cuïm leänh naøo ñoù vôùi moät thoâng soá naøo ñoù thay ñoåi coù quy luaät.

 

Leänh For

for (expression1; expression2; expression3) {
   statement1;
   statement2;
   .
   .
   .
}

Trình töï: 1) Tính giaù trò expression1; 2) Tính giaù trò expression2; 3) Neáu giaù trò expression2 sai, nhaûy khoûi leänh For; neáu ñuùng, thöïc hieän statement1,2…; 4) Tính giaù trò expression3 vaø quay laïi böôùc 2).

Ví du:

1:  /* 07L01.c: Converting 0 through 15 to hex numbers */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int i;
7:
8:     printf("Hex(uppercase)   Hex(lowercase)   Decimal\n");
9:     for (i=0; i<16; i++){
10:       printf("%X                %x                %d\n", i, i, i);
11:    }
12:    return 0;
13: }

Keát quaû:

Hex(uppercase)   Hex(lowercase)   Decimal
0                0                0
1                1                1
2                2                2
3                3                3
4                4                4
5                5                5
6                6                6
7                7                7
8                8                8
9                9                9
A                a                10
B                b                11
C                c                12
D                d                13
E                e                14
F                f                15

Caûnh baùo

Bôûi vì caâu leänh troáng (null statement – phaàn caâu leänh trong leänh For chaúng haïn khoâng coù gì caû) coù nghóa trong C, neân chuù yù khi ñaët laàm daáu ; sau caâu for seõ gaây ra loãi tính toaùn lôùn. Ví duï

for (i=0; i<8; i++)
   sum += i;
 
for (i=0; i<8; i++);
   sum += i;

Ngoaøi ra trong leänh for coù theå coù nhieàu chæ soá chaïy thöïc hieän ñoàng thôøi. Ví duï:

for (i=0, j=10; i<10, j>0; i++, j--){
   /* statement block */
}

Ví duï:

1:  /* Multiple expressions */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int i, j;       
7:
8:     for (i=0, j=8; i<8; i++, j--)
9:        printf("%d  +  %d  =  %d\n", i, j, i+j);
10:    return 0;
11: }

Keát quaû:

0  +  8  =  8
1  +  7  =  8
2  +  6  =  8
3  +  5  =  8
4  +  4  =  8
5  +  3  =  8
6  +  2  =  8
7  +  1  =  8

Voøng laëp For voâ haïn

for ( ; ; ){
  /* statement block */
}

Nhaát thieát phaûi coù ñieàu kieän ñeå nhaûy ra khoûi voøng laëp. Neáu khoâng maùy seõ ñöùng yeân vaø chæ coù theå khôûi ñoäng cöùng trôû laïi.

Ví duï:

1:  /* Conditional loop */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int c;
7:
8:     printf("Enter a character:\n(enter x to exit)\n");
9:     for ( c=' `; c != `x'; ) {
10:       c = getc(stdin);

11:       putchar(c);
12:    }
13:    printf("\nOut of the for loop. Bye!\n");
14:    return 0;
15: }

Keát quaû:

Enter a character:
(enter x to exit)
H
H
i
i
x
x
Out of the for loop. Bye!

Leänh While

while (expression) {
   statement1;
   statement2;
   .
   .
   .
}

Trình töï: 1) Tính giaù trò expression; 2) Neáu giaù trò expression sai, nhaûy khoûi leänh while; neáu ñuùng, thöïc hieän statement1,2… vaø quay laïi böôùc 1).

Ví duï:

1:  /* Using a while loop */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int c;
7:
8:     c = ` `;
9:     printf("Enter a character:\n(enter x to exit)\n");
10:    while (c != `x') {
11:       c = getc(stdin);
12:       putchar(c);
13:    }
14:    printf("\nOut of the while loop. Bye!\n");
15:    return 0;
16: }

Keát quaû:

Enter a character:
 
(enter x to exit)
H
H
i
i
x
x
Out of the while loop. Bye!

Voøng laëp While voâ haïn

while (1) {
   statement1;
   statement2;
   .
   .
   .
}

Nhaát thieát phaûi coù ñieàu kieän ñeå nhaûy ra khoûi voøng laëp. Neáu khoâng maùy seõ ñöùng yeân vaø chæ coù theå khôûi ñoäng cöùng trôû laïi.

Voøng laëp do-while

do {
   statement1;
   statement2;
   .
   .
   .
} while (expression);

Trình töï: 1) Thöïc hieän statement1,2…; 2) Neáu giaù trò expression sai, nhaûy khoûi voøng laëp; neáu ñuùng, quay laïi böôùc 1).

Ví duï:

1:  /* Using a do-while loop */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int i;
7:
8:     i = 65;
9:     do {
10:       printf("The numeric value of %c is %d.\n", i, i);
11:       i++;
12:    } while (i<72);
13:    return 0;
14: }
Keát quaû:
The numeric value of A is 65.
The numeric value of B is 66.
The numeric value of C is 67.
The numeric value of D is 68.
The numeric value of E is 69.
The numeric value of F is 70.
The numeric value of G is 71.

Voøng laëp loàng nhau

Ví duï:

1:  /* Demonstrating nested loops */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int i, j;
7:
8:     for (i=1; i<=3; i++) {   /* outer loop */

9:        printf("The start of iteration %d of the outer loop.\n", i);
10:       for (j=1; j<=4; j++)  /* inner loop */
11:          printf("    Iteration %d of the inner loop.\n", j);
12:       printf("The end of iteration %d of the outer loop.\n", i);
13:    }
14:    return 0;
15: }

Keát quaû:

The start of iteration 1 of the outer loop.
    Iteration 1 of the inner loop.
    Iteration 2 of the inner loop.
    Iteration 3 of the inner loop.
    Iteration 4 of the inner loop.
The end of iteration 1 of the outer loop.
The start of iteration 2 of the outer loop.
    Iteration 1 of the inner loop.
    Iteration 2 of the inner loop.
    Iteration 3 of the inner loop.
    Iteration 4 of the inner loop.
The end of iteration 2 of the outer loop.
The start of iteration 3 of the outer loop.
    Iteration 1 of the inner loop.
    Iteration 2 of the inner loop.
    Iteration 3 of the inner loop.
    Iteration 4 of the inner loop.
The end of iteration 3 of the outer loop.

Exercises

  1. Phaân bieät coù gì khaùc:
for (i=0, j=1; i<8; i++, j++)
     printf("%d  +  %d  =  %d\n", i, j, i+j);
 
for (i=0, j=1; i<8; i++, j++);
     printf("%d  +  %d  =  %d\n", i, j, i+j);
  1. Cho moät soá nguyeân daïng long. Vieát chöông trình xuaát leân maøn aûnh, trong soá ñoù coù nhöõng chöõ soá naøo baèng 4?

Phaâàn tröôùc | Muïc luïc | Phaâàn sau