You are on page 1of 13

LESSON 2

PROGRAM ELEMENTS AND STRUCTURE

STUDENT LEARNING OBJECTIVES:

At the end of the chapter, the students are expected to:

 To know the basic elements that is used to construct algorithms.


 To understand the nature of each element.
 To determine the basic structure that will be used to formulate algorithm.

PROGRAM ELEMENTS

The basic elements of algorithm are as follows:

 Constants
 Variables
 Expressions

CONSTANT

A constant is a literal value that does not change during program execution. It is fixed value.

Two types of constant

1. Numerical constant - may be an integer or real number.


Characteristics:
a. Commas and blank spaces cannot be included within the constant.
b. The constant can be preceded by a minus sign “-“ if desired.
c. The value of constant cannot exceed specified minimum and maximum bounds.
For each type of constant, these bounds will vary from one complier of a
program to another. This is controlled by the software.

Two types of Numeric Constant

1.1. Integer Numeric Constant – is a whole number. It has an exact Value and does not contain a
decimal point.
Example: Several valid decimal integers are shown below:
90 -1 700 5000
The following decimal integer constants are written incorrectly for the reason started:
16,000 illegal character “,”
15 28 34 illegal character “blank space”
243-100-255 illegal character “-“
1.2. Real Numeric constant – number that contains a decimal point or an exponent for both). It has
approximate value.
Example: Several valid real constants are shown below:
3.30 415.05
The following are not valid real constant for the reasons stated:
20 either a decimal point or an exponent must be present
61,456.8 illegal character “,”

2. Character Constant – is a single character, enclosed in an apostrophe (single quotation


marks)

Example: Several characters, constants are shown below:

‘g’ ‘w’ ‘7’ ‘*’ ‘‘

3. String Constant – consist of any number of consecutive characters (including none)


enclosed in double question marks.

Example: Several string constants are shown below:

“computer”

“523.05”

“The right answer is.”

Variables

A variable is a portion of the computer’s main memory used to store a numeric or a string
constant. It is also a named memory location, it represents a certain value. The value stored in the
variable may be changed during program execution but it can only hold one value at a time.

Rules in giving variable names:

1. Must contain only letters, numbers and/or underscore “_”


2. Must begin with letters.
3. Must end either with a letter or a number.
4. Must contain a maximum of eight (8) characters.
5. Underscores must not appear consecutively.
6. Must be unique, that is not a name of another variable.
7. Must not be a keyword or reserved word of any programming language.

Example: Several variables are shown below:

S Sum S_um Total2 a5

Variable name are not case sensitive, that is upper and lower case letters are not differentiated.

The following are not valid variable names for the reason stated:
NO STUD illegal character (blank space)

NO_STUDE exceeds eight characters

8CTR does not start with a letter

CTR1_ ends with an underscore

MA_LE underscore appears consecutively

Types of Variables

1. Numeric Variable – is a variable that can contain data or hold numeric constants (or value)
only. Types of Numeric Variables

a. Integer Numeric Variable – holds only integer constant.


b. Real Numeric Variable – holds real constant.

2. Character Variable – is a variable that can contain character constant.

3. String Variable – is a variable that can contain or hold string constants (or values) only.

EXPRESSIONS

An expression is a group of program elements consisting of operands and operatios. Operands


can be a constatnt or a variable Operators can be a constant or a variable. Operators can be arithmetic
and relational. Expressions always yield a value of a certain type depending on the type of expressions
used.

Types of Expressions

1. Arithmetic expressions
2. String Expressions
3. Boolean Expression

Arithmetic Expressions

An Arithmetic Expression is an expression consisting of numeric constants or numeric variables


separated by arithmetic operator which yields a numeric value.

The general format of a numeric expression is as follows: operand operator operand operator operand

Example: 4*2

4 and 2 are operands


*(Multiplication Symbol) is operator
There are five arithmetic operators in algorithm. They are:
Operator Purpose
+ Addition
- Subtraction
* Multiplication
/ Division or div operator
% Modulus Division

Truncated division is sometimes referred to a modulus operator (%). It is a division in which the
decimal portion of the quotient is always removed or truncated without rounding.

Examples:

10 % 3 = 1

98 % 10 = 8

3 % 100 = 3

1 % 2 = 1

Figure 2-1. Type rules for arithmetic expressions

How to be determine the type of an arithmetic expression:

1. Combining something of type real with something of type either integer or real always yields
something of type real.
2. Combining anything with the division sign always yields something of type real (in C
language, an integer division will yield an integer).
3. Combining two things of type integer with either addition sign (+), subtraction sign (-) or the
multiplication sign (+) yields something of type integer.
4. Placing a minus sign (-), in front of an arithmetic expression does not change its type.

