情報基礎 b lecture 8 takeshi tokuyama tohoku university graduate school of information sciences...

Post on 12-Jan-2016

212 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

情報基礎 B   Lecture 8

Takeshi TokuyamaTohoku University Graduate School of Information

SciencesSystem Information Sciences

Design and Analysis of Information Systems

PROGRAMMING VBADATA TYPE, IF-THEN-ELSE

Data type (Numeric)

Data type name

Data Value range Size

Byte Byte 0 to 255 1 byte

Integer Integer -32,768 to 32,767 2 byte

Long Long Integer-2,147,483,648 to

2,147,483,6474 byte

SinglePrecision floating point

numbers±3.4×1038 to ±1.4×10-45 4 byte

DoubleDouble precision floating

point numbers±1.8×10308 to ±4.9×10-

324 8 byte

Data type (Others)

Data type Value

Boolean True, False

String Values or numbers

Date 100/Jan/1 to 9999/Dec/31

CurrencyLarger than Long,

922,337,203,477.5808 to 922,337,203,685,477.5807

Variant  Numeric and non-numeric values

Variables

• A box to store a value• Variable declaration

– e.g. prepare a box “x” to store integer value– Dim x As Integer

• Declare a Integer type variable “x”

– Dim name As String• Declare a String type variable “name”

xx

namename

1 Sub yeartrans2()

2 ‘Transform Heisei into A.D.

3

4 Dim x As Integer

5 Dim y As Integer

6 Dim name As String

7

8 name = InputBox(“Enter your name.”)

9 x= InputBox(“Transform Heisei into A.D.” & name & “, enter Heisei year.”)

10 y = x + 1988

11

12 MsgBox name & “, Heisei” & x & “is A.D.” & y & ”.”

13

14 End Sub

Variables (Integer and String) and Data IO (InputBox and MsgBox)

“IF” in Excel Function

• Branch with “TRUE” or “FALSE”• IF(logical_test, value_if_true, value_if_false)

Logical formula or Cell number String with “” or just numbers

“IF” in Excel Function

• Grading program in previous lecture– Pass if score is more than 60, fail otherwise

• D16 = IF(A1>=60, “Pass”, “Fail”)

PassPass

FALSE

TRUE

FailFail

>=60

I f - Then - Else in VBA

Action1Action1

FALSE

TRUE

Action2Action2

logical_test

If logical_test Then

Else

End If

Action1Action1

Action2Action2

Grading Program

• Grade score if it is Pass or Fail– Pass if score is more than 60, fail otherwise

PassPass

FALSE

TRUE

FailFail

>=60

Grading Program1 Sub seiseki1()

2 ‘Grading Program

3

4 Dim score1 As Integer

5 Dim name1 As String

6

7 name1 = InputBox(“Enter your name.”)

8 score1 = InputBox(“Enter your score.”)

9

10 If score1 => 60 Then

11 MsgBox “Congratulations!” & name1 & ”, You passed the exam.”

12 Else

13 MsgBox name1 & ”, You failed the exam.”

14 End If

15

16 End Sub

Nesting “IF” in Excel Function

AATRUE

FALSETRUE

FALSETRUE

FALSETRUE

FALSEFALSE

FF

BB

CC

DD

>=90

>=80

>=70

>=60

Nesting “IF” in Excel Function

• Grade– A 100 > Score >= 90– B 90 > Score >= 80– C 80 > Score >= 70– D 70 > Score >= 60– F 60 > Score

=IF(B2>=90, ”A”, IF(B2>=80, ”B”, IF(B2>=70, ”C”, IF(B2>=60, ”D”, “F”))))

=IF(B2>=90, ”A”, IF(B2>=80, ”B”, IF(B2>=70, ”C”, IF(B2>=60, ”D”, “F”))))

If logical_test1 Then

ElseIf logical_test2 Then

Else

End If

I f - Then - Else in VBA

Action1Action1FALSE

TRUElogical_test1

logical_test2 Action2Action2

Action3Action3FALSE

TRUE

Action1

Action2

Action3

Grading Program If-Then-Else1 Sub seiseki2()2 ‘Grading Program If-Then-Else34 Dim score2 As Integer5 Dim name2 As String67 name2 = InputBox(“Enter your name.”)8 score2 = InputBox(“Enter your score.”)910 If   xxxxx     Then11 MsgBox name2 & ”, Your grade is A.”12 ElseIf   xxxxx   Then13 MsgBox name2 & ”, Your grade is B.”14 ElseIf   xxxxxx   Then15 MsgBox name2 & ”, Your grade is C.”16 ElseIf   xxxxx     Then17 MsgBox name2 & ”, Your grade is D.”18 Else19 MsgBox name2 & ”, Your grade is F.”20 End If2122 End Sub

Nesting If-Then-Else

AATRUE

FALSETRUE

FALSETRUE

FALSETRUE

FALSEFALSE

FF

BB

CC

DD

>=90

>=80

>=70

>=60

Grading Program nesting If-Then-Else1 Sub seiseki3()2 ‘Grading Program If-Then-Else34 Dim score3 As Integer5 Dim name3 As String67 name3 = InputBox(“Enter your name.”)8 score3 = InputBox(“Enter your score.”)910 If       Then11 If         Then12 If         Then13 If         Then14 MsgBox name3 & ”, Your grade is A.”15 Else16 MsgBox name3 & ”, Your grade is B.”17 End If18 Else19 MsgBox name2 & ”, Your grade is C.”20 End If21 Else22 MsgBox name2 & ”, Your grade is D.”23 End If24 Else25 MsgBox name2 & ”, Your grade is F.”26 End If2728 End Sub

top related