0% found this document useful (0 votes)
17 views40 pages

PoP Lecture 3

The document discusses important programming concepts such as constants, variables, data types, and rules for naming variables. Constants cannot change value while variables can. Data types include integer, floating point, character, string, and boolean. Variables must be named according to specific rules and declared with the appropriate data type.
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)
17 views40 pages

PoP Lecture 3

The document discusses important programming concepts such as constants, variables, data types, and rules for naming variables. Constants cannot change value while variables can. Data types include integer, floating point, character, string, and boolean. Variables must be named according to specific rules and declared with the appropriate data type.
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/ 40

1

Important Problem Solving Concepts


Lecture 3

09th Feb, 2024

ICT172: Principles to Programming


Lecture Notes
Lesson Objectives
•By the end of the lesson, the student should be able to:

1. Differentiate between a constant and a variable


2. List the types of operators
3. Perform different kinds of operations
4. Write equations in computer forms

ICT172: Principles to Programming


Lecture Notes 2
Constant
•A constant is a value that never changes during the
processing of all the instructions in a solution.

•Constants can be numeric (e.g. 2), character (e.g. ‘e’),


special symbols (e.g. #) or string (e.g. “queen”).

•Once the constant is given a value, it cannot be


changed during the execution of the program.

•For example, the value of PI (which is 3.14159) should be


defined as a constant in a program.
ICT172: Principles to Programming
Lecture Notes 3
Variable
•Unlike constants, the value of a variable may change
during processing.
•A variable actually denotes a symbolic name for a
specific memory location used to hold values used in by
a program.
•Variables can be any data type, just as constants can.

•Consider the fee for the academic year. This data item
should be given a variable name because the fees do
change each year.
ICT172: Principles to Programming
Lecture Notes 4
Rules for naming constants & variables
1. Variables should be well-defined. That is, name a variable
according to what it represents.
2. Do not use spaces in a variable name.
3. Start a variable name with a letter. It is wrong to start variable name
with a digit or another character (except the underscore, _).
4. Do not use a dash (or any other symbol that is used as a
mathematical operator) in a variable name.
5. Be consistent when using upper- and lower-case characters. In C++
HOURS is a different variable name than Hours.
6. As a common naming convention, use upper case for the first
character in each of the words in the name, with no spaces
between words in the name.
7. Named constants be specified in all UPPER-CASE CHARACTERS.
ICT172: Principles to Programming
Lecture Notes 5
Some Invalid Variable Names
Data Item Incorrect Variable Name Problem Corrected Variable
Name
Hours worked Hours Worked Space between words HoursWorked
Name of client CN Does not define data ClientName
item
Rate of pay Pay-Rate Uses a mathematical PayRate
operator
Quantity per Quantity/customer Uses a mathematical QuantityPerCustom
customer operator er
6% salestax 6%_sales_tax Starts with a number SixPercentSalesTax
Or SalesTax
Client address Client_address_for_client_ Too long ClientAddress
of_XYZ_corporation
ICT172: Principles to Programming
Lecture Notes 6
Data Type
•The computer needs data to process a solution.
•Data are unorganized facts. They go into the computer
as input and are processed by the program.
•What is returned to the user is output or information.
•The data the computer uses are of many different
types.
•Computers must be told the data type of each variable
or constant.

ICT172: Principles to Programming


Lecture Notes 7
Data Type
•Data type specifies which type of value a variable can
hold or which operations can be performed on a given
set of values.
•Data types are an essential concept in programming
languages to ensure proper memory allocation and
efficient use of resources.
•The most common data types are:
• Integer
• Floating-point (decimal number)
• Character
• String
• Boolean (logical data)
ICT172: Principles to Programming
Lecture Notes 8
Integers
•Represents whole numbers without any fractional part.
•They can be positive or negative.
•Integers can be used in numeric calculations.

•Examples: 0, 7, -42.

•C++ uses int to represent integers.

ICT172: Principles to Programming


Lecture Notes 9
Floating-Point Numbers (Decimal numbers)
•Represents numbers with a decimal point or in
exponential notation.
•Like integers, floating-point numbers can be positive or
negative.
•Floating-point numbers can also be used in numeric
calculations.
•Examples: 3.14, -0.005, 2.5e3. (the e stands for times 10
to the power of)
•C++ uses float and double to represent real numbers.
ICT172: Principles to Programming
Lecture Notes 10
Character data
•The character data set, consists of all single digit
numbers, letters, and special characters available to
the computer.

•Example:
•‘A’, ‘Z’, ‘3’, ‘#’, ‘&’,
•Note the use of single quotation marks.

•An upper-case letter is considered a different


character from a lower-case letter.

•C++ uses char to represent characters


ICT172: Principles to Programming
Lecture Notes 11
String data
•Strings represents a sequence of characters.

•In a program, strings are specified within a double-


quotation.

•Some languages do not differentiate between


characters and strings.

•However, C++ distinguishes between characters

•C++ uses String for String data type.


ICT172: Principles to Programming
Lecture Notes 12
Boolean (logical) data
•Represents logical values, typically true or false.
•Boolean values are used in making yes-or-no decisions.
•For example, logical data type might be used to check
someone’s HIV aids status; True would mean he/she is
positive, and False would mean he/she is negative.
•A teacher could use logical data type for attendance
tracking; True would mean the student is present, and
False would mean the student is absent.
•C++ uses bool to represent Boolean data
ICT172: Principles to Programming
Lecture Notes 13
Date and Time data
•Represents specific points in time or durations.
•Examples: 2024-02-06, 14:30:00

•In many programming languages, Date and Time are


often treated as a distinct data type that may
internally be implemented as a numeric or string type,
or a combination of both.

•C++ has no built-in data type for date and time

ICT172: Principles to Programming


Lecture Notes 14
Rules for Specifying Data Types
1.The programmer designates the data type during the
programming process. The computer then associates
the variable name with the designated data type.

2.Data types cannot be mixed. For example, string data


cannot be placed in a variable memory location that
has been designated as numeric, and vice versa. When
the computer expects a certain data type, the user
must use that type or the computer will return an error
message.
ICT172: Principles to Programming
Lecture Notes 15
Rules for Specifying Data Types
1. Each of the data types uses what is called a data set.
• The numeric data uses the set of all base 10 numbers, the plus sign
(+), and the negative sign (-);
• The character type uses the set of all characters available to the
computer
• The logical data type uses the set of data consisting of the words
True and False. The use of any data outside the data set results in
an error.

2. Any numeric item that must be used in calculations


resulting in a numeric result must be designated as
numeric data type.
3. All other numbers should be designated as character or
string data types, even if data are all numbers, as in zip
codes.
ICT172: Principles to Programming
Lecture Notes 16
Data Types & Data Sets
Data Type Data set Example
Integer All whole numbers 3580
(Both positive and -45
negative)
Real All real numbers -3792.91
(Whole + decimal) 4739416.0
0.00246
Character All letters, numbers ‘A’, ‘a’, ‘M’, ‘z’, ‘k’
(Surrounded by single quotation marks) and special symbols ‘1’, ‘5’, ‘7’, ‘8’, ‘0’
‘+’, ‘=’, ‘(‘, ‘%’, ‘$’
String Combination of more “Ghana”
(surrounded by double quotation mark) than one character “0246392963”
“08202020”
Boolean
ICT172: Principles to Programming
Lecture Notes
True False True False 17
Examples
•What is the appropriate data type for the following?
1. The price of an item
2. Bank account number
3. Quantity of item bought
4. The name of a university
5. A voter ID number
6. Your sex

ICT172: Principles to Programming


Lecture Notes 18
Operators
• Operators are the data connectors within expressions and
equations.
• They are used to perform operations on one or more
operands.
• They also tell the computer what type of processing needs
to be done.
• The types of operators used in calculations and problem
solving include:
• Arithmetic
• Relational
• Logical
• Assignment
ICT172: Principles to Programming
Lecture Notes 19
Operand and Resultant
•The operand and the resultant are two concepts related
to the operator.
•Operands are the data that the operator connects and
processes.
•The resultant is the answer that results when the
operation is completed.
•For example, in the expression 5 + 7, the + is the operator,
5 and 7 are the operands, and 12 is the resultant.
•Note: Operands can be constants or variables.
ICT172: Principles to Programming
Lecture Notes 20
Mathematical Operators

Mathematical Computer
Operation Result
Operator Symbol
Addition + 3.0+5.2 8.2
Subtraction - 7.5-4.0 3.5
Multiplication * 8.0*5.0 40.0
Division / 9.0/4.0 2.25
Integer Division / 9/4 2
Modulo Division % 9%4 1

ICT172: Principles to Programming


Lecture Notes 21
Relational Operators
Computer
Relational Operator Operation Result
Symbol
Equal to
== 5==7 false
Less than
< 5<7 true
Greater than
> 5>7 false
Less than or equal to
<= 5<=7 true
Greater than or equal to
>= 5>=7 false
Not equal to <> (C++ uses
5<>7 true
!=)
ICT172: Principles to Programming
Lecture Notes 22
Quick Review
• What is a constant?
• What is a variable?
• Mention any two (2) rules to follow when naming variables
• Mention any six of the Arithmetic operators used by computers
• Write the operators for the following operations in computer
form
• Less than
• Greater than
• Less than or equal to
• Greater than or equal to
• Equal to
• Not equal to

ICT172: Principles to Programming


Lecture Notes 23
Logical Operators
•Logical operators are used to connect relational
operators and to perform operations on logical data
(and expressions).

Logical Operator Computer Symbol Operation Result

NOT NOT (C++ uses !) NOT true false


AND AND (C++ uses &&) true AND true true

OR OR (C++ uses ||) True OR False true

ICT172: Principles to Programming


Lecture Notes 24
Logical OR (||) Operator
•When the OR operator is used, the resultant is True if
any one or both of the operands are True. When both
of the operands are False, the resultant is False.

Truth Table for the logical OR (||)


Operand A Operand B Operand A OR Operand B
True True True
True False True
False True True
False False False
ICT172: Principles to Programming
Lecture Notes 25
Logical AND (&&) Operator
•When the AND operator is used, the resultant is True if
and only if both of the operands are True. When either
or both of the operands are operands are False, the
resultant is False.
Truth Table for the logical AND (&&)
Operand A OperandB Operand A AND Operand B
true true true
true false false
false true false
false false False
ICT172: Principles to Programming
Lecture Notes 26
Logical NOT (!) Operator
•The resultant of the NOT operator changes in an
operand from True to False, or from False to True.

•This operation is sometimes called reversing the value


of the operand.

Truth Table for the logical NOT (!)


Operand NOT Operand
true false
true true
ICT172: Principles to Programming
Lecture Notes 27
Operator Precedence
• Mathematical, relational, and logical operators have a
hierarchy, or precedence which refers to the order in which
their operations take place.
Precedence for Operators
st
1 Power (exponentiation)
nd
2 MOD, * /
rd
3 +-
th
4 Relational operators
th
5 NOT
th
6 AND
th
7 OR
ICT172: Principles to Programming
Lecture Notes 28
Evaluating Mathematical Expressions

•Example 1: Evaluate, 6 * 12 / 16.0 + 222


Solution:

Operation Resultant
1. 6*12 72
2. 72/16.0 4.5
3. 4.5+222 226.5

ICT172: Principles to Programming


Lecture Notes 29
Evaluating Mathematical Expressions

Example 2: Evaluate, 5 * (2 + 3) - 4 * 3/(6 + 6).

Operation Resultant
1.
2.
3.
4.
5.
6.
ICT172: Principles to Programming
Lecture Notes 30
Evaluating Relational Operators
•Assume the programmer has written the expression
𝐴−2>𝐵
•Evaluate the expression given that: A = 6 and B = 8

Operation Resultant
1. A- 2 4
2. 4 > 8 False

ICT172: Principles to Programming


Lecture Notes 31
Evaluating Logical Operators

•Assume the programmer has written the expression


𝐴 𝐴𝑁𝐷 𝐵 𝑂𝑅 𝐶 𝐴𝑁𝐷 𝐴
• Evaluate the expression given that: A = True; B = False and C = True

Operation Resultant
1. A AND B False
2. C AND A True
3. False OR True True

ICT172: Principles to Programming


Lecture Notes 32
Evaluating Logical Operators

• Assume the programmer has written the expression


𝐹 = 𝑁𝑂𝑇 𝐴 < 𝐵 𝐴𝑁𝐷 (𝐶 𝑂𝑅 𝐴)
• Evaluate the expression given that: A = 4; B = 2; C = True and D = False

Operation Resultant
1. A<B False
2. C OR A True
3. NOT False True
4. True AND True True

ICT172: Principles to Programming


Lecture Notes 33
Writing Expression and Equation in Computer Form

ICT172: Principles to Programming


Lecture Notes 34
Expressions and Equations
•To be able to develop useful solutions, the
programmer needs skills in generating expression and
equations.
•For the beginning programmer, a big part of learning
to solve problems on the computer is learning how to
write and evaluate expressions and equations.
•Expressions and equations make up part of the
instructions in the solution to a problem.
•An expression processes data, the operands, through
the use of operators.
ICT172: Principles to Programming
Lecture Notes 35
Guidelines when writing C++ Equations
• Use Arithmetic Operators
• +, -, /, *, %
• Use Parentheses for Priority
• Use parentheses to control the order of operations.
• Assignment Operators
• Use the assignment operator (=) to store the result in a variable.
• Include cmath for Mathematical Functions in C++
• If you need more advanced mathematical functions, include the <cmath>
library.
• Use Appropriate Data Types:
• Ensure that the data type of variables matches the result type of the
operation.
• Be Careful of Integer Division
• Integer division truncates the decimal part. If you need a floating-point result,
ensure at least one operand is a floating-point number

ICT172: Principles to Programming


Lecture Notes 36
Writing Mathematical Expressions

Mathematical Expression Computer Expression


4𝑌 𝑋 ∗ 3 ∗ 𝑌 + 4 − (4 ∗ 𝑌)/(𝑋 + 6)
𝑋 3𝑌 + 4 −
𝑋+6
𝑋 = (𝑋 − 𝑌)2 𝑋 = 𝑋 − 𝑌 ∗ (𝑋 − 𝑌)
𝑌 + 3 = 𝑋(𝑍 + 5) 𝑌 =𝑋∗ 𝑍+5 −3
𝑋 is less than 𝑌 + 5 𝑋 < (𝑌 + 5)
𝐴−2>𝑏 (𝐴 − 2) > 𝑏
Average of three number (𝑁𝑈𝑀1 + 𝑁𝑈𝑀2 + 𝑁𝑈𝑀3)/3

ICT172: Principles to Programming


Lecture Notes 37
Exercise
• Write the following mathematical equations in computer form:
3Z−1
1. X = 5Y +
4 3Z+1 −Y
Z+Y
2. X = Y + 3Z −
Z−3
3. X= −b + (b 2 − 4ac)
4. The area of lab 5
5. The sum of three numbers
6. The percent increase of a value given the
beginning number and the ending number.
7. The number of miles given a number of feet. (Use
5,280 feet per mile.)
ICT172: Principles to Programming
Lecture Notes 38
More Exercise

Convert the following into computer form


a. 32 times a plus b
b. The character that represents 8
c. The string that represents the name Julie Nelson.
d. (b2 - 4ac) / 2a
e. (a + b)/c(ef)-gh
f. (-b + (b2 - 4ac)) / 2a

ICT172: Principles to Programming


Lecture Notes
40

End of Lecture 3

ICT172: Principles to Programming


Lecture Notes

You might also like