Professional Documents
Culture Documents
Zak - Ch03 Theory
Zak - Ch03 Theory
Chapter 3
Coding with
Variables, Named
Constants,
and Calculations
• Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
FOCUS ON THE CONCEPTS LESSON
(1 of 2)
Concepts covered in this lesson:
• F-1 Pseudocode /planning steps (it is important!)
• F-2 Main memory of a computer
• F-3 Variables
• F-5 Arithmetic expressions
• F-6 Assigning a value to an existing variable
• F-7 ToString method
• F-8 Option statements
• F-9 Named constants
• Procedure-level variables
• How to convert the planning steps to Visual Basic
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-1 Pseudocode (1 of 3)
Many programmers use planning tools such as pseudocode.
• Pseudocode/Planning steps uses short phrases to
describe the steps a procedure must take to accomplish its
goal. e.g.
– Declare input and output variables
– Accept 2 numbers
– Calculate the sum
– Display the sum
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-2 Main Memory of a Computer (2 of 3)
• Reserving a memory location is more commonly
referred to as declaring a memory location.
• There are two types of memory locations that a
programmer can declare:
– variables and
– named constants.
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-2 Main Memory of a Computer (3 of 3)
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-3 Variables
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Selecting an Appropriate Data Type
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Selecting an Appropriate Data Type
• The String data type can store from zero to
approximately 2 billion characters.
• The Boolean data type stores Boolean (or logical)
values: either True or False.
• The Char data type store one Unicode character.
• Examples:
• Integer: 15 or 500024 or -88 or 4577711111
• Double: 15.00001 or -55.0 or 40000000.45
• Decimal: 1.000004 or -104.33 or 80000000.0
• String: “VUT” or “Vanderbijlpark” or “Sebokeng”
• Char: “7” or “r” or “R” or “(“ or “<“
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Selecting an Appropriate Data Type
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Selecting an Appropriate Name
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Selecting an Appropriate Name
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-5 Arithmetic Expressions
Most commonly used operators:
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-5 Arithmetic Expressions
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-6 Assigning a Value to an Existing
Variable
An assignment statement is also used to assign a
value to a variable during run time.
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-6 Assigning a Value to an Existing
Variable
To calculate the circle’s area and then assign it to a
variable:
• or
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-7 To String Method
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-7 To String Method
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-8 Option Statements
Option Explicit On
Option Strict On
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-8 Option Statements
• Option Strict On
– Stop automatic (implicit) conversions
– (This will stop you from converting between the
wrong types by accident)
• Option Explicit On
– All variables must be declared
– (you cannot just use a variable without a Dim
statement for it)
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-8 Option Statements
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-8 Option Statements
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-8 Option Statements
and variable assignments
• The type of the variable determine the data that can be
saved in it:
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-8 Option Statements
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-8 Option Statements
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-8 Option Statements
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-9 Named Constants
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-9 Named Constants
Syntax for Const statement
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-9 Named Constants (3 of 4)
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
F-9 Named Constants
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
A-1 Determine a Memory Location’s Scope
and Lifetime
• The scope indicates where the declared memory
location can be used in an application’s code,
• and the lifetime indicates how long the variable or
named constant remains in the computer’s main
memory.
• The scope and lifetime are determined by where you
declare the memory location in your code.
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
A-1 Determine a Memory Location’s Scope
and Lifetime
• Memory locations declared in a procedure have
procedure scope and are called procedure-level
variables and procedure-level named constants.
• (Thus they can only be used in that procedure)
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
A-2 Use Procedure-Level Variables
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
A-3 Use Procedure-Level Named
Constants
• Like procedure-level variables, procedure-level named
constants are declared at the beginning of a procedure,
and they can be used only after their declaration
statement within the procedure.
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
How to code an application from the
planning.
• At the end of the Study/Leaner guide in Appendix F you
will find a list if initial VB.NET statements and their
meanings. This will assist students!!
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
How to code an application from the
planning.
• Open the Planning of the Tax Application (Week 3).
• You know already how to create the solution and how
to design the screen using the planning.
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
How to code an application from the
planning/ pseudocode
1. The first step is to add the comments and the option
statements. Press F7 to open the Code and fill it in:
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
How to code an application from the
planning/ pseudocode
2. Now we will take the button planning/pseudocode steps
and follow each one. So we must declare the variables:
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
How to code an application from the
planning/ pseudocode
3. The next step is to get the amount.
We need to get the amount that the
user entered in the text box txtAmount
And store it in the variable:.
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
How to code an application from the
planning/ pseudocode
When we type the statement is gives a conversion error.
Click on the red block below txtAmount.Text to open the
error message and click on the Replace message.
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
How to code an application from the
planning/ pseudocode
The Replace adds the conversion (From String to
Decimal) for us. (The Text property stores a String value):
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
How to code an application from the
planning/ pseudocode
4. The next step is to:
calculate the tax amount
We add the formula:
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
How to code an application from the
planning/ pseudocode
5. The last step is to:
display the tax amount
We add the statement:
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
How to code an application from the
planning/ pseudocode
Again there is a conversion error that we are warned
about. When we display output we want to format the
output (e.g. display a rounded value). So now we will add
a call to the toString function. For monetary values we
will always specify that we want currency formatting:
(So for output we will always call the toString function)
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Summary
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Summary
• User-provided items (input) included in a calculation
should be stored in variables.
• The results of calculations made by the application
should also be stored in variables.
• In most procedures, the Dim statement is used to
declare a variable.
• Each data type is a class from which a variable can be
instantiated.
• The value stored in a control’s Text property is always
treated as a string.
• You can use the CInt, CDec or CDbl calls to convert a
string to a number.
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Summary
• The arithmetic operators have an order of precedence.
You can use parentheses to override the normal order
of precedence.
• The integer division (\) operator divides two integers
and then returns the result as an integer.
• The modulus (Mod) operator divides two numbers and
then returns the remainder of the division.
• When an expression contains more than one operator
with the same precedence number, those operators are
evaluated from left to right.
• Arithmetic expressions should not contain dollar signs,
commas, or percent signs.
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Summary
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Summary
• To ensure that all of your variables have been declared,
enter the Option Explicit On statement above the Public
Class clause in the Code Editor window.
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Summary
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part
Summary
Copyright © 2017 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a
publicly accessible website, in whole or in part