1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,

Slides:



Advertisements
Similar presentations
Variables, Constants &Computation T o i n s e r t y o u r c o m p a n y l o g o o n t h i s s l i d e F r o m t h e I n s e r t M n u e l e c t P i c.
Advertisements

Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
1 CS 106, Winter 2009 Class 10, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
1 CS 106, Winter 2009 Class 5, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
8 November Forms and JavaScript. Types of Inputs Radio Buttons (select one of a list) Checkbox (select as many as wanted) Text inputs (user types text)
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
IS437: Fall 2004 Instructor: Dr. Boris Jukic Data Types, Constants, Variables, Scope, Conversion.
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
Chapter Three Using Variables and Constants Programming with Microsoft Visual Basic th Edition.
Variables & Math Operators CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Adding Controls to User Forms. Adding Controls A user form isn’t much use without some controls We’re going to add controls and write code for them Note.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Chapter 3: Using Variables and Constants
Programming with Microsoft Visual Basic th Edition CHAPTER THREE USING VARIABLES AND CONSTANTS.
CS0004: Introduction to Programming Input and Output.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
CIS162AD - C# Decision Statements 04_decisions.ppt.
Intrinsic Functions Pre-coded Functions Used to improve developer productivity Broad Range of Activities Math calculations Time/Date functions String.
National Diploma Unit 4 Introduction to Software Development Data types, variables and constants.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
3 - Variables Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Numbers continued The Integer Data Type Multiple Declarations Parentheses Three Types of Errors.
Applications Development
Data Types and Variables. Data Type! Computers are all about Data! Data can be in the form of Text Dates Sounds Pictures.
Chapter 8 - Visual Basic Schneider
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
1 Chapter 3 – Examples The examples from chapter 3, combining the data types, variables, expressions, assignments, functions and methods with Windows controls.
# 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?
Hungarian Notation A must in this course Every object used MUST be renamed including the form(s) using the following rules Form  frmFormName E.g. frmTemperature.
Tutorial 3: Using Variables and Constants1 Tutorial 3 Using Variables and Constants.
1 CS 106 Computing Fundamentals II Chapter 210 “Adding Controls to User Forms” Herbert G. Mayer, PSU CS Status 7/4/2013 Initial content copied verbatim.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
CECS 5020 Computers in Education Visual Basic Variables and Constants.
1 Writing Software Kashef Mughal. 2 Algorithms  The term algorithm (pronounced AL-go-rith-um) is a procedure or formula for solving a problem.  An Algorithm.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
Basic Java Syntax Comments Basic data types Operators and assignment.
Slide 1 Controls v Control naming convention –Label: lblName –Command Button: cmdName –Text Box: txtName.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Controlling Program Flow with Decision Structures.
CIS 338: VB Variables Dr. Ralph D. Westfall April, 2011.
Introduction to Programming Python Lab 8: Loops 26 February PythonLab8 lecture slides.ppt Ping Brennan
Making Interactive Programs with Visual Basic .NET
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
Programming with Microsoft Visual Basic 2012 Chapter 3: Using Variables and Constants.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
A variable is a name for a value stored in memory.
Data Types, Arithmetic Operations
Introduction to Programming
Other Kinds of Arrays Chapter 11
Variables and Arithmetic Operations
CIS16 Application Development Programming with Visual Basic
Coding Concepts (Data- Types)
CHAPTER FOUR VARIABLES AND CONSTANTS
Unit 3: Variables in Java
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Presentation transcript:

1 CS 106, Winter 2009 Class 6, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,

2 Numeric Data Types Double – Covers a huge range of values – Used for almost all computations – Can lose some accuracy in long computations (not a concern for the programs we do in this class) Integer – Used for counting – Always precise – Smaller range of values than Double

3 Types of Results Adding two integers gives an integer, adding two doubles gives a double Dividing two integers using / gives a double! Which is why we have \ and mod: 14 \ 4 = 3, and 14 mod 4 = 2 The exponential function always gives a double as a result VB will change integers into doubles in mixed computations, but not the reverse

4 String Data Type Besides numbers, strings are the other kind of data we will usually be working with Strings are strings of characters, so let’s look at characters first

5 Characters 256 characters used to be enough for upper and lower case letters, numbers, punctuation, and a few oddities The 8-bit byte was the standard character size, in the ASCII code Now we want Chinese, Arabic, etc so the new system has 16 bits, allowing for 2 16 = 65,536 characters. The representation system is called Unicode. It’s very important to have standard representations for characters!

6 Strings A string is a sequence of characters, typically stored in several words as a length and a list of characters (details vary) Strings are declared as follows Dim professor as String professor = “Cindy Brown”

7 String Operations: Concatenation DimaString, bString, cStringas String aString = “Hello” bString = “Dolly” cString = aString&“ “&bString‘insert a space The value of cString is now “Hello Dolly” You can also use aString&= “ “ &bString (this changes aString to “Hello Dolly”)

