Programming Fundamentals: Chapter Outline
Programming Fundamentals: Chapter Outline
Programming
Fundamentals
CHAPTER OUTLINE
ALGORITHMS AND SYNTAX
VARIABLES AND DATA TYPE
OPERATORS
EXPRESSING PROGRAM ALGORITHMS
INTRODUCTION
Chapter Objectives
At the end of the chapter, the
student should be able to:
▪ read program algorithms and
syntax;
▪ declare variables with specific
data types and scopes;
▪ use different operators in
writing program statements;
and
▪ create flowcharts in expressing
program algorithms.
6
ALGORITHMS AND SYNTAX
Algorithm
▪ An algorithm is a specific step-by-
step solution to a particular
problem.
▪ An algorithm can be a single line of
code or combinations of lines.
▪ The computer’s response is based
on the predetermined instructions
that is contained in the algorithm.
▪ If an event falls outside the
algorithm, either there would be no
response or an error would occur
within the computer system.
ALGORITHMS AND SYNTAX
Program Statement
▪ A line of code in a Visual Basic program and serves
as basic sensible unit of an algorithm.
▪ A statement is any combination of Visual BASIC
keywords, properties, object names, variables,
numbers, special symbols and other values that
collectively create a valid instruction recognized by
the Visual Basic compiler.
Example:
End
Label1.Text = TimeOfDay
MessageBox.Show("Hello " & txtName.Text & "!")
ALGORITHMS AND SYNTAX
Syntax
▪ The rules of construction that must be used to
build program statements
▪ Example (Declaring a variable):
Dim [VariableName] As DataType
▪ Note that the codes which are in blue text are
keywords, defined as the basic command elements
in Visual BASIC.
▪ Words enclosed by brackets [ ] are user-based
values in which the specified value depends on the
programmer.
VARIABLES AND DATA TYPES
VARIABLES
▪ A temporarily named
storage location for
data in your program.
▪ Can (1) hold
information entered by
the user at run time, (2)
be the result of a
specific calculation, or
(3) be a piece of data
you want to display on
your form.
VARIABLES AND DATA TYPES
Declaring Variables
▪ The process of creating a variable is called
declaring.
▪ It is the setting aside of space in the memory
(RAM) of a computer for the variable’s use.
▪ Keyword used is Dim which stands for
dimension
▪ Example:
Dim FirstName As String
Dim Age As Integer
VARIABLES AND DATA TYPES
Assigning Values to Variables
▪ Example:
Dim FirstName As String
FirstName = “Juan"
or
Dim FirstName As String = “Juan"
▪ The variable on the LEFT side of the equation
always receives the value of the expression written
at the right side of the equation
VARIABLES AND DATA TYPES
Variable Naming Conventions
1. Begin each variable name with a letter or
underscore.
2. Try to keep variable names under 33 characters to
make them easier to read.
3. Make your variable names descriptive by
combining one or more words when it makes
sense to do so.
4. Use a combination of uppercase and lowercase
characters and numbers.
5. Don’t use Visual Basic keywords
6. Optionally, you can begin each variable name
with a three-letter abbreviation that corresponds
to the variable’s data type (e.g. intSum, decAve,
strLastName)
VARIABLES AND DATA TYPES
Constants
▪ Syntax:
Const [ConstantName] As DataType = [Value]
▪ Example:
Const Pi As Double = 3.14159265
Const UnivGasConst As Single = 8.314472
VARIABLES AND DATA TYPES
Literals
▪ Literals are exact values, which are placed on variables
or constants, that are NOT results of an expression or
calculation. In contrast to constants, a literal is not a
name -- it is the value itself
▪ In the statement below, “Paolo” and “3” are literals:
Dim FirstName As String = “Paolo"
Const MyByte As Byte = 3
▪ In some cases, a literal can be forced to a particular data
type in order to prevent an error, by appending a type
character to it, or by placing it within enclosing
characters.
▪ Example, Dim is a keyword. Enclosing it with “” allows it
to be assigned as a value of a string.
VARIABLES AND DATA TYPES
Literals
▪ The table below summarizes how to use literals:
Enclosing Appended type
Data type Example
character character
Boolean (none) (none) (none)
Byte (none) (none) (none)
Char " c “Hello"c
Date # (none) #6/23/2011#
Decimal (none) D or @ 123.45D ; 123.45@
Double (none) R or # 123.45R ; 123.45#
Integer (none) I or % 12345I ; 12345%
Long (none) L or & 12345L ; 12345&
Short (none) S 12345S
Single (none) F or ! 123.45F ; 123.45!
String " (none) "ABCD“
VARIABLES AND DATA TYPES
Literals
▪ The examples below demonstrate the correct usage of
type characters and enclosing characters:
' Default to Integer.
Const DefaultInteger As Integer = 100%
Example: If Value = 8
Logical operators have the following order of precedence: negation (Not), conjunction
(And, AndAlso), inclusive disjunction (Or, OrElse), and exclusive disjunction (Xor).
OPERATORS
Concatenation Operators
[Accept Input]
If [input] >= - 273.15 Then
[Add 273.15 to input]
Else
[Ask for another value]
End If
EXPRESSING PROGRAM ALGORITHMS
Flowcharts
▪ Flowchart is a
graphical
representation of
an algorithm to
define its logical
flow.
▪ It illustrates the
steps of an
algorithm by using
different kinds of
boxes which are
ordered by
connecting them
with arrows (shown
on the right).
EXPRESSING PROGRAM ALGORITHMS
Flowcharts
The algorithm for a Celsius-To-Kelvin program can be expressed as:
EXPRESSING PROGRAM ALGORITHMS
Programming Language
btnConvert txtCelsius
txtKelvin
EXPRESSING PROGRAM ALGORITHMS
Sample Program
SUMMARY
▪Writing program algorithms
follow specific syntax.
▪One of the basic building blocks of
algorithms is a variable that is
declared with proper data type
and scope.
▪Operators are symbols or
keywords used to process
variables, constants, and literals
into meaningful results.
▪Flowcharts, using graphics, are
ways of planning and expressing
an algorithm in developing and
presenting program algorithms.
Thank you very much!
Next Chapter:
STRUCTURED PROGRAMMING
Decision/Selection Structure
Repetition Structure