50
Variables, Constants, Methods, and Calculations Chapter 3 - Review

Variables, Constants, Methods, and Calculations Chapter 3 - Review

Embed Size (px)

Citation preview

Page 1: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Variables, Constants, Methods, and Calculations

Chapter 3 - Review

Page 2: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Variables

Creating a space in memory to hold data to be utilized while the application is running.

Every variable has a:NameData typeScopeLifetime

Page 3: Variables, Constants, Methods, and Calculations Chapter 3 - Review

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

Page 4: Variables, Constants, Methods, and Calculations Chapter 3 - Review

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

Page 5: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Declaring a Variable

Declaration statement: used to declare, or create, a variable

Declaration statement includesScope keyword: Dim or Private or StaticName of the variableData type Initial value (optional)

Page 6: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Assigning Data to an Existing Variable

Assignment statement:Used to assign values to properties of

controlsUsed 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

Page 7: Variables, Constants, Methods, and Calculations Chapter 3 - Review

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

Page 8: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Option Explicit ON

Page 9: Variables, Constants, Methods, and Calculations Chapter 3 - Review

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

Page 10: Variables, Constants, Methods, and Calculations Chapter 3 - Review

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 numbersData loss can occur with demoting

Page 11: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Option Strict ON

Option Strict ON = prevents possible loss of data by not allowing any implicit type conversionsCan be used to enforce correct data typing

Placed in the General Declarations section of the code editor

Page 12: Variables, Constants, Methods, and Calculations Chapter 3 - Review

General Declaration

Page 13: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Program Design

TOE ChartsPseudocodeFlowcharts

All are visual tools to help design the logical flow of the program.

Page 14: Variables, Constants, Methods, and Calculations Chapter 3 - Review

TOE Charts

Task

Object

Event

Page 15: Variables, Constants, Methods, and Calculations Chapter 3 - Review

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

Page 16: Variables, Constants, Methods, and Calculations Chapter 3 - Review

TOE Chart – Task

1 – Input

2 – Calculate

3 – Print to screen or paper

4 – Exit program

5 – Clear form

Page 17: Variables, Constants, Methods, and Calculations Chapter 3 - Review

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.

Page 18: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Perry Primary SchoolMainForm Task Object Event

1.Calculate the sum of and the difference between the two numbers 2.Display the sum and difference in the sumLabel and differenceLabel controls

computeButton Click

End the application exitButton Click

Display the sum and difference (from computeButton)

sumLabel, differenceLabel

None

Get and display two numbers firstTextBox, secondTextBox

None

Display the DialogForm optionsButton Click

Display the SplashScreenForm MainForm LoadDialogForm Task Object Event

Change the color of the MainForm’s text to red

redButton Click

Change the color of the MainForm’s text to black

blackButton Click

SplashScreenForm Task Object Event

Close the SplashScreenForm splashTimer Tick

Page 19: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Pseudocode

Pseudocode identifies the steps of a procedure and writes it in short phases.

Each procedure will have it’s own pseudocode.

Page 20: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Skate-Away Pseudocode

Page 21: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Flowcharts

Flowcharts identifies the steps of a procedure and diagrams it using special symbols.

Page 22: Variables, Constants, Methods, and Calculations Chapter 3 - Review

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

Page 23: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Flowcharts

Flowcharts identifies the steps of a procedure and diagrams it using special symbols.

Each procedure will have it’s own flowchart.

Page 24: Variables, Constants, Methods, and Calculations Chapter 3 - Review
Page 25: Variables, Constants, Methods, and Calculations Chapter 3 - Review

General Design Guide

1. Input

2. Calculate

3. Output (print to screen and/or paper)

4. Clear old data

5. End program

Page 26: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Code Flow

Step – by – step

Page 27: Variables, Constants, Methods, and Calculations Chapter 3 - Review

General Design Guide

1. Input

2. Calculate

3. Output (print to screen and/or paper)

4. Clear old data

5. End program

Page 28: Variables, Constants, Methods, and Calculations Chapter 3 - Review

General Design Guide

1. Input Validate

2. Calculate

3. Output (print to screen and/or paper)

4. Clear old data

5. End program

Page 29: Variables, Constants, Methods, and Calculations Chapter 3 - Review

General Design Guide

1. Input Validate Convert

2. Calculate

3. Output (print to screen and/or paper)

4. Clear old data

5. End program

Page 30: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Using the TryParse MethodMethod: a specific portion of a class’s

instructions that performs a task for the class

TryParse method: Part of every numeric data type’s classUsed to convert a string to that numeric data

type

Page 31: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Using the TryParse MethodTryParse method has 4 arguments

String: string value to be convertedVariable: location to store the result IFormatProvider (optional): specifies

formattingNumberStyles (optional): allows formatting

characters to be in the data to be converted

Page 32: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Using the TryParse MethodMethod: a specific portion of a class’s

instructions that performs a task for the class

TryParse method: Part of every numeric data type’s classUsed to convert a string to that numeric data

type

Page 33: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Using the TryParse MethodTryParse method has 4 arguments

String: string value to be convertedVariable: location to store the result IFormatProvider (optional): specifies

formattingNumberStyles (optional): allows formatting

characters to be in the data to be converted

Page 34: Variables, Constants, Methods, and Calculations Chapter 3 - Review

General Design Guide

1. Input Validate Convert

2. Calculate

3. Output (print to screen and/or paper)

4. Clear old data

5. End program

Page 35: Variables, Constants, Methods, and Calculations Chapter 3 - Review

General Design Guide

1. Input

2. Calculate

3. Output (print to screen and/or paper)

4. Clear old data

5. End program

Page 36: Variables, Constants, Methods, and Calculations Chapter 3 - Review

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

Page 37: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Arithmetic Operators – Precedence Order

Page 38: Variables, Constants, Methods, and Calculations Chapter 3 - Review

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

Page 39: Variables, Constants, Methods, and Calculations Chapter 3 - Review

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.

Page 40: Variables, Constants, Methods, and Calculations Chapter 3 - Review

General Design Guide

1. Input

2. Calculate

3. Output (print to screen and/or paper)

4. Clear old data

5. End program

Page 41: Variables, Constants, Methods, and Calculations Chapter 3 - Review

General Design Guide

1. Input

2. Calculate

3. Output (print to screen and/or paper)

4. Clear old data

5. End program

Page 42: Variables, Constants, Methods, and Calculations Chapter 3 - Review

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

Page 43: Variables, Constants, Methods, and Calculations Chapter 3 - Review

General Design Guide

Output / Display RULE: Output to be displayed on the

screen must be of the data type STRING.

Page 44: Variables, Constants, Methods, and Calculations Chapter 3 - Review

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.

Page 45: Variables, Constants, Methods, and Calculations Chapter 3 - Review

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.

Page 46: Variables, Constants, Methods, and Calculations Chapter 3 - Review

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

Page 47: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Format Specifier

Page 48: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Format Specifier and Precision Specifier

Page 49: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Formatting Numeric Output (continued)

Page 50: Variables, Constants, Methods, and Calculations Chapter 3 - Review

Formatting Numeric Output (continued)