CẤU TRÚC ĐIỀU KHIỂN

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

LẬP TRÌNH CƠ SỞ TRẦN NHẬT VINH

CẤU TRÚC ĐIỀU KHIỂN

Cấu trúc điều khiển if else trong C


Thông thường để kiểm giá trị có thỏa mãn với giá trị mong muốn hay không?. Trong
ngôn ngữ C chúng ta thường dùng lệnh If để kiểm tra.
Các Cấu Trúc Điều Khiển

 Câu lệnh if
 Câu lệnh if else
 Câu lênh if…elseif…else
 Câu lệnh điều kiện lồng nhau

Câu lệnh IF: sử dụng câu lệnh IF để lọc kết quả đúng

if(expression){

//code to be executed

Lưu đồ của lệnh If


LẬP TRÌNH CƠ SỞ TRẦN NHẬT VINH

Ví dụ lệnh if:

#include<stdio.h>

int main(){

int number=0;

printf("enter a number:");

scanf("%d",&number);

if(number%2==0){

printf("%d is even number",number);

return 0;
LẬP TRÌNH CƠ SỞ TRẦN NHẬT VINH

Kết quả:

enter a number:4

4 is even number

Kết quả:

enter a number:5

Câu lệnh if else.


Phần trên chúng ta mới chỉ dùng lệnh if để kiểm tra nếu đúng thì sao còn chưa bắt được
sự kiện nếu sai thì làm gì. Do đó trong C hay bất kỳ ngôn ngữ lập trình nào đó đều cung
cấp cho chúng ta câu lệnh else để xử lý điều kiện sai, theo cú pháp:

if(expression){

//code to be executed if condition is true

}else{

//code to be executed if condition is false

Lưu đồ của lệnh If - else


LẬP TRÌNH CƠ SỞ TRẦN NHẬT VINH

Ví dụ lệnh if-else

#include<stdio.h>

int main(){

int number=0;

printf("enter a number:");

scanf("%d",&number);

if(number%2==0){

printf("%d is even number",number);

else{
LẬP TRÌNH CƠ SỞ TRẦN NHẬT VINH

printf("%d is odd number",number);

return 0;

Kết quả:

enter a number:4

4 is even number

Kết quả:

enter a number:5

5 is odd number

Câu lệnh if - else - if


Thực thi nhiều câu lệnh với nhiều điều kiện khác nhau.
Cấu trúc:

if(condition1){

//code to be executed if condition1 is true

}else if(condition2){

//code to be executed if condition2 is true

else if(condition3){

//code to be executed if condition3 is true

...

else{

//code to be executed if all the conditions are false

Lưu đồ lệnh if-else-if:


LẬP TRÌNH CƠ SỞ TRẦN NHẬT VINH

Ví dụ lệnh if-else-if:

#include<stdio.h>

int main(){

int number=0;

printf("enter a number:");

scanf("%d",&number);

if(number==10){

printf("number is equals to 10");

else if(number==50){
LẬP TRÌNH CƠ SỞ TRẦN NHẬT VINH

printf("number is equal to 50");

else if(number==100){

printf("number is equal to 100");

else{

printf("number is not equal to 10, 50 or 100");

return 0;

Kết quả:

enter a number:4

number is not equal to 10, 50 or 100

Kết quả:

enter a number:50

number is equal to 50
LẬP TRÌNH CƠ SỞ TRẦN NHẬT VINH

Câu lệnh switch


Switch so sánh một biểu thức nguyên với một danh sách giá trị các số nguyên, các
hằng kí tự hoặc biểu thức hằng. Mỗi giá trị trong danh sách chính là một case (trường
hợp) trong khối lệnh của switch. Ngoài ra, trong khối lệnh switch còn có thể có một
default case (trường hợp mặc định) có thể có hoặc không. Mặt khác, trong mỗi trường
hợp còn chứa các khối lệnh chờ được thực thi.

Cú pháp của cấu trúc rẽ nhánh switch…case

Muốn sử dụng cấu trúc switch…case, bạn dùng cú pháp như sau:

switch(expression){

case value1:

//code to be executed;

break; //optional

case value2:

//code to be executed;

break; //optional

......

default:

//code to be executed if all cases are not matched;

Các qui tắc của lệnh switch tronng ngôn ngữ C


Biểu thức nguyên trong switch được tính toán và kiểm tra lần lượt với giá trị của từng
case. Đầu tiên, nó sẽ được so sánh với giá trị của case đầu tiên, nếu bằng nhau thì sẽ
thực hiện các lệnh (statement) trong case này cho đến khi nó gặp được từ khoá break.
Khi đó, cấu trúc switch…case kết thúc. Chương trình sẽ thực hiện tiếp những dòng lệnh
sau cấu trúc switch…case. Ngược lại, nếu như giá trị biểu thức nguyên không bằng giá
trị case đầu tiên thì nó sẽ tiếp tục so sánh đến giá trị của case thứ hai và tiếp tục thực
hiện như những bước trên. Giả sử, đến cuối cùng vẫn không tìm được giá trị bằng nó thì
các khối lệnh trong default sẽ được thực hiện nếu như có tồn tại default.
LẬP TRÌNH CƠ SỞ TRẦN NHẬT VINH

Chú ý:
1) Biểu thức trong lệnh switch phải là kiểu dữ liệu số
nguyên, ký tự.
2) case value phải là hằng số nguyên hoặc ký tự.
3) case value có thể sử dụng trong lệnh switch.
4) Lệnh break trong switch không bắt buộc.
Xem ví dụ sau: Kiểu dữ liệu nào hợp lệ trong lệnh switch:

int x,y,z;

char a,b;

float f;

Lệnh Switch đúng Lệnh Switch sai Lệnh Switch đúng

switch(x) switch(f) case 3;

switch(x>y) switch(x+2.5) case 'a';

switch(a+b-2) case 1+2;

switch(func(x,y)) case 'x'>'y';


LẬP TRÌNH CƠ SỞ TRẦN NHẬT VINH

Sơ đồ hoạt động của cấu trúc switch…case như sau:

Ví dụ lệnh switch :

#include<stdio.h>

int main(){

int number=0;

printf("enter a number:");
LẬP TRÌNH CƠ SỞ TRẦN NHẬT VINH

scanf("%d",&number);

switch(number){

case 10:

printf("number is equals to 10");

break;

case 50:

printf("number is equal to 50");

break;

case 100:

printf("number is equal to 100");

break;

default:

printf("number is not equal to 10, 50 or 100");

return 0;

Kết quả:

enter a number:4

number is not equal to 10, 50 or 100

Kết quả:

enter a number:50

number is equal to 50

Ví dụ không có lệnh break trong switch :

#include<stdio.h>

int main(){

int number=0;
LẬP TRÌNH CƠ SỞ TRẦN NHẬT VINH

printf("enter a number:");

scanf("%d",&number);

switch(number){

case 10:

printf("number is equals to 10\n");

case 50:

printf("number is equal to 50\n");

case 100:

printf("number is equal to 100\n");

default:

printf("number is not equal to 10, 50 or 100");

return 0;

Kết quả:

enter a number:10

number is equals to 10

number is equals to 50

number is equals to 100

number is not equal to 10, 50 or 100

Kết quả:

enter a number:50

number is equal to 50

number is equals to 100

number is not equal to 10, 50 or 100


LẬP TRÌNH CƠ SỞ TRẦN NHẬT VINH

You might also like