Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.

Slides:



Advertisements
Similar presentations
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Advertisements

Fundamentals of Programming in Visual Basic
1.
Fundamentals of Programming in Visual Basic
Chapter 31 Fundamentals of Programming in Visual Basic (Continue IV) Strings Variables and Strings Using Text Boxes for Input and Output Concatenation.
Introduction to Computing Dr. Nadeem A Khan. Lecture 4.
Chapter 31 Fundamentals of Programming in VB(Continue I) Numbers Arithmetic Operations Variables Incrementing the Value of a Variable.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
JavaScript, Third Edition
Basic Input/Output and Variables Ethan Cerami New York
Copyright © 2001 by Wiley. All rights reserved. Chapter 3: Variables, Assignment Statements, and Arithmetic Variables Assignment Statements Arithmetic.
Variables & Math Operators CE 311 K - Introduction to Computer Methods Daene C. McKinney.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Chapter 31 Fundamentals of Programming in Visual Basic (Continue VI) String Properties and Methods: "Visual".Length is 6. "Visual".ToUpper is VISUAL. "123.
1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Created By Mayson Al-Duwais1. Using Exit to Terminate Repetition Statements To terminate different types of repetition statements you can use a special.
Variables and Constants
CS0004: Introduction to Programming Variables – Numbers.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
2440: 211 Interactive Web Programming Expressions & Operators.
R EVIEW OF C HAPTER 2. H OW TO D EVELOP A VB A PPLICATION Design the Interface for the user Literally draw the GUI Drag buttons/text boxes/etc onto form.
Chapter 3 - VB.NET by Schneider1 Chapter 3 – Fundamentals of Programming in VB.NET VB.NET Controls VB.NET Events Numbers Strings Input and Output.
Chapter 3 - VB 2005 by Schneider1 Chapter 3 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers.
CS0004: Introduction to Programming Variables – Strings.
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: Data Types and Operators JavaScript - Introductory.
Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
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 – Fundamentals of Programming in Visual Basic 3.1 Visual Basic Controls 3.2 Visual Basic Events 3.3 Numbers 3.4 Strings 3.5 Input and Output.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
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.
Introduction to Programming Fundamentals of Programming in Visual Basic.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
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.
Chapter 3 - Visual Basic Schneider Numeric Variables Used to store numbers Value is assigned by a statement of the form: numVar = expression The variable.
Chapter 4 Getting Started with VBA. Subroutines Subroutine is the logical section of code that performs a particular task. Subroutine is also called a.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Creation of Variables with Numeric, alphanumeric, date, picture, memo data types Constant - A quantity that does not change during the execution of a program.
3.4 Strings Variables and Strings Using Text Boxes for Input and Output Concatenation ANSI Character Set String Properties and Methods: LengthToUpper TrimToLower.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3 Variables and Calculations.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Introduction to Computer CC111 Week 10 Visual Basic 3 1.
Chapter 3 – Fundamentals of Programming in Visual Basic
Chapter 3 – Variables, Input, and Output
Visual Basic Variables
Chapter 3 – Variables, Input, and Output
Chapter 3 Data Types, Variables, and Expressions
Variables and Arithmetic Operations
Chapter 3 – Variables, Input, and Output
Fundamentals of Programming in VB.NET
Numbers.
Strings(Part 1) String Literal and String Variable
String Variable, Methods and Properties
Section 3.3 Numbers Arithmetic Operations Variables
Variable Review & IO User 12/26/2018.
String Variable, Methods and Properties
Additional Topics in VB.NET
Core Objects, Variables, Input, and Output
String Variable, Methods and Properties
Introduction to Programming
String Variable, Methods and Properties
Presentation transcript:

Chapter 3 - VB 2008 by Schneider1 Chapter 3 – Variables, Input, and Output 3.1 Numbers 3.2 Strings 3.3 Input and Output

Chapter 3 - VB 2008 by Schneider2 3.1 Numbers Arithmetic Operations Variables Incrementing the Value of a Variable

