0% found this document useful (0 votes)
5 views11 pages

LESSON 2 - Program Elements

This document provides an overview of programming elements, focusing on data types, constants, variables, and expressions. It outlines the learning outcomes for students, including understanding various data types such as strings, integers, and booleans, as well as the rules for defining variables and their initialization. Additionally, it explains expressions in programming, including arithmetic, string, and boolean expressions, along with their evaluation rules.
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)
5 views11 pages

LESSON 2 - Program Elements

This document provides an overview of programming elements, focusing on data types, constants, variables, and expressions. It outlines the learning outcomes for students, including understanding various data types such as strings, integers, and booleans, as well as the rules for defining variables and their initialization. Additionally, it explains expressions in programming, including arithmetic, string, and boolean expressions, along with their evaluation rules.
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/ 11

ICT Specialization 2 1

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.

Float (floating-point): represent numbers with fractional parts.

Polytechnic University of the Philippines


ICT Specialization 2 2

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”

single characters, such as ‘A’


letters, digits, or symbols ‘@’

Different Data Types and Examples

Constants
Values that do not change during program execution. It has a fixed value.
Type of Constant:

Numeric Constant: may be an integer or real numbers


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
compiler of a program to another.

Integer Numeric Constant: a whole number, it has an exact value and


does not contain decimal point

Polytechnic University of the Philippines


ICT Specialization 2 3

Example: 90 -1 700 5000


The following are illegal numeric constant:
16,000 illegal character “,”
15 28 34 illegal character “blank space”
243-100-255 illegal character “-”

Real Numeric Constant: number that contains a decimal point or an


exponent (or both). It has approximate value.

Example: 3.30 415.05


The following are not valid real constant
20 Either a decimal point or an exponent must be present
1,258.02 illegal character “,”

Character Constant: a single character, enclosed in an apostrophe (single


quotation marks)
Example: ‘F’ ‘w’ ‘7’ ‘*’ ‘’

String Constant: consist of any consecutive character enclosed in double


quotation marks.
Example: “Fee” “wi” “790”

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.

Rules in Defining a Variable Name


Variable names must be one word. The name can contain letters, digits, hyphens,
or underscores. No language allows embedded spaces in variable names, and
most do not allow punctuation such as periods, commas, or colons.
Variable names must start with a letter. Some programming languages allow
variable names to start with a nonalphabetic character such as an underscore.
Almost all programming languages prohibit variable names that start with a digit.
Must be unique. Variable names must be unique within the scope where they are
declared. This means that no two variables within the same scope can have the
same name.

Polytechnic University of the Philippines


ICT Specialization 2 4

Must not be a keyword or reserved word of any programming language. Variable


names must not be the same as keywords or reserved words of the programming
language being used. Keywords are predefined words in the language that have
special meanings and are reserved for specific purposes, such as defining control
structures or data types.
Example: S Sum Num1 F_Name
The following are not valid
NO STUD illegal character (blank space)
8ctr does not start with a letter
MA_ _LE underscore appears consecutively

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.

Polytechnic University of the Philippines


ICT Specialization 2 5

Example:
string myName = “Juanita”
float unit_cost=10.00
integer quantity = 65

Assigning Values to Variables


Assigning values to variables is a fundamental operation in programming that
involves storing a data value in a variable for later use.
Assignment statement is assigning a value to a variable.
Example:
myAnswer = myNumber * 2

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

Operands: These are the values or variables involved in the 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 (=, +=, -=)

Polytechnic University of the Philippines


ICT Specialization 2 6

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.

Polytechnic University of the Philippines


ICT Specialization 2 7

Float and integer will yield float value

2.0 + 1 = 3.0
2.0 / 1 = 2.0

Division will yield float value

2.0 / 1.0 = 2.0


2.0 / 1 = 2.0

Integer Division (it truncates the remainder part of the quotient)

2 / 2 = 1
5 / 2 = 2

Complex Arithmetic Operators


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

(6 + 2) / 2

Precedence Rules Arithmetic Expression

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


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

Polytechnic University of the Philippines


ICT Specialization 2 8

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

Polytechnic University of the Philippines


ICT Specialization 2 9

Relational Operator

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 variable, then 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 final value of the said expression

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

Rate > 5 TRUE


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

Polytechnic University of the Philippines


ICT Specialization 2 10

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

(CS >=10) AND (CS <=20) (IT ==10) OR (IT ==20)


TRUE AND TRUE FALSE OR FALSE
TRUE FALSE

Polytechnic University of the Philippines


ICT Specialization 2 11

ASSESSMENT GUIDE/QUESTIONS:

3. Given the following values, evaluate the boolean expressions below

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"))

Polytechnic University of the Philippines

You might also like