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

X. Caùc leänh ñieàu khieån taùc vuï chöông trình

Noäi dung:

Vôùi phaàn naøy ta laøm quen vôùi caùc caâu leänh ñieàu kieän sau:

 

Leänh if

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

Neáu expression laø ñuùng (TRUE), caùc statement1,2…  seõ ñöôïc thi haønh; neáu sai, chöông trình boû qua.

Ví duï:

1:  /* Using the if statement */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int i;
7:
8:     printf("Integers that can be divided by both 2 and 3\n");
9:     printf("(within the range of 0 to 100):\n");
10:    for (i=0; i<=100; i++){
11:       if ((i%2 == 0) && (i%3 == 0))
12:         printf("   %d\n", i);
13:   }
14:   return 0;
15: }

Keát quaû:

 
Integers that can be divided by both 2 and 3
(within the range of 0 to 100):
   0
   6
   12
   18
   24
   30
   36
   42
   48
   54
   60
   66
   72
   78
   84
   90
   96

Leänh if-else

if (expression) {
   statement1;
   statement2;
   .
   .
   .
}
else {
   statement_A;
   statement_B;
   .
   .
   .
}

Neáu expression laø ñuùng (TRUE), caùc statement1,2…  seõ ñöôïc thi haønh; neáu sai, ), caùc statement_A,_B… seõ ñöôïc thi haønh.

Ví duï:

1:  /* Using the if-else statement */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int i;
7:
8:     printf("Even Number   Odd Number\n");
9:     for (i=0; i<10; i++)
 
10:       if (i%2 == 0)
11:          printf("%d", i);
12:       else
13:          printf("%14d\n", i);
14:
15:    return 0;
16: }

Keát quaû:

 
Even Number   Odd Number
0             1
2             3
4             5
6             7
8             9

Leänh if loàng nhau

Ví duï:

1:  /* Using nested if statements */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int i;
7:
8:     for (i=-5; i<=5; i++){
9:        if (i > 0)
10:           if (i%2 == 0)
11:               printf("%d is an even number.\n", i);
12:           else
13:               printf("%d is an odd number.\n", i);
14:       else if (i == 0)
15:           printf("The number is zero.\n");
16:       else
17:           printf("Negative number: %d\n", i);
18:    }
19:    return 0;
20: }

Keát quaû:

Negative number: -5
Negative number: -4
Negative number: -3
Negative number: -2
Negative number: -1
The number is zero.
1 is an odd number.
2 is an even number.
3 is an odd number.
4 is an even number.
5 is an odd number.

Leänh switch

Leänh ñieàu kieän vôùi nhieàu khaû naêng xaõy ra.
 
switch (expression) {
   case expression1:
        statement1;
   case expression2:
        statement2;
   .
   .
   .
   default:
        statement-default;
}

Ví duï:

1:  /* Using the switch statement */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int day;
7:
8:     printf("Please enter a single digit for a day\n");
9:     printf("(within the range of 1 to 3):\n");
10:    day = getchar();
11:    switch (day){
12:       case `1':
13:          printf("Day 1\n");
14:       case `2':
15:          printf("Day 2\n");
16:       case `3':
17:          printf("Day 3\n");
18:       default:
19:          ;
20:    }
21:    return 0;
22: }
 

Keát quaû:

 
Please enter a single digit for a day
(within the range of 1 to 3):
3
Day 3

Neáu cho giaù trò laø 1, keát quaû seõ laø:

Please enter a single digit for a day
(within the range of 1 to 3):
1
Day 1
Day 2
Day 3

Ta thaáy sau khi thi haønh 1 tröôøng hôïp, caùc tröôøng hôïp sau ñoù cuõng tuaàn töï thi haønh. Muoán khoâng xaõy ra, phaûi duøng leänh break ñeå nhaûy ra khoûi leänh ñieàu kieän.

Leänh break

Duøng ñeå chaám döùt thi haønh cuïm leänh naøo ñoù trong leänh ñieàu kieän switch hoaëc voøng laëp for, while.

Ví duï:

1:  /* Adding the break statement */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int day;
7:
8:     printf("Please enter a single digit for a day\n");
9:     printf("(within the range of 1 to 7):\n");
10:    day = getchar();
11:    switch (day){
12:       case `1':
13:          printf("Day 1 is Sunday.\n");
14:          break;
15:       case `2':
16:          printf("Day 2 is Monday.\n");
17:          break;
18:       case `3':
19:          printf("Day 3 is Tuesday.\n");
20:          break;
21:       case `4':
22:          printf("Day 4 is Wednesday.\n");
23:          break;
24:       case `5':
25:          printf("Day 5 is Thursday.\n");
26:          break;
27:       case `6':
28:          printf("Day 6 is Friday.\n");
29:          break;
30:       case `7':
31:          printf("Day 7 is Saturday.\n");
32:          break;
33:       default:
34:          printf("The digit is not within the range of 1 to 7.\n");
35:          break;
36:    }
37:    return 0;
38: }