Chapter 3 - VB 2008 by Schneider3 Numbers continued The Integer Data Type Multiple Declarations Parentheses Three Types of Errors The Error List Window

Chapter 3 - VB 2008 by Schneider4 Arithmetic Operations Numbers are called numeric literals Five arithmetic operations in Visual Basic + addition - subtraction * multiplication / division ^ exponentiation

Chapter 3 - VB 2008 by Schneider5 Numeric Expressions * (4 + 5) 2 ^ 3

Chapter 3 - VB 2008 by Schneider6 Displaying Numbers Let n be a number or a numeric expression. The statement lstBox.Items.Add(n) displays the value of n in the list box.

Chapter 3 - VB 2008 by Schneider7 Example 1: Form

Chapter 3 - VB 2008 by Schneider8 Example 1: Code and Output Private Sub btnCompute_Click (...) Handles btnCompute.Click lstResults.Items.Add(5) lstResults.Items.Add(2 * 3) lstResults.Items.Add((2 ^ 3) – 1) End Sub Output 5 in list 6 box 7

Chapter 3 - VB 2008 by Schneider9 Numeric Variable A numeric variable is a name to which a number can be assigned. Examples: speed distance interestRate balance

Chapter 3 - VB 2008 by Schneider10 Variables Declaration: Dim speed As Double Variable name Data type Assignment: speed = 50

Chapter 3 - VB 2008 by Schneider11 Initialization Numeric variables are automatically initialized to 0: Dim varName As Double To specify a nonzero initial value Dim varName As Double = 50

Chapter 3 - VB 2008 by Schneider12 Numeric Expressions Numeric variables can be used in numeric expressions. Dim balance As Double = 1000 lstBox.Items.Add(1.05 * balance) Output: 1050

Chapter 3 - VB 2008 by Schneider13 Assignment Statement Dim numVar1 As Double = 5 Dim numVar2 As Double = 4 numVar1 = 3 * numVar2 lstBox.Items.Add(numVar1) Output: 12

Chapter 3 - VB 2008 by Schneider14 Incrementing To add 1 to the numeric variable var var = var + 1 Or as a shortcut var += 1 Or as a generalization var += numeric expression

Chapter 3 - VB 2008 by Schneider15 Integer Data Type Variables of type Double can be assigned both whole numbers and numbers with decimals. The statement Dim varName As Integer declares a numeric variable that can only be assigned whole number values between about -2 billion and 2 billion.

Chapter 3 - VB 2008 by Schneider16 Multiple Declarations Dim a, b As Double Two other types of multiple-declaration statements are Dim a As Double, b As Integer Dim c As Double = 2, b As Integer = 5

Chapter 3 - VB 2008 by Schneider17 Parentheses Parentheses should be used liberally in numeric expressions. In the absence of parentheses, the operations are carried out in the following order: ^, * and /, + and -.

Chapter 3 - VB 2008 by Schneider18 Three Types of Errors Syntax error Run-time error Logic error

Chapter 3 - VB 2008 by Schneider19 Some Types of Syntax Errors Misspellings lstBox.Itms.Add(3) Omissions lstBox.Items.Add(2 + ) Incorrect punctuation Dim m; n As Integer

Chapter 3 - VB 2008 by Schneider20 A Type of Run-time Error Overflow error Dim numVar As Integer = numVar = numVar * numVar

Chapter 3 - VB 2008 by Schneider21 A Logical Error Dim average As Double Dim m As Double = 5 Dim n As Double = 10 average = m + n / 2 Value of average will be 10. Should be 7.5.

