if statemet1

34
The Selection Structure The if…then…Else and Select Case Statement Paramet Damchoo , lecturer Business computer Department Management science Faculty Surattani ratjabhat University สสสสสสสสสส สสสสสสสสสส สส มมมมมมมมมมมมมมมมมมมม มมมม

Upload: sup11

Post on 22-Apr-2015

622 views

Category:

Technology


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: If statemet1

The Selection Structure

The if…then…Else and Select Case Statement

Paramet Damchoo , lecturer Business computer DepartmentManagement science Faculty Surattani ratjabhat University

สร้�างปั�ญญา ศร้�ทธาความดี� มหาวิ�ทยาลัยแห�งเม�องคนดี�

Page 2: If statemet1

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.

Page 3: If statemet1

Figure 1-1: Decisions you might need to make today

Page 4: If statemet1

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

Page 5: If statemet1

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

Page 6: If statemet1

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

Page 7: If statemet1

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

Page 8: If statemet1

Example 1

บริ�ษัทหน��งต้�องการิค�านวิณภาษั�เง�นไดี� (Tax) ของพนกงาน โดียพ�จาริณา

จากเง�นเดี�อน (Salary) ท��ไดี�ริบ หากเง�นเดี�อน เดี�อนลัะ 20000

บาทข�&นไปค�ดีภาษั� 15% แต้�ถ้�าไดี�เง�นเดี�อนต้��ากวิ�า 20000 บาท จะ

ค�ดีภาษั� 10%

Page 9: If statemet1

Condition

Salary

Tax = Salary * 15% Tax = Salary * 10%

Salary >= 20000 Salary < 20000

เคริ��องหมายต้ริงข�าม

CONDITION

Page 10: If statemet1

• ในการิเข�ยน condition ของค�าสั่�งแบบม�ทางเลั�อกน&น ในกริณ�ท��เป+นแบบสั่องทางเลั�อก จะม�เพ�ยง 1 Condition ต้าม syntax

If Condition Then [instructions when the

condition is true] Else

[instructions when the condition is false]

End If

Page 11: If statemet1

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

Page 12: If statemet1

Problem Problem Definition

Process Tax = Salary * 15% Tax = Salary * 10%

Input

Salary (เง�นเดี�อน ) ชน�ดีม-ลั Single

Output Tax (ภาษั�) ชน�ดีข�อม-ลั Single

Page 13: If statemet1

Algorithm 1. เริ��มต้�น2. ริบค�า เง�นเดี�อน3. ต้ริวิจสั่อบเง��อนไข

ถ้�า เง�นเดี�อน > = 20000 แลั�วิท�า ภาษั� = เง�นเดี�อน x 15%

ม�ฉะน&นแลั�วิ ภาษั� = เง�นเดี�อน x 10%

4. แสั่ดีงค�า ภาษั�5. จบการิท�างาน

Page 14: If statemet1

Flowchart

1 . เริ��มต้�น2. ริบค�า เง�นเดี�อน3. ต้ริวิจสั่อบเง��อนไข

ถ้�า เง�นเดี�อน > = 20000 แล้�วท%า

ภาษี� = เง�นเดี�อน x 15% ม�ฉะน�*นแล้�ว ภาษี� = เง�นเดี�อน x 10% 4. แสั่ดีงค�า ภาษั� 5. จบการิท�างาน

START

INPUT Salary

Salary >=2000

0

ภาษี� = เง�นเดี�อน x 15%

ภาษี� = เง�นเดี�อน x 10%

OUTPUT Salary

STOP

Page 15: If statemet1

Pseudo codeBEGIN

Input SalaryIF Salary >=20000 THENTax =Salary*(15/100) ELSE Tax =Salary*(10/100)END IF

DISPLAY Tax END

Page 16: If statemet1

การิใช� Message Box

ต้วิอย�าง Message Box

3. Title3. Title

