CH2-Beginning Problem-Solving Concepts for the Computer
CH2-Beginning Problem-Solving Concepts for the Computer
1
• Constants and Variables
• Data Types
• Functions
Overview • Operators
• Expressions and Equations
2
1. Differentiate between variables and
constants.
2. Differentiate between character,
numeric, and logical data types.
3. Identify operators, operands, and
resultants.
4. Identify and use functions.
Objectives 5. Identify and use operators according to
their placement in the hierarchy chart.
6. Set up and evaluate expressions and
equations using variables, constants,
operators, and the hierarchy of
operations.
3
Computer Problems
2- Logical, problems involving relational or logical processing, the kinds of processing used in
decision making on the computer; and
4
Important Concepts to Learn
•Two of the most fundamental concepts that you will learn in this chapter are the constant
and the variable.
•They are the building blocks of the equations and expressions that ultimately make up
solutions to computer problems.
•Other concepts that are essential to developing computer solutions to problems are
operators and functions.
•Operators are the many signs and symbols that show relationships between constants and
variables in the expressions and equations that make up the solution.
5
Constants and Variables
• In contrast, the value of a variable may change during processing. In many languages
variables are called identifiers since the name identifies what the value represents.
• For example, because the value of PI does not change, it would be a constant and defined
within the program. This constant may be given a name, but the only way to change the
value of the constant is to change the program. Any constant may be given a name. This
allows an easier access to constants. Many name conventions stipulate that named constants
be given names containing all capital letters to easily distinguish them from variables.
However, this may vary between companies in which you may work.
6
Constants
and
Variables
7
Naming Conventions for Constants and
Variables
The convention for naming variables may differ with companies as well as languages. It is
very important to have all programmers within an environment follow the specified
conventions.
Why?
1. It allows several programmers to work on the same project without the problem of
conflicting variable and module names.
2. It allows programs to be easily read because there is only one consistent name for a
variable.
8
Naming Conventions for Constants and
Variables
4. The software should perform more efficiently by using consistent naming of modules
and variables.
9
Rules for Naming and Using Variables
1. Name a variable according to what it represents, that is, Hours for hours worked,
PayRate for rate of pay, and so on.
4. Do not use a dash (or any other symbol that is used as a mathematical operator) in a
variable name.
10
Rules for Naming and Using Variables
11
Incorrect Variable Names
12
Data Types
• 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. This information is printed in the form of reports.
• Computers must be told the data type of each variable or constant. A few languages and applications also
use the date as a data type. Other languages allow the programmer to define data types
13
Data Type
• Numeric Data
• Character Data—Alphanumeric Data
• String Data
• Logical Data
• Other Data Type:
• Date Data Type
• User Defined Data Type
14
Numeric Data
• Integers are whole numbers, such as 5,297 or -376. They can be positive or negative.
• Real numbers, or floating point numbers, are whole numbers plus decimal parts.
• A real number can be expressed in scientific notation, such as 2.3E5 or 5.4E–3.
• Examples: rate of pay, salary, angles, distance, or radius, account number or a zip code.
15
Data Set
• Each data type has a data set, the set of symbols necessary to specify a datum as a
particular data type.
• A data set is the set of values that are allowed in a particular data type.
16
Character Data—Alphanumeric Data
• The character data set, sometimes called alphanumeric data set, consists of all single digit
numbers, letters, and special characters available to the computer—a, A, Z, 3, #, &, and so
forth—placed within quotation marks.
• . The ASCII (American Standard Code for Information Interchange) character set contains
256 characters.
17
ASCII
18
String Data
When more than one character are put together, the computer considers this item a string
—derived from a string of characters.
Character and string data can be compared and arranged in alphabetical order in the
following way.
• The computer gives each character a number.
• The numbers are compared to see which is larger and are then arranged in ascending
numeric order.
• B has a larger number representing it than A, B is placed after A.
19
Concatenation
• Character data or string data can be joined together with the + operator in an operation
called concatenation.
20
Logical Data
• Logical data consist of two values in the data set—the words True and False. (Some
languages accept yes, T, and Y for True, and no, F, and N for False as part of the data set.)
• For example, logical data type might be used to check someone’s credit record; True
would mean her credit is okay, and False would mean it’s not okay.
21
Data Types and
Their Data Sets
22
Examples of Data Types
23
Examples of Data Types
24
Functions
• Functions are small sets of instructions that perform specific tasks and return values.
• Because they are basic tasks that are used repeatedly in the problem solving process, by
using them a programmer or user can shorten the problem-solving time and improve the
readability of the solution.
25
Functions
• The form of a function is the name of the function followed by an open parenthesis,
followed by the data needed to perform the function and concluded by a closed
parenthesis:
FunctionName(data)
• The value of the result of the function is returned in the name of the function.
26
Functions
• Functions use data. The data is listed as part of the function and are called parameters.
27
Functions
28
Functions
29
Functions
30
Functions
31
Operators
32
Operators
• 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.
33
Operators
5+7
o The + is the operator,
o 5 and 7 are the operands,
o and 12 is the resultant.
34
Operators and Their Computer
Symbols
35
Operators and Their Computer
Symbols
36
Definitions of the Logical Operators
37
Definitions of the Logical Operators
38
Definitions of the Logical Operators
39
Expressions and Equations
• Expressions and equations make up part of the instructions in the solution to a computer
problem.
• For example, to find the number of square feet in a room you would multiply the length of
the room by the width in the expression
Length * Width
40
Expressions and Equations
The resultant of the expression Length * Width would then be stored in a memory location
called Area.
41
Expressions and Equations
• Equations are often called assignment statements because the variable on the lefthand
side of the equal sign is assigned the value of the expression on the right-hand side.
• The equal sign does not mean equals; instead, it means replaced by or is assigned the
value of.
42
Expressions and Equations
43
44
45
Setting Up a Numeric Expression
• Assume the programmer has to modify the following mathematical expression for
computer use:
46
Setting Up a Numeric Expression
47
Setting Up a Mathematical Equation
• The programmer has to modify the equation so that it is in the form of an assignment
instruction:
48
Setting Up a Mathematical Equation
2- Use mathematical rules to complete the equation so that there is one variable on the left
side of the equal sign.
49
Setting Up a Relational Expression
50
Evaluating a Mathematical Expression
• To find out if proposed solutions are correct, it is important for the programmer to
evaluate, or test, all expressions and equations.
Assume the programmer has written the expression The programmer uses the following
values to evaluate the expression:
51
Evaluating a Mathematical Expression
52
Evaluating a Relational Expression
53
Evaluating a Logical Expression
54
Evaluating a Logical Expression
55
Evaluating an Equation That Uses Both
Relational and Logical Operators
56
Evaluating an Equation That Uses Both
Relational and Logical Operators
57
Dr.Hanin Abdulrahman 2023 58
59
60
61
End Chapter 2
62