Keát quaû:

 
Please enter a single digit for a day
(within the range of 1 to 7):
1
Day 1 is Sunday.

Ví duï:

1:  /* Breaking an infinite 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:     while (1) {
10:       c = getc(stdin);
11:       if (c == `x')
12:          break;
13:    }
14:    printf("Break the infinite while loop. Bye!\n");
15:    return 0;
16: }

Keát quaû:

 
Enter a character:
(enter x to exit)
H
I
x
Break the infinite while loop. Bye!

Leänh continue

Thay vì chaám döùt moät voøng laëp giöõa chöøng, coù khi ta caàn tieáp tuïc voøng laëp maø nhaûy qua moät soá caâu leänh naøo ñoù. Trong tröôøng hôïp naøy ta duøng leänh continue. Gaëp leänh naøy, chöông trình nhaûy ngay trôû laïi veà ñaàu leänh laëp vaø tieáp tuïc.

Ví duï:

1:  /* Using the continue statement */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     int i, sum;
7:
8:     sum = 0;
9:     for (i=1; i<8; i++){
10:       if ((i==3) || (i==5))
11:          continue;
12:       sum += i;
13:    }
14:    printf("The sum of 1, 2, 4, 6, and 7 is: %d\n", sum);
15:    return 0;
16: }

Keát quaû:

The sum of 1, 2, 4, 6, and 7 is: 20

Leänh goto

labelname:
   statement1;
   statement2;
   .
   .
   .
   goto  labelname;

Leänh goto laøm cho chöông trình nhaûy ñeán vò trí xaùc ñònh bôûi labelname.

Caûnh baùo

Khi duøng caùc leänh break, continue vaø ñaëc bieät goto, phaûi raát caån thaän vaø haïn cheá, vì caùc leänh treân thöôøng hay gaây ra nhöõng loãi nghieâm troïng khoâng döï tính tröôùc ñöôïc maø ta khoù duøng phöông tieän doø söûa loãi (debugger) ñeå tìm ra.

Baøi taäp

  1. Cho x = 4, y = 2, vaø operator = `-', Keát quaû cuûa caùc cuïm leänh sau laø gì?
switch (operator){
   case `+':  x += y;
   case `-':  x -= y;
   case `*':  x *= y;
   case `/':  x /= y;
   default:   break;
}
 
switch (operator){
   case `+':  x += y; break;
   case `-':  x -= y; break;
   case `*':  x *= y; break;
   case `/':  x /= y; break;
   default:   break;
}
  1. Giaù trò cuûa x sau cuïm leänh naøy?
x = 1;
for (i=2; i<10; i++){
   if (i%3 == 0)
      continue;
   x += i;
}
  1. Vieát chöông trình ñoïc kyù töï töø I/O chuaån. Neáu gaëp kyù töï A,B,C thì hieån thò giaù trò soá leân maøn aûnh (duøng switch).

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