Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!

Similar presentations


Presentation on theme: "1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!"— Presentation transcript:

1 1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!! (cs105ta4@cs.uiuc.edu) Exam-only Review on Sunday from 3pm to 5pm and 7pm to 9pm CS105 Fall 2009

2 2 Objectives Learn how to define and use variables Learn simple error checking techniques Learn If and Nested-If Statements in VBA CS105 Fall 2009

3 3 Continue Ticket Application Last week, we made an interface where users can enter their: Name Age Calculate the ticket discount This week, we continue adding more features: Prevent errors by validating user’s name and age Based on more complex data calculate discount CS105 Fall 2009

4 Why do we need to check for errors? Expect users to make mistakes ANYWHERE Example: Click on cmdAge, and enter “twenty”, and click on cmdCalculate Our mission is to prevent this from happening 4 Ignore the error Find out what’s wrong and fix it CS105 Fall 2009

5 First, let’s check for an empty name We want to prevent users from entering an empty name. Take a look at cmdName_Click sub-procedure We have: Range("D11").Value = InputBox("Enter Name:", "Name", "John") What we want is: 5 Range("D11").Value Variable Check for errors here CS105 Fall 2009

6 6 Variables and Types Variables are temporary containers that hold data: Variables have a Type. Types define exactly what kind of data a variable can hold. A List of Variable Types can be found on the next slide. In CS105, we follow a naming convention for variable names. Please refer to the course guide for more details.naming convention strGreeting “Hello, I am a String” intNumber 17 CS105 Fall 2009

7 7 List of frequently used Variable Types TypeDescriptionPrefix Integer Stores integers between the range of -32768…32767 int StringStores strings, or a sequence of charactersstr VariantStore any typesvnt BooleanStore logical data that is either True or Falsebln DoubleStores floating point numbersdbl CS105 Fall 2009

8 Option Explicit : taken from lecture notes Programmers make mistakes, therefore we require students to use Option Explicit and correct naming You find this on the Tools Menu/Options. With Option Explicit you must use Dim and Const

9 9 Declare a variable and write assignments In this course, before you can use a variable, you MUST declare it. Dim vntName As Variant In the cmdName_Click sub procedure, use the declared variable to separate the existing assignment Does it work exactly the same as before? vntName = InputBox("Enter Name:", "Name", "John") Range("D11").Value = vntName Range("D11").Value Variable

10 10 Error Checking Flowchart Error? Display error message Continue on to next statement True False Exit What kind of statement would you use to express this flow chart? CS105 Fall 2009

11 11 Error in the Name? If vnt Name = "" Then MsgBox "Please enter a valid name", vbCritical, "Error" Exit Sub MsgBox “Won’t appear", vbCritical, "Error" End If CS105 Fall 2009 Range("D11").Value A Variable Check for errors here All code below this point will be ignored An empty string

12 Checking for valid age Let’s find more possible errors, and fix them Click on cmdAge, and enter “twenty” 12 CS105 Fall 2009 12 Ignore the error Find out what’s wrong and fix it

13 Debugging Mode When a line is highlighted in yellow, you’re in debugging mode The yellow line often tells you where the errors are Your worksheet will stop working (e.g. you won’t be able to click on the button, etc) To exit Debugging Mode Press the “reset” button located at the top middle section 13CS105 Fall 2009

14 14 Error Checking for Age If we get an error because of “Type-mismatch”, that means we need to verify the user entered a number We do this the same way we checked for an empty name Declare a variable of type VARIANT Dim vntAge As Variant Assign the output of InputBox to vntAge vntAge = InputBox("Enter Age:","Age",18) CS105 Fall 2009

15 cmdAge_Click() Private Sub cmdAge_Click() Dim vntAge As Variant vntAge = InputBox(“Enter age”, “Age”, “18”) If Not IsNumeric(vntAge) Then MsgBox "Please enter a valid age", vbCritical, "Error" Exit Sub End If Range("D12").Value = vntAge End Sub 15 IsNumeric( ) is a function that returns True if the argument is a number CS105 Fall 2009

16 Calculate the discount based on Age CS105 Fall 200916 Age <= 12 Discount = $15 Discount = $0 True False Age >= 65 Discount = $30 True False

17 17 If, ElseIf and Else To check multiple conditions is use If with Else and ElseIf statement. If Then ElseIf Then Else End If How many If statements in the above code? One CS105 Fall 2009 If vntAge <= 12 Then Range("D13").Value = 15 ElseIf vntAge >= 65 Then Range("D13").Value = 30 Else Range("D13").Value = 0 End If

18 Discount for a “Lucky” drawer The manager wants to give a free ticket to anybody named “Lucky”. CS105 Fall 200918 Name= “Lucky” Discount = “ticket price” Calculate discount based on Age as before True False This is another If statement

19 19 Nested If Nested IF is an If statement that is inside another If statement. If Then ElseIf Else End If CS105 Fall 2009 If Then Else End If The outer IF The inner IF Could nest here Or here

20 20 NestedIf for Discount … Dim vntName As Variant Dim vntAge As Variant vntName = Range("D11").Value vntAge = Range("D12").Value If vntName = “Lucky" Then Range("D13").Value = Range("D7").Value Else If vntAge <= 12 Then Range("D13").Value = 15 ElseIf vntAge >= 65 Then Range("D13").Value = 30 Else Range("D13").Value = 0 End If … CS105 Fall 2009

21 21 What you should know? What are variables and why we need them? How to declare a variable How to write If/Nested-If/ElseIf statement in VBA? How to do error-checking in VBA? What is IsNumeric() function? CS105 Fall 2009

22 UserForm demo CS105 Fall 200922

23 Exercises Assuming the code in slide 20 is executed. If cell D11 contains “Lucky” and cell D12 contains “12”, what value will be put in D13? After the code is executed, D13 contains 30, can you give a pair of values for D11 and D12 that might have produced the above result. If cell D11 contains “12” and cell D12 contains “lucky”, which line of code will produce an error? CS105 Fall 200923


Download ppt "1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!"

Similar presentations


Ads by Google