<< Chapter < Page Chapter >> Page >
X Y X AND Y X OR Y NOT X
TRUE TRUE TRUE TRUE FALSE
TRUE FALSE FALSE TRUE FALSE
FALSE TRUE FALSE TRUE TRUE
FALSE FALSE FALSE FALSE TRUE

Câu lệnh

Một câu lệnh (statement) xác định một công việc mà chương trình phải thực hiện để xử lý dữ liệu đã được mô tả và khai báo. Các câu lệnh được ngăn cách với nhau bởi ký tự xuống dòng. Ký tự xuống dòng báo hiệu kết thúc một câu lệnh.

Lệnh gán

Cú pháp:

<Tên biến>=<Biểu thức>

Ví dụ:

Giả sử ta có khai báo sau:

Dim TodayTemp As Single, MinAge As Integer

Dim Sales As Single, NewSales As Single, FullName As String

Các lệnh sau gán giá trị cho các biến trên:

TodayTemp = 30.5

MinAge = 18

Sales = 200000

NewSales = Sales * 1.2

Giả sử người dùng cần nhập họ và tên vào ô nhập liệu TextBox có thuộc tính Name là txtName, câu lệnh dưới đây sẽ lưu giá trị của ô nhập liệu vào trong biến FullName:

FullName = txtName.Text

Lưu ý: Kiểu dữ liệu của biểu thức (vế phải của lệnh gán) phải phù hợp với biến ta cần gán trị.

Lệnh rẽ nhánh if

  • Một dòng lệnh:

If<điều kiện>Then<dòng lệnh>

  • Nhiều dòng lệnh:

If<điều kiện>Then

Các dòng lệnh

End If

Trong đó,<điều kiện>: biểu thức mà kết quả trả về kiểu Boolean.

Ý nghĩa câu lệnh: Các dòng lệnh hay dòng lệnh sẽ được thi hành nếu như điều kiện là đúng. Còn nếu như điều kiện là sai thì câu lệnh tiếp theo sau cấu trúc If ... Then được thi hành.

  • Dạng đầy đủ: If ... Then ... Else

If<điều kiện 1>Then

[Khối lệnh 1]

ElseIf<điều kiện 2>Then

[Khối lệnh 2]...

[Else

[Khối lệnh n]]

End If

VB sẽ kiểm tra các điều kiện, nếu điều kiện nào đúng thì khối lệnh tương ứng sẽ được thi hành. Ngược lại nếu không có điều kiện nào đúng thì khối lệnh sau từ khóa Else sẽ được thi hành.

Ví dụ:

If (TheColorYouLike = vbRed) Then

MsgBox "You are a lucky person"

ElseIf (TheColorYouLike = vbGreen) Then

MsgBox "You are a hopeful person"

ElseIf (TheColorYouLike = vbBlue) Then

MsgBox "You are a brave person"

ElseIf (TheColorYouLike = vbMagenta) Then

MsgBox "You are a sad person"

Else

MsgBox "You are an average person"

End If

Lệnh lựa chọn select case

Trong trường hợp có quá nhiều các điều kiện cần phải kiểm tra, nếu ta dùng cấu trúc rẽ nhánh If…Then thì đoạn lệnh không được trong sáng, khó kiểm tra, sửa đổi khi có sai sót. Ngược lại với cấu trúc Select…Case, biểu thức điều kiện sẽ được tính toán một lần vào đầu cấu trúc, sau đó VB sẽ so sánh kết quả với từng trường hợp (Case). Nếu bằng nó thi hành khối lệnh trong trường hợp (Case) đó.

Select Case<biểu thức kiểm tra>

Case<Danh sách kết quả biểu thức 1>

[Khối lệnh 1]

Case<Danh sách kết quả biểu thức 2>

[Khối lệnh 2]

.

.

.

[Case Else

[Khối lệnh n]]

End Select

Mỗi danh sách kết quả biểu thức sẽ chứa một hoặc nhiều giá trị. Trong trường hợp có nhiều giá trị thì mỗi giá trị cách nhau bởi dấu phẩy (,). Nếu có nhiều Case cùng thỏa điều kiện thì khối lệnh của Case đầu tiên sẽ được thực hiện.

Ví dụ của lệnh rẽ nhánh If…Then ở trên có thể viết như sau:

Select Case TheColorYouLike

Case vbRed

MsgBox "You are a lucky person"

Case vbGreen

MsgBox "You are a hopeful person"

Case vbBlue

MsgBox "You are a brave person"

Case vbMagenta

MsgBox "You are a sad person"

Case Else

MsgBox "You are an average person"

Questions & Answers

Ayele, K., 2003. Introductory Economics, 3rd ed., Addis Ababa.
Widad Reply
can you send the book attached ?
Ariel
?
Ariel
What is economics
Widad Reply
the study of how humans make choices under conditions of scarcity
AI-Robot
U(x,y) = (x×y)1/2 find mu of x for y
Desalegn Reply
U(x,y) = (x×y)1/2 find mu of x for y
Desalegn
what is ecnomics
Jan Reply
this is the study of how the society manages it's scarce resources
Belonwu
what is macroeconomic
John Reply
macroeconomic is the branch of economics which studies actions, scale, activities and behaviour of the aggregate economy as a whole.
husaini
etc
husaini
difference between firm and industry
husaini Reply
what's the difference between a firm and an industry
Abdul
firm is the unit which transform inputs to output where as industry contain combination of firms with similar production 😅😅
Abdulraufu
Suppose the demand function that a firm faces shifted from Qd  120 3P to Qd  90  3P and the supply function has shifted from QS  20  2P to QS 10  2P . a) Find the effect of this change on price and quantity. b) Which of the changes in demand and supply is higher?
Toofiq Reply
explain standard reason why economic is a science
innocent Reply
factors influencing supply
Petrus Reply
what is economic.
Milan Reply
scares means__________________ends resources. unlimited
Jan
economics is a science that studies human behaviour as a relationship b/w ends and scares means which have alternative uses
Jan
calculate the profit maximizing for demand and supply
Zarshad Reply
Why qualify 28 supplies
Milan
what are explicit costs
Nomsa Reply
out-of-pocket costs for a firm, for example, payments for wages and salaries, rent, or materials
AI-Robot
concepts of supply in microeconomics
David Reply
economic overview notes
Amahle Reply
identify a demand and a supply curve
Salome Reply
i don't know
Parul
there's a difference
Aryan
Demand curve shows that how supply and others conditions affect on demand of a particular thing and what percent demand increase whith increase of supply of goods
Israr
Hi Sir please how do u calculate Cross elastic demand and income elastic demand?
Abari
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, Giáo trình visual basic. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10777/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Giáo trình visual basic' conversation and receive update notifications?

Ask