Numerical Constant - May Be An Integer or Real Number. Characteristics
Numerical Constant - May Be An Integer or Real Number. Characteristics
PROGRAM ELEMENTS
Constants
Variables
Expressions
CONSTANT
A constant is a literal value that does not change during program execution. It is fixed value.
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 “,”
“computer”
“523.05”
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.
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)
Types of Variables
1. Numeric Variable – is a variable that can contain data or hold numeric constants (or value)
only. Types of Numeric Variables
3. String Variable – is a variable that can contain or hold string constants (or values) only.
EXPRESSIONS
Types of Expressions
1. Arithmetic expressions
2. String Expressions
3. Boolean Expression
Arithmetic Expressions
The general format of a numeric expression is as follows: operand operator operand operator operand
Example: 4*2
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
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:
2.0 / 1 = 2.0
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
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))
= b. 5 * (12 * 10 /5)
= c. 5 * (120/5)
= d. 5 * 24
= e. 120
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 !=)
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.
Rate = 100
Total_CS = 50
Total_IT = 64
Comtech = 40
Names = “JOY JOAN KZ”
Relational Expression Interpretation
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.
NOT = TRUE
False
False
False
False
OR truth table
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.
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