Examples:

1. Real and integer will yield real value.


2.0 + 1 = 3.0
2.0 - 1 = 2.0
2.0 / 1 = 1.0
2.0 * 1 = 2.0
2. Division will yield real value.

2.0 / 1,0 = 2.0

2.0 / 1 = 2.0

3. Integer Division (It truncates the remainder part of the quotient)


2 / 2 = 1
5 / 2 = 2

Complex arithmetic expressions

Numeric expression can be mixed to contain several operands and operators. Parenthesis can be used as
well to group a certain portion of the expression.

Examples:

6 + 2/2 = a. 6 + 2/2

= b. 6 + 1.0

= c. 7.0

(3 + 4) * 10 = a. (3+4) *10

= b. 7 * 10

= c. 70

10 * 4 * 2.0 = a. 10 * 4 * 2.0

= b. 40 * 2.0

= c. 80.0
Figure 2-2. Precedence Rules for Arithmetic expressions

Order of evaluation for arithmetic expression

1. If parentheses are present, then they determine the order of operation.


2. If the order is not determined by parenthesis, then *, / and % operations are evaluated
before + and -.
3. If there is still the question of which operation to do first, the compelling operations in their
left to right order.
Examples of evaluation:

1. 20 / 4 * 9 % 3
= a. 20 / 4 * 9 % 3-
= b. 5*9%3
= c. 45 % 3
= d. 0
2. 5 * 3 / 5 8* 10 -20 * 2
= a. 5 * 3 / 5 * 10 – 20 * 2
= b. 15 / 5 * 10 20 * 2
= c. 3 * 10 * - 20 * 2
= d. 30 – 20 * 2
= e. 30 – 40
= f. -10
3. – (15 – 8) + 10 * (11 + 6)
= a. -7 + 10 * (11 + 6)
= b. -7 + 10 * 17
= c. -7 + 170
= d. 163
4. 5 * (12 * 10 / (25 – 20))

= a. 5 * (12 * 10 / (25 – 20))

= b. 5 * (12 * 10 /5)

= c. 5 * (120/5)

= d. 5 * 24

= e. 120

Constructing Arithmetic Expressions

In practice, arithmetic expressions are not normally expressed in programmable forms instead they are
normally expressed in algebraic form. For example,

2X + XY
3XY

In programming language, the example just mentioned will be considered as incorrect or invalid. For
one, operands must be separated by an operator and only valid symbols must appear in the expression.
In algorithmic language, only the following symbols and elements must be included in the expression.
( ) * / + - symbols
Numeric constants
String constants
Variable names
Examples of conversion:
2X + XY
1. 3XY
= (2 * X) / (3 * X + Y)

2. (3 + 4) (8 + 9) = (3 + 4) * (8 + 9)

String Expression
A string expression is an expression consisting of string constants or strings variables separated
by string operator, which yields a string value.
The genera; format of a string expression is as follows:
operand operator operand operator operand
Example:
A = “Jesus”
B = “will”
C = “guide us”
D = ``
D = A+B+C
D, A, B, and C are all string variables.
+ operator
Output: D = “Jesus will guide us”
String expressions use only one operator, the + operator. The effect of + operator on string is
concatenation.
Concatenation – means appending the value of the second operand to the end of the first value of the
first operand.

Boolean Expressions
A Boolean expression is either a relational expression or logical expression, which yields either a TRUE
or FALSE value. (Since this module is Algorithm based on C programming language, the equivalent of
TRUE is 1 and FALSE is 0)

Relational Expressions
A relational expression is a Boolean expression consisting of numeric or string expressions, numeric or
string expressions, numeric of string variables and numeric or string constants, separated by a relational
operator.
The general format of a relational expression is as follows:
Operand operator operand . . .
Both of the operands must be of the same type that is either both numeric and both string. The operand
can be a numeric or string expression, numeric or string variable, or numeric or string constant. The
following are the relational operators:

Operator Meaning
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
= Equal to (in C, equal is ==)
<> Not equal to (in C, equal is !=)

Figure 2-3 Rules in evaluating Relational expressions:

1. When evaluating a relational expression, the value of the left operand is compared to the
value of the right operand to determine whether the entire expression is true or false.
2. If the operand is a variable then its corresponding value will be used for comparison.
3. If the operand is a numeric or string expression, the said expression is evaluated first to
determine its final value that will be used for comparison.
4. If a portion of numeric or string expression is variable, then its corresponding value will be
used to arrive at the final value of the said expression.
5. When comparing string values, each character or both operands are compared according to
its ACII code equivalent. The comparison is made from left to right one character at a time
using the value of the ASCII code as the basis of comparison. If one operand is shorter (i.e.,
contain lesser number of characters) than the other, the shorter operand is padded with
spaces to make the length of both operands equal.

