Download presentation
Presentation is loading. Please wait.
Published byMegan Terry Modified over 9 years ago
1
Variables, Constants, Methods, and Calculations Chapter 3 - Review
2
Variables Creating a space in memory to hold data to be utilized while the application is running. Every variable has a: Name Data type Scope Lifetime
3
Selecting a Data Type for a Variable Each variable must be assigned a data type Data type: the type of data the variable can store Each data type is a class
4
Selecting a Name for a Variable Identifier: descriptive name given to a variable Use a meaningful name that reflects the purpose of the variable Use Hungarian Notation (intNumber, strName) Variable names must conform to naming rules (no spaces, no reserved words, start with characters, no punctuation except the underscore _)
5
Declaring a Variable Declaration statement: used to declare, or create, a variable Declaration statement includes Scope keyword: Dim or Private or Static Name of the variable Data type Initial value (optional)
6
Assigning Data to an Existing Variable Assignment statement: Used to assign values to properties of controls Used to assign values to variables Assignment operator: (=) Value on the right of the = operator is assigned to the variable on the left of the = operator
7
Option Explicit ON Option Explicit ON = must declare (create) the variable before using Protects against misspelled variable names in code Placed in the General Declarations section of code editor
8
Option Explicit ON
9
Data Typing Implicit type conversion: can occur if the value on the right side of an assignment statement is not the same data type as the variable on the left side Example: 25.65 converted to Integer = 26 25.45 converted to Integer = 25
10
Data Typing Promoting: when a value is converted to another data type that stores larger numbers Demoting: when a value is converted to another data type that stores only smaller numbers Data loss can occur with demoting
11
Option Strict ON Option Strict ON = prevents possible loss of data by not allowing any implicit type conversions Can be used to enforce correct data typing Placed in the General Declarations section of the code editor
12
General Declaration
13
Program Design TOE Charts Pseudocode Flowcharts All are visual tools to help design the logical flow of the program.
14
TOE Charts Task Object Event
15
TOE Charts TOE Charts help the programmer to think through what the application will do and what it will need to run properly. 1. Input 2. Calculate 3. Print out to screen or paper 4. Clear used data from controls 5. Exit program
16
TOE Chart – Task 1 – Input 2 – Calculate 3 – Print to screen or paper 4 – Exit program 5 – Clear form
17
TOE Charts TOE Charts provide a visual tool to identify the tasks of the application, the objects used in the user interface and the events (clicking, double-clicking) that trigger the code. TOE Charts identifies the tasks, objects and events of the FORM.
18
Perry Primary School MainForm TaskObjectEvent 1.Calculate the sum of and the difference between the two numbers 2.Display the sum and difference in the sumLabel and differenceLabel controls computeButtonClick End the applicationexitButtonClick Display the sum and difference (from computeButton) sumLabel, differenceLabel None Get and display two numbersfirstTextBox, secondTextBox None Display the DialogFormoptionsButtonClick Display the SplashScreenFormMainFormLoad DialogForm TaskObjectEvent Change the color of the MainForm ’ s text to red redButtonClick Change the color of the MainForm ’ s text to black blackButtonClick SplashScreenForm TaskObjectEvent Close the SplashScreenFormsplashTimerTick
19
Pseudocode Pseudocode identifies the steps of a procedure and writes it in short phases. Each procedure will have it’s own pseudocode.
20
Skate-Away Pseudocode
21
Flowcharts Flowcharts identifies the steps of a procedure and diagrams it using special symbols.
22
Flowcharts Oval – start / stop symbol Rectangles – process symbols represents tasks such as making calculations Parallelogram – input / output symbol represent input tasks – getting information from user represent output tasks – displaying information Lines – flowlines symbol represents logical flow connects the symbols
23
Flowcharts Flowcharts identifies the steps of a procedure and diagrams it using special symbols. Each procedure will have it’s own flowchart.
25
General Design Guide 1. Input 2. Calculate 3. Output (print to screen and/or paper) 4. Clear old data 5. End program
26
Code Flow Step – by – step
27
General Design Guide 1. Input 2. Calculate 3. Output (print to screen and/or paper) 4. Clear old data 5. End program
28
General Design Guide 1. Input Validate 2. Calculate 3. Output (print to screen and/or paper) 4. Clear old data 5. End program
29
General Design Guide 1. Input Validate Convert 2. Calculate 3. Output (print to screen and/or paper) 4. Clear old data 5. End program
30
Using the TryParse Method Method: a specific portion of a class’s instructions that performs a task for the class TryParse method: Part of every numeric data type’s class Used to convert a string to that numeric data type
31
Using the TryParse Method TryParse method has 4 arguments String: string value to be converted Variable: location to store the result IFormatProvider (optional): specifies formatting NumberStyles (optional): allows formatting characters to be in the data to be converted
32
Using the TryParse Method Method: a specific portion of a class’s instructions that performs a task for the class TryParse method: Part of every numeric data type’s class Used to convert a string to that numeric data type
33
Using the TryParse Method TryParse method has 4 arguments String: string value to be converted Variable: location to store the result IFormatProvider (optional): specifies formatting NumberStyles (optional): allows formatting characters to be in the data to be converted
34
General Design Guide 1. Input Validate Convert 2. Calculate 3. Output (print to screen and/or paper) 4. Clear old data 5. End program
35
General Design Guide 1. Input 2. Calculate 3. Output (print to screen and/or paper) 4. Clear old data 5. End program
36
General Design Guide Calculate RULE 1: There is an order in which the computer performs the operation in an expression RULE 2: All variables must be of the same data type to be able to do mathematical calculations
37
Arithmetic Operators – Precedence Order
38
General Design Guide Calculate RULE 1: There is an order in which the computer performs the operation in an expression RULE 2: All variables must be of the same data type to be able to do mathematical calculations
39
General Design Guide Calculate RULE 1: There is an order in which the computer performs the operation in an expression RULE 2: All variables must be of the same data type to be able to do mathematical calculations When possible, have the TryParse Method convert the user input (String Data Type) to the numeric data type needed for the calculation.
40
General Design Guide 1. Input 2. Calculate 3. Output (print to screen and/or paper) 4. Clear old data 5. End program
41
General Design Guide 1. Input 2. Calculate 3. Output (print to screen and/or paper) 4. Clear old data 5. End program
42
General Design Guide 1. Input 2. Calculate 3. Output (print to screen and/or paper) Convert data type to STRING Format output 4. Clear old data 5. End program
43
General Design Guide Output / Display RULE: Output to be displayed on the screen must be of the data type STRING.
44
General Design Guide Output / Display RULE: Output to be displayed on the screen must be of the data type STRING. RULE: Values (data) assigned to the Text Property must be of the data type STRING.
45
General Design Guide Output / Display RULE: Output to be displayed on the screen must be of the data type STRING. RULE: Values (data) assigned to the Text Property must be of the data type STRING. Provide proper formatting of the output as required.
46
Formatting Numeric Output Formatting: specifying the number of decimal places and any special characters to display Format specifier: specifies the type of formatting to use (“C”) (“N”) (“D”) (“P”) Precision specifier: controls the number of significant digits or zeros to the right of the decimal point (“C2”)(“N1”)
47
Format Specifier
48
Format Specifier and Precision Specifier
49
Formatting Numeric Output (continued)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.