8 More String Operations There are plenty of useful string operations – str.Length‘length of the string – str.ToUpper‘convert to upper case – str.ToLower‘convert to lower case – str.Trim‘remove leading and trailing blanks – str.Substring(m,n) ‘substring length n starting at m – str.IndexOf(str2)‘start of str2 in str Here str is the name of a String variable. See page 82 for examples

9 Examples We’ll pause here and run a few examples

10 Other Data Types Boolean: named after logician George Boole Can take only one of two values: True and False DimvarNameas Boolean There are several other data types, which we will introduce if we need them

11 Strict and Explicit You should turn on the options for Strict and Explicit in the VB system (see book pg 78) This will make the system enforce declarations for variables, and will make you explicitly convert values to different types when needed This helps by catching errors and is a very good programming practice

12 Data Types We have learned about several data types Double: for general computation Integer: for counting String: for working with text Boolean: True and False Variables must be declared as one of these data types (or some other valid type), using a Dim statement

13 Casting Converting information from one data type to another is called casting. (CInt, CStr) Casting is most often needed when printing numeric values in text boxes or list boxes, or reading them from text boxes DimnumVaras Double numVar = 124 txtBox.Text = CStr(numVar + 2) ‘convert to string

14 Scope of Variables Variables can be declared within a sub-procedure or function, or at the level of the class A variable that is declared at the level of the class (also called a global variable) can be seen by all the sub-procedures and functions, and is in existence as long as the program is running A variable declared within a sub-procedure or function (also called a local variable) can only be seen there, and exists only as long as the sub- procedure or function is running

15 Global vs Local Public Class formDemo DimvarAas Double‘global variable Private Sub butRed_Click(…) HandlesbutRed.Click DimvarB as String‘local variable End Sub End Class

16 Tricky Case Public Class formDemo DimvarAas Double = 1‘global variable Private Sub butRed_Click(…) HandlesbutRed.Click DimvarA as Double = 1‘local variable varA += 1‘local varA now = 2, global varA still = 1 End Sub Private Sub butBlue_Click(…) HandlesbutBlue.Click DimvarB as Double‘local variable varB = varA‘local varB now = 1, using global varA End Sub End Class

17 Types of Errors As you start to write code you will encounter errors This is a natural part of programming. Very few people can write a perfect program the first time There are three major types of errors – Syntax errors – Runtime errors – Logic errors

18 Syntax Errors A syntax error is a mistake in the form of the program: writing something that is not a legal VB statement The editor will catch syntax errors and try to figure out what the problem is You cannot run your program if it has any syntax errors in it

19 Runtime Errors With a runtime error the program will start to run, but then something happens that the system can’t handle, and you get a runtime error message Typical causes include dividing by zero (dividing by a variable whose value has become zero) or having a number become too large to fit in its data type

20 Logic Errors With a logic error, the program runs fine but produces a wrong answer (an answer you didn’t expect) or behaves in a way you didn’t want it to Reading over the code may show you the source of the error As with runtime errors, using the debugger is often the quickest way to identify where the error takes place

21 The Debugger Pages have a quick overview of how to use the debugger Appendix D has a nice, thorough discussion with an example. I strongly suggest you work through that example and read the rest of the appendix This will save you a whole lot of time in the long run

22 Formatting Output Output can appear in a text box, list box, or file Formatting lets us present neatly arranged columns, control the number of decimal places shown, etc. Output is almost always a string. There are cases where people make files of other data types, but we will not encounter those

23 Formatting Sampler FormatNumber( , 1):123.6 FormatCurrency( ,2):$ FormatCurrency(-1000):($1000) FormatPercent(0.185,2):18.50% See the book for formatting with zones: how to make nice neat columns (pg 97)

24 Postponed Topic from Chapter 3 Reading data from files and writing to files is very important It’s the only way to have a permanent record of what happened in your program We’ll skip it for now, come back if we have time

25 Break 10 min

26 New Assignment This will be the first programming assignment It’s due --- Let’s take a look….

27 Class example Create an order process for an ice cream store. The user should be able to choose the number of scoops to buy. Each scoop costs $.75. After the user chooses the number of scoops, the program computes the cost, adding a 15% tip for the server.

28 Interface Objects Place to enter the number of scoops (txtScoops) Place to display the price (txtPrice) A Buy button (butBuy) [I thought of this while doing the use case…how will the program know the user is finished entering the number?] Information for the user (labels)

29 Use Case 1 The user enters a number of scoops in the Scoops window The user pushes the Buy button The program figures out how much to charge by taking the number of scoops times.75 and adding the 15% charge

30 Use case 2 The user has not entered anything in the Scoops window The user pushes the Buy button Experiment shows the program blows up with a runtime error. Once we have conditionals we can test for this (Chapter 4) Experiment shows a non-number causes a blowup too

31 Objects and Events Scoops text box – User enters a value Buy button – User pushes the button Price window – Program writes in the window The only really non-trivial one is pushing the Buy button

32 Flowchart Push Buy button Input a number? yesno Handle bad input end Compute cost Add tip end Print result

33 Variables and Constants What does the program need to remember? – How much a scoop costs – What percent the tip should be – How many scoops the user is buying

34 Let’s look at the code…