Examples of relational Expressions evaluation:

Rate = 100
Total_CS = 50
Total_IT = 64
Comtech = 40
Names = “JOY JOAN KZ”
Relational Expression Interpretation

Rate > 50 TRUE


Total_CS < > Total_IT TRUE
Comtech < > 40 FALSE
Names = “Arnie” FALSE
“DOG” = “dog” FALSE

Logical Expressions
A logical expression is an expression consisting of one or more relational expressions separated
by the logical operator that yields either TRUE or FALSE. The general format of a logical expression is as
follows:
The relational expression before the logical operator is optional only of the logical NOT is used.

The following are the logical operators:


NOT
AND
OR
To evaluate a logical expression, the so-called truth table must be noted. Each logical has operator has a
corresponding truth table and are as follows:
NOT truth table

NOT True = FALSE

NOT = TRUE

False

AND truth table

True AND True = TRUE

True AND = FALSE

False

False AND = FALSE

False

False AND = FALSE

False
OR truth table

True OR True = TRUE

True OR False = TRUE

False OR True = TRUE

False OR False = FALSE

When evaluating a logical expression, the relational expressions must be evaluated first to determine
whether the said relational expression is true or false. Once evaluated, the appropriate truth table must
be referred to determine the final evaluation.

Example of Logical expressions evaluation:


Consider the following variables and its value.
CS = 15
IT = 0
NAME = `KC’
BAYAD = 500
1. (CS > = 10) AND (CS < = 20)
TRUE AND TRUE = TRUE
2. (IT = 1) OR (IT = 2)
FALSE OR FALSE = FALSE
3. NOT (NAME=’JOHN’)
NOT FALSE = TRUE
4. NOT (BAYAD > 1000)
NOT FALSE = TRUE
5. (BAYAD>600) AND (BAYAD < 800>
FALSE AND TRUE = FALSE

Evaluating Complex Logical Expressions


When evaluating complex logical expressions, the precedence rule of logical expression
evaluation must be taken into consideration to determine the order by which the operations are to be
evaluated. According to the precedence rule, operators with a higher hierarchy and priority are
evaluated first.
Hierarchy or Priority of Logical Operators
Expressions in parenthesis first
NOT second
AND third
OR fourth

In the event that the operation involves operators with equal priority, the left to right rule is
followed. The so-called left to right states that when the operation involves operators with equal
priority, the left most operation is evaluated first.
For nested parenthesis, that is, operation in parenthesis within operation in parenthesis, the
inner parenthesis is evaluated first prior to the outer parenthesis.
Examples of evaluation:
Consider the following variables and its value:
A=1
NAME = “JOANA”
CCMIT = 100
1. (A =1) OR (A = 2) OR (A = 3)
TRUE OR FALASE OR TRUE = TRUE
2. (NAME= “JOANNA”) AND (CCMIT = 100) OR (A>=50)
=TRUE AND TRUE OR FALSE
=TRUE OR FALSE = TRUE
PROGRAMM STRUCTURE
Writing Algorithms
An algorithm is a sequence of statements in the algorithmic language. The statements are
executed – the instructions in them are obeyed- one after another in the order in which they are
written. We can frame an algorithm for display as follows:

ALGORITHM algorithm-name
Statements of algorithm
END algorithm-name
ALGORITHM EXPRESSIONS
OUTPUT ’10 + 5 =’ 10 + 5
OUTPUT ’10 – 5 =’ 10 – 5
OUTPUT ’10 * 5 =’ 10 * 5
OUTPUT ’10 / 5 =’ 10 / 5
OUTPUT ’10 / 5.0 =’, 10 / 5.0
OUTPUT ’10 % 5 =’, 10 5
END EXPRESSIONS

Executing EXPRESSIONS cause the following printout to be produced:


10 + 5 = 15
10 - 5 = 5
10 * 5 = 50
10 / 5 = 2
10 / 5.0 = 2.0
10 % 5 = 0
In this book algorithms are printed using all capital letters for easy readability and to make them stand
out from the surrounding text. This is not necessarily the best way to write out algorithms by hand. A
good form for handwritten algorithm is use lowercase letters and underline keywords such as
ALGORITHM, END and OUTPUT, to make them stand out.

You might also like