Docsity

Programming Basics: Constants and Variables, Assignments of Information Technology

Toggle sidebar
bg1
Constants and Variables
Named Constants are used to hold values that never change, at least for the life of the
program. Some constants truly are constant, for example, the value of pi is always
3.14...., and the number of inches in a foot are always 12. At other times, the same value
will hold for the run of a program, e.g., number of students, tax rate, etc. Declaring
constants looks like the following:
Const Pi as Single = 3.14
Const Inchperfoot as Integer = 12
Const Numstudent as Integer = 40
Const Welcome as String = “Welcome to the Superette”
Const Taxrate as Single = 0.07
** Note that if you have a percent, it must have a 0 in front of the decimal
Variables are used to hold values that can change while a program is running. There are
three types of variables (that we're concerned with now).
Integer -- for whole numbers, including negative whole numbers
Single -- for numbers that can have a decimal
String -- for strings of characters
Declaring variables looks like the following:
Dim Age as Integer
Dim GPA as Single
Dim Student as String * 30
(** note the number at the end-- it's there to save space --
otherwise, the computer will make the string 256 characters long)
Good programming practice requires that all variables be declared. Our version of Visual
Basic is set up to require you to declare all variables and will give you an error if you
don’t.
pf3

Partial preview of the text

Download Programming Basics: Constants and Variables and more Assignments Information Technology in PDF only on Docsity!

Constants and Variables

Named Constants are used to hold values that never change, at least for the life of the program. Some constants truly are constant, for example, the value of pi is always 3.14...., and the number of inches in a foot are always 12. At other times, the same value will hold for the run of a program, e.g., number of students, tax rate, etc. Declaring constants looks like the following:

Const Pi as Single = 3. Const Inchperfoot as Integer = 12 Const Numstudent as Integer = 40 Const Welcome as String = “Welcome to the Superette” Const Taxrate as Single = 0. ** Note that if you have a percent, it must have a 0 in front of the decimal

Variables are used to hold values that can change while a program is running. There are three types of variables (that we're concerned with now).

Integer -- for whole numbers, including negative whole numbers Single -- for numbers that can have a decimal String -- for strings of characters

Declaring variables looks like the following:

Dim Age as Integer Dim GPA as Single Dim Student as String * 30 (** note the number at the end-- it's there to save space -- otherwise, the computer will make the string 256 characters long)

Good programming practice requires that all variables be declared. Our version of Visual Basic is set up to require you to declare all variables and will give you an error if you don’t.

Assignments

In order to give a variable a value, you assign a value to it. The value could be a simple value, or it could be the result of an expression. The format is as follows:

Variable = value Variable = expression

The value may be one of a number of things: An actual value IQ = 115 The value of another variable YourIQ = MyIQ The result of a computation YourIQ = MyIQ - 40 The value of a constant Length = footlength (where footlength was declared as 12) Although arithmetic is fully covered in another section, here are some more examples of computing values: The results of computing a multiplication: salestax = baseprice * taxrate

The result of a concatenation of strings: welcome = “Howdy” & “, “ & “Pardner”

The initial assignment can be given to a variable when it is declared:

Dim counter as Integer = 0 Dim totalprice as Single = 0.