Presentation is loading. Please wait.

Presentation is loading. Please wait.

情報基礎 B Lecture 8 Takeshi Tokuyama Tohoku University Graduate School of Information Sciences System Information Sciences Design and Analysis of Information.

Similar presentations


Presentation on theme: "情報基礎 B Lecture 8 Takeshi Tokuyama Tohoku University Graduate School of Information Sciences System Information Sciences Design and Analysis of Information."— Presentation transcript:

1 情報基礎 B Lecture 8 Takeshi Tokuyama Tohoku University Graduate School of Information Sciences System Information Sciences Design and Analysis of Information Systems

2 PROGRAMMING VBA DATA TYPE, IF-THEN-ELSE

3 Data type (Numeric) Data type name DataValue rangeSize Byte 0 to 2551 byte Integer -32,768 to 32,7672 byte LongLong Integer -2,147,483,648 to 2,147,483,647 4 byte Single Precision floating point numbers ±3.4×10 38 to ±1.4×10 - 45 4 byte Double Double precision floating point numbers ±1.8×10 308 to ±4.9×10 - 324 8 byte

4 Data type (Others) Data typeValue BooleanTrue, False StringValues or numbers Date100/Jan/1 to 9999/Dec/31 Currency Larger than Long, 922,337,203,477.5808 to 922,337,203,685,477.5807 Variant Numeric and non-numeric values

5 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” x x name

6 1Sub 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 14End Sub Variables (Integer and String) and Data IO (InputBox and MsgBox)

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

8 “IF” in Excel Function Grading program in previous lecture – Pass if score is more than 60, fail otherwise D16 = IF(A1>=60, “Pass”, “Fail”) Pass FALSE TRUE Fail >=60

9 I f - Then - Else in VBA Action1 FALSE TRUE Action2 logical_test If logical_test Then Else End If Action1 Action2

10 Grading Program Grade score if it is Pass or Fail – Pass if score is more than 60, fail otherwise Pass FALSE TRUE Fail >=60

11 Grading Program 1Sub 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 16End Sub

12 Nesting “IF” in Excel Function A A TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE F F B B C C D D >=90 >=80 >=70 >=60

13 Nesting “IF” in Excel Function Grade – A100 > 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”))))

14 If logical_test1 Then ElseIf logical_test2 Then Else End If I f - Then - Else in VBA Action1 FALSE TRUE logical_test1 logical_test2 Action2 Action3 FALSE TRUE Action1 Action2 Action3

15 Grading Program If-Then-Else 1Sub seiseki2() 2‘Grading Program If-Then-Else 3 4 Dim score2 As Integer 5 Dim name2 As String 6 7 name2 = InputBox(“Enter your name.”) 8 score2 = InputBox(“Enter your score.”) 9 10 If xxxxx Then 11 MsgBox name2 & ”, Your grade is A.” 12 ElseIf xxxxx Then 13 MsgBox name2 & ”, Your grade is B.” 14 ElseIf xxxxxx Then 15 MsgBox name2 & ”, Your grade is C.” 16 ElseIf xxxxx Then 17 MsgBox name2 & ”, Your grade is D.” 18 Else 19 MsgBox name2 & ”, Your grade is F.” 20 End If 21 22End Sub

16 Nesting If-Then-Else A A TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE F F B B C C D D >=90 >=80 >=70 >=60

17 Grading Program nesting If-Then-Else 1Sub seiseki3() 2‘Grading Program If-Then-Else 3 4 Dim score3 As Integer 5 Dim name3 As String 6 7 name3 = InputBox(“Enter your name.”) 8 score3 = InputBox(“Enter your score.”) 9 10 If Then 11 If Then 12 If Then 13 If Then 14 MsgBox name3 & ”, Your grade is A.” 15 Else 16 MsgBox name3 & ”, Your grade is B.” 17 End If 18 Else 19 MsgBox name2 & ”, Your grade is C.” 20 End If 21 Else 22 MsgBox name2 & ”, Your grade is D.” 23 End If 24 Else 25 MsgBox name2 & ”, Your grade is F.” 26 End If 27 28End Sub


Download ppt "情報基礎 B Lecture 8 Takeshi Tokuyama Tohoku University Graduate School of Information Sciences System Information Sciences Design and Analysis of Information."

Similar presentations


Ads by Google