<< Chapter < Page Chapter >> Page >
Học xong chương này, sinh viên sẽ nắm được các vấn đề sau: - Khối lệnh trong C.- Cấu trúc rẽ nhánh. - Cấu trúc lựa chọn.- Cấu trúc vòng lặp. - Các câu lệnh “đặc biệt”.

Khối lệnh

Một dãy các khai báo cùng với các câu lệnh nằm trong cặp dấu ngoặc móc { và } được gọi là một khối lệnh.

Ví dụ 1:

{

char ten[30];

printf(“\n Nhap vao ten cua ban:”);

scanf(“%s”, ten);

printf(“\n Chao Ban %s”,ten);

}

Ví dụ 2:

#include<stdio.h>

#include<conio.h>

int main ()

{ /*đây là đầu khối*/

char ten[50];

printf("Xin cho biet ten cua ban !");

scanf("%s",ten);

getch();

return 0;

} /*đây là cuối khối*/

Một khối lệnh có thể chứa bên trong nó nhiều khối lệnh khác gọi là khối lệnh lồng nhau. Sự lồng nhau của các khối lệnh là không hạn chế.

Minh họa:

{

… lệnh;

{

… lệnh;

{

… lệnh;

}

… lệnh;

}

… lệnh;

}

Lưu ý về phạm vi tác động của biến trong khối lệnh lồng nhau:

- Trong các khối lệnh khác nhau hay các khối lệnh lồng nhau có thể khai báo các biến cùng tên.

Ví dụ 1:

{

… lệnh;

{

int a,b; /*biến a, b trong khối lệnh thứ nhất*/

… lệnh;

}

…lệnh;

{

int a,b;/*biến a,b trong khối lệnh thứ hai*/

… lệnh;

}

}

Ví dụ 2:

{

int a, b; /*biến a,b trong khối lệnh “bên ngoài”*/

… lệnh;

{

int a,b;/*biến a,b bên trong khối lệnh con*/

}

}

- Nếu một biến được khai báo bên ngoài khối lệnh và không trùng tên với biến bên trong khối lệnh thì nó cũng được sử dụng bên trong khối lệnh.

- Một khối lệnh con có thể sử dụng các biến bên ngoài, các lệnh bên ngoài không thể sử dụng các biến bên trong khối lệnh con.

Ví dụ:

{

int a, b, c;

…lệnh;

{

int c, d;

…lệnh;

}

}

Cấu trúc rẽ nhánh

Cấu trúc rẽ nhánh là một cấu trúc được dùng rất phổ biến trong các ngôn ngữ lập trình nói chung. trong c, có hai dạng: dạng không đầy đủ và dạng đầy đủ.

Dạng không đầy đủ

Cú pháp:

if (<Biểu thức điều kiện>)

<Công việc>

Lưu đồ cú pháp:

Bt đkiệnCông việcThoátĐúngSai

Giải thích:

<Công việc>được thể hiện bằng 1 câu lệnh hay 1 khối lệnh.

Kiểm tra Biểu thức điều kiện trước.

Nếu điều kiện đúng (!= 0) thì thực hiện câu lệnh hoặc khối lệnh liền sau điều kiện.

Nếu điều kiện sai thì bỏ qua lệnh hoặc khối lệnh liền sau điều kiện (những lệnh và khối lệnh sau đó vẫn được thực hiện bình thường vì nó không phụ thuộc vào điều kiện sau if).

Ví dụ 1:

Yêu cầu người thực hiện chương trình nhập vào một số thực a. In ra màn hình kết quả nghịch đảo của a khi a size 12{<>} {} 0.

#include<stdio.h>

#include<conio.h>

int main ()

{

float a;

printf("Nhap a = "); scanf("%f",&a);

if (a !=0 )

printf("Nghich dao cua %f la %f",a,1/a);

getch();

return 0;

}

Giải thích:

- Nếu chúng ta nhập vào a size 12{<>} {} 0 thì câu lệnh printf("Nghich dao cua %f la %f",a,1/a)được thực hiện, ngược lại câu lệnh này không được thực hiện.

- Lệnh getch() luôn luôn được thực hiện vì nó không phải là “lệnh liền sau” điều kiện if.

Ví dụ 2: Yêu cầu người chạy chương trình nhập vào giá trị của 2 số a và b, nếu a lớn hơn b thì in ra thông báo “Gia trị của a lớn hơn giá trị của b”, sau đó hiển thị giá trị cụ thể của 2 số lên màn hình.

#include<stdio.h>

#include<conio.h>

int main ()

{

int a,b;

printf("Nhap vao gia tri cua 2 so a, b!");

scanf("%d%d",&a,&b);

if (a>b)

{

printf("\n Gia tri cua a lon hon gia tri cua b");

printf("\n a=%d, b=%d",a,b);

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Co nuoi. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10760/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Co nuoi' conversation and receive update notifications?

Ask