LESSON 2 - Program Elements
LESSON 2 - Program Elements
LESSON 2
PROGRAM ELEMENTS
INTRODUCTION
Programming is more than just a means of instructing a computer to perform tasks. The
language also serves as a framework within which we organize our ideas about
computational processes. Programs serve to communicate those ideas among the
members of a programming community. Thus, programs must be written for people to
read, and only incidentally for machines to execute.
LEARNING OUTCOMES
At the end of this module, the students are expected to:
1. Learn the different data types.
2. Understand the use and declaration of constants and variables.
3. Understand the expressions.
COURSE CONTENTS
Data Types
It is a classification that describes the following:
What values can be held by the item
How the item is stored in computer memory
What operations can be performed on the item
String: describes data items that are Alphanumeric characters; They can represent
text, words, or sentences. Typically enclosed in double quotes.
Character: single characters, such as letters, digits, or symbols. They are typically
represented using single quotes ('')
Integer: whole numbers without any decimal points. They can be positive, negative,
or zero.
Boolean: represent logical values, which can be either true or false. They are
commonly used in conditional statements and logical operations.
“Hello World”
“Alice”
“Bob123”
Constants
Values that do not change during program execution. It has a fixed value.
Type of Constant:
Variables
These are placeholders used to store and manipulate data within a program. They serve
as containers for storing values that can change during the execution of the program. Can
only hold one value at a time.
Types of Variables
1. Numeric Variable: contain data or hold numeric value only
Types of Numeric Variable
a. Integer Numeric Variable – holds integer value only
b. Real Numeric Variable – holds real value only
2. Character Variable: contain character value only
3. String Variable: contain or holds string values only
Variable Declaration
Variables must be declared before they are used for the first time in a program.
Some languages require all variables to be declared at the beginning of the
program, others allow variables to be declared at the beginning of each module,
and others allow variables to be declared anywhere at all as long as they are
declared before their first use.
Example:
string myName
float unit_cost
integer quantity
Variable Initialization
Variable initialization is the process of assigning an initial value to a variable at the
time of its declaration. Initializing a variable ensures that it starts with a known
value, which can be useful for preventing unexpected behavior and errors in a
program.
If a variable is declared without initialization, some programming languages
automatically assign a default value to it based on its data type. For example, in
Java, numeric types are initialized to 0, booleans to false, and reference types to
null if no explicit value is provided.
Example:
string myName = “Juanita”
float unit_cost=10.00
integer quantity = 65
Expressions
A group of program elements consisting of operands and operators that can be evaluated
and produce a result.
Expressions can represent calculations, comparisons, or other operations.
Example of an Expression
Operators: Operators are symbols or keywords that perform operations on one or more
operands to produce a result. Common types of operators include:
arithmetic operators (+, -, *, /)
comparison operators (==, !=, <, >)
logical operators (&&, ||, !)
assignment operators (=, +=, -=)
Types of expressions:
Arithmetic Expressions
String Expressions
Boolean Expressions
Arithmetic Expressions
An expression consisting of numeric constants or numeric variables separated by an
arithmetic operator which yields a numeric value.
These expressions use arithmetic operators to perform addition, subtraction,
multiplication, division, and other mathematical computations.
sum = myNumber + 5
product = 6*4
Arithmetic Operators
1. Combining something of type float with something of type either integer or float
always results to float
2. Combining anything with division sign always yields to float
3. Combining two things of type integer either addition (+), subtraction(-) or
multiplication (*) yields to integer type
4. Placing a minus sign (-) in front of an arithmetic expression does not change its type.
2.0 + 1 = 3.0
2.0 / 1 = 2.0
2 / 2 = 1
5 / 2 = 2
(6 + 2) / 2
20 / 4 * 9 % 3
= 20 / 4 * 9 % 3
= 5 * 9 % 3
= 45 % 3
= 0
String Expressions
Consists of string constants or string variables separated by string operator which yield
to string value.
String expressions use only one operator, the + operator. The effect of + operator on
string is concatenation.
Concatenation appending the value of the string.
A = “Jesus ”
B = “will ”
C = “guide us” Output
D =‘ ’
D = A + B+ C D= “Jesus will guide us”
Boolean Expressions
Expression which yields either TRUE(1) or FALSE (0) value.
Either a Relational Expression or Logical Expression.
Relational Expressions
Boolean expression consisting of numeric or string expressions, numeric or string
variables and numeric or string constant separated by relational operator.
Both of the operand must be of the same type, that is either both numeric or both
string
Relational Operator
Rate = 100
Total_CS = 50
Total_IT = 64
Comtech = 40
Names = “JOY JOAN KZ”
Logical Expressions
An expressions consisting of one or more relational expressions separated by the
logical operator that yields either TRUE or FALSE
Truth Table
CS = 15
IT = 0
NAME = “KC”
BAYAD = 500
ASSESSMENT GUIDE/QUESTIONS:
A =10
B=5
C = “apple”
D = “mango”
E= 100
F=200
a. (A > 10 AND B < 20) OR (C == "apple" AND D != "orange") AND (E <= 100 OR F > 200)
b. (A > 10 AND B < 20) OR (C == "hello" AND (E <= 5 OR D != "world"))