You are on page 1of 32

C H A P T E R 1.

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.

2
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.
Computer responds 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 in 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 are called statements syntax
Example (Declaring a variable):

Dim [VariableName] As DataType

Note that the codes which are in blue 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 temporary named storage


location for data in your
program.
Can hold information
entered by the user at run
time, the result of a specific
calculation, or 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 setting aside a space in the memory (RAM) of a
computer for the variables 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

The assignment operator (=) is used to assign data to


variables.
Example:
Dim FirstName As String
FirstName = Paolo"
or
Dim FirstName As String = Paolo"

Always the variable on left side of the equation


receives the value of the expression 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. Dont use Visual Basic keywords
6. Optionally, you can begin each variable name with a
three-letter abbreviation which corresponds to the
variables data type.
VARIABLES AND DATA TYPES
Constants

A constant is a container of values which do not


change.
Syntax:
Const [ConstantName] As Datatype = [Value]
Example:
Const Pi As Double = 3.14159265
Const UnivGasConst As Single = 8.314472
VARIABLES AND DATA TYPES
Data Type
A property of a variable which defines the values that
the variable can take, the programming language used,
or the operations that can be performed on it.
The following data types are used in Visual BASIC:

1.Integer: whole only numbers (decimals are automatically


rounded)
2.Decimal: whole numbers and decimal numbers
3.Text: letters, words or combination of symbols
4.Date: date with specific format
5.Boolean: logic values (true or false)
6.Object: any object such textboxes, forms and other
controls
VARIABLES AND DATA TYPES
Integer Data Type
The range of values for an integer data (default value = 0) type
depends directly on the size of the binary digits that can be
managed by that type; the more digits, the bigger the range.
VARIABLES AND DATA TYPES
Decimal Data Type
The Decimal data type (default value = 0) provides the greatest
number of significant digits for a number (up to 29 significant
digits).
It is particularly suitable for calculations that require a large
number of digits but cannot tolerate rounding errors.
VARIABLES AND DATA TYPES
Text Data Type
String
Text data can be stored using the String data type.
A String holds sequences of unsigned 16-bit (2-byte) code
points that range in value from 0 through 65535.
Char
Another data type that can be used for text data is the
Char data type.
Char data type holds a single Unicode character which is
an unsigned 16-bit (2-byte) code points ranging in value
from 0 through 65535.
The default value of String and Char is Nothing (a null
reference) which is not the same as the empty string
(value "").
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 the statement below, Paolo and 3 are literals:
Dim FirstName As String = Paolo"
Const MyByte As Byte = 2

In some cases, a literal is needed to be forced to a


particular data type to prevent an error.
Example, Dim is a keyword. Enclosing it with allows
it to be assigned as a value of a string.
VARIABLES AND DATA TYPES
Literals
Below are actual examples of when to use literals:
' Default to Integer.
Const DefaultInteger As Integer = 100

' Default to Double.


Const DefaultDouble As Double = 54.3345612

' Force constant to be type Char.


Const MyCharacter As Char = "a"c

' DateTime constants.


Const MyDate As DateTime = #1/15/2001#
Const MyTime As DateTime = #1:15:59 AM#

' Force data type to be Long.


Const MyLong As Long = 45L

' Force data type to be Single.


Const MySingle As Single = 45.55!
VARIABLES AND DATA TYPES
Literals
The table below summarizes how to use literals:
Data type Enclosing Appended 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
Scope
Scope determines which code can access the variable
depending on its location in the program.
A variable can have scope at one of the following
levels:
Level Description
Block scope Available only within the code block in
which it is declared
Procedure scope Available to all code within the procedure
in which it is declared
Module scope Available to all code within the module,
class, or structure in which it is declared
Namespace scope Available to all code in the namespace in
which it is declared
OPERATORS

An operator is symbol or keyword which is used


to perform a logical process.
Together with keyword, variables, and values, it
is used fo construct a formula.
Visual BASIC uses the following general types of
operators:
1. Arithmetic Operators
2. Shortcut Operators
3. Comparison Operators
4. Logical Operators
5. Concatenation Operator
OPERATORS
Arithmetic Operators
Example
Symbol Description
Expression Answer
+ Addition 1+2 3
- Subtraction 9 11 -2
* Multiplication 3*5 15
/ Division 12 / 5 2.4
\ Integer Division 12 \ 5 2
Mod Modulo (remainder) 12 mod 5 2
^ Exponentiation 2^3 8
The priority of opeartion is as follows: ^, *, /, \, mod, +, -.
Example: 8 \ 10 / 5 mod 3 = 1
OPERATORS
Shortcut Operators
The assignment operator (=) can be preceded by an
arithmetic operator (except Mod) to create shortcut
expressions

Example: If Value = 8

Original Equation Shortcut Syntax Result


Value = Value+8 Value +=8 16
Value = Value*2 Value *=2 16
Value = Value^2 Value ^=2 64
OPERATORS
Comparison Operators
Example
Operator Description
Expression Answer
< Less than intA < intB False
Numeric > Greater than intA > intB True
and String = Equal to intA = intB False
(intA = 20,
intB = 10, <> Not Equal to intA <> intB True
intC = 10) <= Less than or equal to intB <= intC True
>= Greater than or equal to intB >= intC True

Note that all comparison operators have equal precedence.


Other comparison operators include Is, IsNot, and Like which
shall be discussed on later chapters.
OPERATORS
Logical Operators
Example (intA = 2, intB = 5, intC = 9)
Operator Description
Expression Answer
And If both conditions are true, then result is true intA = 2 And intB < 8 True
Performs logical negation on a Boolean
Not (intA > intB) True
Not expression, or bitwise negation (1 to 0 or 0 to
Not (intA = 2) False
1) on a numeric expression
Or If one condition is true, then result is true intA < 8 or intB > 5 or intA > intC True
Result is true if only one of the expressions is intA = 2 Xor intB = 6 True
Xor true but not both intA = 2 XorintB = 5 False
If the first expression is true, then the result
is already true without evaluation of the next intA = 5 AndAlso intB = intC False
AndAlso expression. Similarly, if the first expression is Not (intA = 5) AndAlso intB = intC True
false, then the result is already false
If a true statement is already encountered, intA = 2 OrElse intB = intC True
OrElse the result is true Not (intA = 5) OrElse intB = intC True

Logical operators have the following order of precedence: negation


(Not), conjunction (And, AndAlso), inclusive disjunction (Or, OrElse),
and exclusive disjunction (Xor).
OPERATORS
Concatenation Operators

Concatenation the merging of two or more


strings into one longer string.
Symbol used: &, +
Example:
sngV = 20
lblResult.Text = The velocity is & sngV & m/s.
Result is: The velocity is 20 m/s.
EXPRESSING PROGRAM ALGORITHMS
Natural Language Algorithms
A program algorithm can be expressed in many ways and
the easiest way is by using a natural language (like the
English language).
Example: Write an algorithm in creating a program that
will convert a temperature expressed in degrees Celsius
to Kelvin (O K = O C + 273.15).

Step-by-step solution:
1. Obtain the input temperature in degrees Celsius from the user;
2. If the input is a valid number then compute the Kelvin value by
adding 273.15 to the input temperature. If not, ask the user for
another input;
3. Display the computed value to the user.
EXPRESSING PROGRAM ALGORITHMS
Pseudocode
An informal code that combines both the natural
language and a programming language.
Example (Kelvin to degrees celsius converter):

[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
algorithmto 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
Sample Program

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 location.
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

You might also like