if statemet1
DESCRIPTION
TRANSCRIPT
The Selection Structure
The if…then…Else and Select Case Statement
Paramet Damchoo , lecturer Business computer DepartmentManagement science Faculty Surattani ratjabhat University
สร้�างปั�ญญา ศร้�ทธาความดี� มหาวิ�ทยาลัยแห�งเม�องคนดี�
The Selection Structure
You use the selection structure , also called the decision structure, when you want a program to make a decision or comparison and then, based on the result of that decision or comparison, to select one of two paths. You will need to use the selection structure to complete the Math application.
Figure 1-1: Decisions you might need to make today
Coding the Selection Structure in Visual Basic
• You use the if …then… Else statement to code the selection structure in Visual Basic. The syntax of the If…Then…Else statement is shown in figure 1-2
Figure 1-2: Syntax of If…Then…Else…Statement
If Condition Then
[instructions when the condition is true]
Else
[instructions when the condition is false]
End If
Figure1-3: lists the relational operators you can use in the If…Then..Else statement ‘s condition
Relational Operator Meaning
= Equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
<> Not equal to
Logical Operation
False
False
FalseTrue
FalseFalse
False
FalseFalse
False
FalseFalse
False
TrueTrue
False
TrueTrueFalse
FalseTrue
False
TrueFalse
TrueTrueFalse
True
FalseFalse
True
FalseTrue
TrueTrue
False
TrueTrue
True
TrueTrue
True
AND เปั�น จร้�ง กร้ณี�เดี�ยว ค�อ จร้�ง จร้�ง
OR เปั�นเท�จ กร้ณี�เดี�ยว ค�อ เท�จ เท�จ
XOR ต่�างก�นไดี�จร้�ง NOT ต่ร้งก�นข้�าม
NotNot
XORXORORORANDAND
Example 1
บริ�ษัทหน��งต้�องการิค�านวิณภาษั�เง�นไดี� (Tax) ของพนกงาน โดียพ�จาริณา
จากเง�นเดี�อน (Salary) ท��ไดี�ริบ หากเง�นเดี�อน เดี�อนลัะ 20000
บาทข�&นไปค�ดีภาษั� 15% แต้�ถ้�าไดี�เง�นเดี�อนต้��ากวิ�า 20000 บาท จะ
ค�ดีภาษั� 10%
Condition
Salary
Tax = Salary * 15% Tax = Salary * 10%
Salary >= 20000 Salary < 20000
เคริ��องหมายต้ริงข�าม
CONDITION
• ในการิเข�ยน condition ของค�าสั่�งแบบม�ทางเลั�อกน&น ในกริณ�ท��เป+นแบบสั่องทางเลั�อก จะม�เพ�ยง 1 Condition ต้าม syntax
If Condition Then [instructions when the
condition is true] Else
[instructions when the condition is false]
End If
IF Salary >= 20000 THEN
Tax = Salary * 15%ELSE
Tax = Salary * 10%
IF Salary >= 20000 THEN
Tax = Salary * 15%ELSE
Tax = Salary * 10%
IF Salary < 20000 THEN
Tax = Salary * 10%
ELSE Tax = Salary * 15%
IF Salary < 20000 THEN
Tax = Salary * 10%
ELSE Tax = Salary * 15%
CONDITION
Problem Problem Definition
Process Tax = Salary * 15% Tax = Salary * 10%
Input
Salary (เง�นเดี�อน ) ชน�ดีม-ลั Single
Output Tax (ภาษั�) ชน�ดีข�อม-ลั Single
Algorithm 1. เริ��มต้�น2. ริบค�า เง�นเดี�อน3. ต้ริวิจสั่อบเง��อนไข
ถ้�า เง�นเดี�อน > = 20000 แลั�วิท�า ภาษั� = เง�นเดี�อน x 15%
ม�ฉะน&นแลั�วิ ภาษั� = เง�นเดี�อน x 10%
4. แสั่ดีงค�า ภาษั�5. จบการิท�างาน
Flowchart
1 . เริ��มต้�น2. ริบค�า เง�นเดี�อน3. ต้ริวิจสั่อบเง��อนไข
ถ้�า เง�นเดี�อน > = 20000 แล้�วท%า
ภาษี� = เง�นเดี�อน x 15% ม�ฉะน�*นแล้�ว ภาษี� = เง�นเดี�อน x 10% 4. แสั่ดีงค�า ภาษั� 5. จบการิท�างาน
START
INPUT Salary
Salary >=2000
0
ภาษี� = เง�นเดี�อน x 15%
ภาษี� = เง�นเดี�อน x 10%
OUTPUT Salary
STOP
Pseudo codeBEGIN
Input SalaryIF Salary >=20000 THENTax =Salary*(15/100) ELSE Tax =Salary*(10/100)END IF
DISPLAY Tax END
การิใช� Message Box
ต้วิอย�าง Message Box
3. Title3. Title
2. ค�าคงท�+ปั,-มControl Msgbox
2. ค�าคงท�� Icon
1. Promt
Syntax MessageBox
Msgbox Promt , Icon+Control+Default ,Title
สั่ามาริถ้สั่ลับต้�าแหน�งกนไดี�สั่ามาริถ้สั่ลับ
ต้�าแหน�งกนไดี�
ค�าคงท�� Icon
VbCritical
ไอคอนท��ปริากฏค�อ
VbExclamation
ไอคอนท��ปริากฏค�อ
VbInformation
ไอคอนท��ปริากฏค�อ
Vbquestion
ไอคอนท��ปริากฏค�อ
ค�าคงท��ป01มควิบค0มVbOKOnly ป01มปริากฏ
ค�อVbokCancel
ป01มปริากฏค�อ
VbYesNo ป01มปริากฏค�อ
VbYesNoCancel
ป01มปริากฏค�อ
VbAbortRetryIgnore
ป01มปริากฏค�อ
VbRetryCancel
ป01มปริากฏค�อ
coding
Msgbox
“ค0ณต้�องการิออกจากโปริแกริมใช� ไหม”
,VbInformation
+ VbYesnoCancel +
VbDefault2 , “ต้วิอย�างของMSGBOX”
Net IF statement
• จะใช�ในกริณ� ท��ม�ทางเลั�อกมากกวิ�า 2 ทางเลั�อก โดียท��ค�าสั่�ง IF น&น จะม�ไดี�
เพ�ยงสั่องทางเลั�อกเท�าน&น (True/False)
Syntax
• IF Condition-1 THEN
[instruction when condition is true]
ELSEIF Condition-2 THEN
[instruction when condition is true]
ELSE
[instruction when condition is false]
END IF
Example-1
• ให�ริบชนป2ของนกศึ�กษัาแลั�วิให�แสั่ดีงข�อควิามเพ��อแสั่ดีงวิ�านกศึ�กษัาม�สั่ถ้านภาพเป+นนกศึ�กษัาริะดีบใดี
ช&นป2 ข�อควิาม1 Freshman2 Sophomore3 Junior4 Seniorอ��นๆ Not is student
ConditionIF Class = 1 THEN
OUTPUT “Freshman “ ELSEIF Class = 2 THEN
OUTPUT “Sophomore”ELSEIF Class = 3 THEN
OUTPUT “Junior”ELSEIF Class = 4 THEN
OUTPUT “Senior” ELSE
OUTPUT “Not is Student”
Problem Definition
• INPUT– Class (ช&นป2)
Integer
• PROCESS– (condition ท&งหมดี)
• OUTPUT– แสั่ดีงข�อควิาม
Example-2
ให�นกศึ�กษัาเข�ยนโปริแกริมของริ�านหนงสั่�อ (Book Shop) ทางริ�านก�าหนดีม�หนงสั่�อ 2 ปริะเภท ค�อ หนงสั่�อพ�มพ5 แลัะ น�ต้ยสั่าริโดียก�าหนดีเง��อนไขดีงน�& ถ้�าซื้�&อหนงสั่�อพ�มพ5 (News paper) จะไม�ม�สั่�วินลัดี ถ้�าซื้�&อน�ต้ยสั่าริ(Magazine) จะให�สั่�วินลัดี 5% ถ้�าเป+นหนงสั่�ออ��นๆ ให�แสั่ดีงข�อควิามวิ�า Not Sale ก�าหนดีให� ริบต้วิเลัข แทน ปริะเภทหนงสั่�อ ค�อ 1 หมายถ้�ง News paper 2 ค�อ Magazine โดียให�ริบ จ�านวินหนงสั่�อ แลัะริาคาหนงสั่�อ ทาง Textbox
ม�ค�าถ้ามหริ�อป1าวิๆ
ๆๆ
ม�ค�าถ้ามหริ�อป1าวิๆ
ๆๆ