Chapter 3 - VB 2008 by Schneider22 Error List Window Dim m; n As Double lstResults.Items.Add(5 lstResults.Items.Add(a)

Chapter 3 - VB 2008 by Schneider Strings Variables and Strings Option Explicit and Option Strict Using Text Boxes for Input and Output Concatenation String Properties and Methods: LengthToUpper TrimToLower IndexOfSubstring

Chapter 3 - VB 2008 by Schneider24 Strings continued Auto Correction The Empty String Initial Value of a String Widening and Narrowing Internal Documentation Line-Continuation Character Scope of a Variable

Chapter 3 - VB 2008 by Schneider25 String Literal A string literal is a sequence of characters surrounded by quotation marks. Examples: "hello" " " "#ab cde?"

Chapter 3 - VB 2008 by Schneider26 String Variable A string variable is a name to which a string value can be assigned. Examples: country ssn word firstName

Chapter 3 - VB 2008 by Schneider27 String Variable Declaration: Dim firstName As String Variable name Data type Assignment: firstName = "Fred"

Chapter 3 - VB 2008 by Schneider28 String Variable You can declare a string variable and assign it a value at the same time. Dim firstName As String = "Fred"

Chapter 3 - VB 2008 by Schneider29 Add Method Let str be a string literal or variable. Then, lstBox.Items.Add(str) displays the value of str in the list box.

Chapter 3 - VB 2008 by Schneider30 String Variable You can assign the value of one string variable to another. Dim strVar1 As String = "Hello" Dim strVar2 As String = "Goodbye" strVar2 = strVar1 lstOutput.Items.Add(strVar2) Output: Hello

Chapter 3 - VB 2008 by Schneider31 Variables and Strings Private Sub btnDisplay_Click(...) Handles btnDisplay.Click Dim president As String president = "George Washington" lstOutput.Items.Add("president") lstOutput.Items.Add(president) End Sub Output: president George Washington

Chapter 3 - VB 2008 by Schneider32 Using Text Boxes for Input and Output The contents of a text box is always a string Input example strVar = txtBox.Text Output example txtBox.Text = strVar

Chapter 3 - VB 2008 by Schneider33 Concatenation Combining two strings to make a new string quote1 = "We'll always " quote2 = "have Paris." quote = quote1 & quote2 txtOutput.Text = quote & " - Humphrey Bogart" Displays We'll always have Paris. - Humphrey Bogart

Chapter 3 - VB 2008 by Schneider34 Appending To append str to the string variable var var = var & str Or as a shortcut var &= str

Chapter 3 - VB 2008 by Schneider35 Appending Example Dim var As String = "Good" var &= "bye" txtBox.Text = var OUTPUT: Goodbye Can you concatenate a string with a number and concatenate numbers? yes, the result will be string

Chapter 3 - VB 2008 by Schneider36 The Empty String The string "", which contains no characters, is called the empty string or the zero-length string. The statement lstBox.Items.Add("") skips a line in the list box. The contents of a text box can be cleared with either the statement txtBox.Clear() or the statement txtBox.Text = ""

Chapter 3 - VB 2008 by Schneider37 Initial Value of a String By default the initial value is Nothing Strings can be given a different initial value as follows: Dim name As String = "Fred"

Chapter 3 - VB 2008 by Schneider38 Comments Private Sub btnCompute_Click (...) Handles btnCompute.Click 'Calculate the balance in an account Dim rate As Double 'Annual rate of interest Dim curBalance As Double 'Current balance

Chapter 3 - VB 2008 by Schneider39 Internal Documentation 1.Other people can easily understand the program. 2.You can understand the program when you read it later. 3.Long programs are easier to read because the purposes of individual pieces can be determined at a glance.

Chapter 3 - VB 2008 by Schneider40 Automatic Colorization Comments – green String literals – maroon Keywords – blue Note: Keywords are words such as Sub, Handles, Private, With, and End that have special meaning in Visual Basic. They cannot be used as variable names.

Chapter 3 - VB 2008 by Schneider41 Getting Input from an Input Dialog Box stringVar = InputBox(prompt, title) fileName = InputBox("Enter the name " _ & "of the file containing the " & _ "information.", "Name of File") Title Prompt

Chapter 3 - VB 2008 by Schneider42 Using a Message Dialog Box for Output MessageBox.Show(prompt, title) MessageBox.Show("Nice try, but no cigar.", _ "Consolation") Title Prompt