[go: up one dir, main page]

0% found this document useful (0 votes)
45 views37 pages

Programming Fundamentals: Chapter Outline

The document provides an overview of programming fundamentals including algorithms and syntax, variables and data types, and operators. It defines key concepts such as algorithms, program statements, syntax, variables, data types, literals, constants, and operators. It also describes variable naming conventions and scopes as well as the different arithmetic, comparison, logical, and other operators used in programming.

Uploaded by

Adrian Masacayan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views37 pages

Programming Fundamentals: Chapter Outline

The document provides an overview of programming fundamentals including algorithms and syntax, variables and data types, and operators. It defines key concepts such as algorithms, program statements, syntax, variables, data types, literals, constants, and operators. It also describes variable naming conventions and scopes as well as the different arithmetic, comparison, logical, and other operators used in programming.

Uploaded by

Adrian Masacayan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

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

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


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

▪ A constant is a container of values that do not


change. It is a meaningful name that represents
the same value throughout a program.

▪ 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%

' Default to Double.


Const DefaultDouble As Double = 54.3345612R

' 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
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 numbers only (decimals are
automatically rounded off)
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 as textboxes, forms and
other controls
VARIABLES AND DATA TYPES
Integer Data Type
The range of values for an integer data type
(default value = 0) 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 that cannot
tolerate rounding off errors.
VARIABLES AND DATA TYPES
Text Data Type
String
▪ Text data can be stored using the String data type.
▪ It can store up to 2 billion Unicode characters
(approximately 700,000 A4-size pages).
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 point 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 (that
has a value of "").
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 codes within the procedure
in which it is declared
Module scope Available to all codes within the module,
class, or structure in which it is declared
Namespace scope Available to all codes 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 to
construct a formula.
▪ Visual BASIC uses the following general categories of
operators:
1. Arithmetic Operators
2. Shortcut Operators
3. Comparison Operators
4. Logical Operators
5. Concatenation Operators
▪ Take note that the operator categories are not operators
themselves but rather just a way of classifying the
operators.
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 operation is as follows: (), ^, *, /, \, mod, +, -
P EMD I M A S
▪ Example: 8 \ 10 / 5 mod 3 = 1
OPERATORS
Shortcut Operators
The assignment operator (=), which assigns the value of
its right operand to its left operand, 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
Shortcut Operators
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 in 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 Xor intB = 5 False
If the first expression is false, then the result
is already false without evaluation of the intA = 5 AndAlso intB = intC False
AndAlso next expression. If the first expression is True, Not (intA = 5) AndAlso intB = intC False
then the second expression is evaluated.
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.
▪ Operators: &, +
▪ Example:
sngV = 20
lblResult.Text = “The velocity is ” & sngV & “ m/s.”
‘Result is: The velocity is 20 m/s.
OPERATORS
Operator Precedence
✓ operators shown in one row in a line of
code are evaluated before those that
come in later rows
✓ operators on the same row having the
same precedence levels are evaluated
from left to right in an expression
✓ PEMDIMAS
✓ UMIMASBCELA
OPERATORS
Operator Precedence
1. Unary (in C: ++, --, !) However, you can use
2. Multiplicative (*, /) PARENTHESES to force some
3. Integer Division (\) parts of an expression to be
evaluated before others.
4. Modulus (Mod)
5. Additive (+, -)
6. String Concatenation (&)
7. Bitwise shift (in C: <<, >>)
8. Comparison (=, <, <=, >, >=; have equal precedence)
9. Equality (=, <>)
10.Logical (Not, And/AndAlso, Or/OrElse, Xor)
11. Assignment or shortcut (=, +=, -=, *=, /=, \=, …)
OPERATORS
Quotient Result for \ and Mod
For integer division wherein the
(a) number being divided
(dividend or numerator) or (b) the
divisor (or denominator), or (c)
both dividend and divisor are
decimal numbers, they must first
be converted to integer values
before doing the division process
e.g. 3.5 \ 2 = 4 \ 2;
3.25 \ 2 = 3 \ 2 = 1;
3 \ 1.5 = 3 \ 2 = 1;
3 \ 1.25 = 3 \ 1 = 3;
3 \ 0.5 → undefined since 0.5
becomes 0
3.5 \ 1.5 = 4 \ 2 = 2
3.5 \ 1.25 = 4 \ 1 = 4
EXPRESSING PROGRAM ALGORITHMS
Ways of expressing program algorithms
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 for 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 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

You might also like