<< 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

if three forces F1.f2 .f3 act at a point on a Cartesian plane in the daigram .....so if the question says write down the x and y components ..... I really don't understand
Syamthanda Reply
hey , can you please explain oxidation reaction & redox ?
Boitumelo Reply
hey , can you please explain oxidation reaction and redox ?
Boitumelo
for grade 12 or grade 11?
Sibulele
the value of V1 and V2
Tumelo Reply
advantages of electrons in a circuit
Rethabile Reply
we're do you find electromagnetism past papers
Ntombifuthi
what a normal force
Tholulwazi Reply
it is the force or component of the force that the surface exert on an object incontact with it and which acts perpendicular to the surface
Sihle
what is physics?
Petrus Reply
what is the half reaction of Potassium and chlorine
Anna Reply
how to calculate coefficient of static friction
Lisa Reply
how to calculate static friction
Lisa
How to calculate a current
Tumelo
how to calculate the magnitude of horizontal component of the applied force
Mogano
How to calculate force
Monambi
a structure of a thermocouple used to measure inner temperature
Anna Reply
a fixed gas of a mass is held at standard pressure temperature of 15 degrees Celsius .Calculate the temperature of the gas in Celsius if the pressure is changed to 2×10 to the power 4
Amahle Reply
How is energy being used in bonding?
Raymond Reply
what is acceleration
Syamthanda Reply
a rate of change in velocity of an object whith respect to time
Khuthadzo
how can we find the moment of torque of a circular object
Kidist
Acceleration is a rate of change in velocity.
Justice
t =r×f
Khuthadzo
how to calculate tension by substitution
Precious Reply
hi
Shongi
hi
Leago
use fnet method. how many obects are being calculated ?
Khuthadzo
khuthadzo hii
Hulisani
how to calculate acceleration and tension force
Lungile Reply
you use Fnet equals ma , newtoms second law formula
Masego
please help me with vectors in two dimensions
Mulaudzi Reply
how to calculate normal force
Mulaudzi
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