2. ค�าคงท�+ปั,-มControl Msgbox

2. ค�าคงท�� Icon

1. Promt

Page 17: If statemet1

Syntax MessageBox

Msgbox Promt , Icon+Control+Default ,Title

สั่ามาริถ้สั่ลับต้�าแหน�งกนไดี�สั่ามาริถ้สั่ลับ

ต้�าแหน�งกนไดี�

Page 18: If statemet1

ค�าคงท�� Icon

VbCritical

ไอคอนท��ปริากฏค�อ

VbExclamation

ไอคอนท��ปริากฏค�อ

VbInformation

ไอคอนท��ปริากฏค�อ

Vbquestion

ไอคอนท��ปริากฏค�อ

Page 19: If statemet1

ค�าคงท��ป01มควิบค0มVbOKOnly ป01มปริากฏ

ค�อVbokCancel

ป01มปริากฏค�อ

VbYesNo ป01มปริากฏค�อ

VbYesNoCancel

ป01มปริากฏค�อ

VbAbortRetryIgnore

ป01มปริากฏค�อ

VbRetryCancel

ป01มปริากฏค�อ

Page 20: If statemet1

coding

Msgbox

“ค0ณต้�องการิออกจากโปริแกริมใช� ไหม”

,VbInformation

+ VbYesnoCancel +

VbDefault2 , “ต้วิอย�างของMSGBOX”

Page 21: If statemet1

Net IF statement

• จะใช�ในกริณ� ท��ม�ทางเลั�อกมากกวิ�า 2 ทางเลั�อก โดียท��ค�าสั่�ง IF น&น จะม�ไดี�

เพ�ยงสั่องทางเลั�อกเท�าน&น (True/False)

Page 22: If statemet1

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

Page 23: If statemet1

Example-1

• ให�ริบชนป2ของนกศึ�กษัาแลั�วิให�แสั่ดีงข�อควิามเพ��อแสั่ดีงวิ�านกศึ�กษัาม�สั่ถ้านภาพเป+นนกศึ�กษัาริะดีบใดี

ช&นป2 ข�อควิาม1 Freshman2 Sophomore3 Junior4 Seniorอ��นๆ Not is student

Page 24: If statemet1

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”

Page 25: If statemet1

Problem Definition

• INPUT– Class (ช&นป2)

Integer

• PROCESS– (condition ท&งหมดี)

• OUTPUT– แสั่ดีงข�อควิาม

Page 26: If statemet1
Page 27: If statemet1
Page 28: If statemet1
Page 29: If statemet1

Example-2

ให�นกศึ�กษัาเข�ยนโปริแกริมของริ�านหนงสั่�อ (Book Shop) ทางริ�านก�าหนดีม�หนงสั่�อ 2 ปริะเภท ค�อ หนงสั่�อพ�มพ5 แลัะ น�ต้ยสั่าริโดียก�าหนดีเง��อนไขดีงน�& ถ้�าซื้�&อหนงสั่�อพ�มพ5 (News paper) จะไม�ม�สั่�วินลัดี ถ้�าซื้�&อน�ต้ยสั่าริ(Magazine) จะให�สั่�วินลัดี 5% ถ้�าเป+นหนงสั่�ออ��นๆ ให�แสั่ดีงข�อควิามวิ�า Not Sale ก�าหนดีให� ริบต้วิเลัข แทน ปริะเภทหนงสั่�อ ค�อ 1 หมายถ้�ง News paper 2 ค�อ Magazine โดียให�ริบ จ�านวินหนงสั่�อ แลัะริาคาหนงสั่�อ ทาง Textbox

Page 30: If statemet1
Page 31: If statemet1
Page 32: If statemet1
Page 33: If statemet1

ม�ค�าถ้ามหริ�อป1าวิๆ

ๆๆ

ม�ค�าถ้ามหริ�อป1าวิๆ

ๆๆ

Page 34: If statemet1