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

Slides:



Advertisements
Similar presentations
Driving Test 1 Marking Scheme Focus on five areas to pass driving test 1.
Advertisements

Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
5.04 Apply Decision Making Structures
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
1.
30/04/ Selection Nested If structures & Complex Multiple Conditions.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
CS0004: Introduction to Programming Repetition – Do Loops.
Programming in Visual Basic
Objectives In this chapter, you will learn about:
Val Function A Function performs an action and returns a value The expression to operate upon, known as the argument, (or multiple arguments), must be.
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming.
1 Visual Basic for Applications (VBA) for Excel Prof. Yitzchak Rosenthal.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Chapter 4: The Selection Process in Visual Basic.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
Access VBA Programming for Beginners - Class 2 - by Patrick Lasu
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
CS105 Fall CS105 Lab 3 – Excel: The IF Function Announcements MP 1 will be released on Monday 9/14, due Monday 9/28 by 9pm Midterm 1 on Tuesday 9/29.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
MS Visual Basic Applications Walter Milner. Event-driven programming Standard approach for GUIs Contrast with old character interfaces – program determines.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Visual Basic Programming
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
22/11/ Selection If selection construct.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
ME 142 Engineering Computation I Using Subroutines Effectively.
# 1# 1 Using Procedures and Functions What is a procedure? What is a sub procedure (subroutine)? Built-in functions in your code What is a function? CS.
4-1 Chapter 4 The Selection Process in VB.NET. 4-2 Learning Objectives Understand the importance of the selection process in programming. Describe the.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Pay Example (PFirst98) Please use speaker notes for additional information!
ME 142 Engineering Computation I Using Subroutines Effectively.
# 1# 1 What is a variable that you create? What is a constant that you create? What is an intrinsic (built-in) constant? What variables are built in?
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
31/01/ Selection If selection construct.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
# 1# 1 Nested If Statements in VBA What is a compound condition that we evaluate? What is a Nested If statement? How do we use ElseIf? CS 105 Spring 2010.
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
Controlling Program Flow with Decision Structures.
Microsoft Visual Basic 2012 CHAPTER FIVE Decision Structures.
AVCE ICT – Unit 7 - Programming Session 12 - Debugging.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
MIC305 Week 6 Beyond controls Review of properties Differences with VB6: using classes and instances Programming constructs.
1 4.2 Selection Logical Operators. 2 Learning Objectives Explain how the logical operator AND Boolean statements works. Directly testing if text boxes.
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Subroutines (PrArith, Math,projCP1, PrAdrProc, PrAdrProcFunc) Please use speaker notes for additional information!
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
VBA - Excel VBA is Visual Basic for Applications
Scripts & Functions Scripts and functions are contained in .m-files
CHAPTER FIVE Decision Structures.
More Selections BIS1523 – Lecture 9.
Aliza Tech Technology Consulting Services
Department Array in Visual Basic
Conditions and Ifs BIS1523 – Lecture 8.
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
If selection construct
If selection construct
CS285 Introduction - Visual Basic
Consult America Technology Consulting Services
Presentation transcript:

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!! Exam-only Review on Sunday from 3pm to 5pm and 7pm to 9pm CS105 Fall 2009

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

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

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 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 List of frequently used Variable Types TypeDescriptionPrefix Integer Stores integers between the range of …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

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

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

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

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

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

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

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

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

UserForm demo CS105 Fall

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