0% found this document useful (0 votes)
3 views

CH2-Beginning Problem-Solving Concepts for the Computer

Chapter 2 introduces fundamental problem-solving concepts in programming, focusing on constants, variables, data types, functions, operators, and expressions. It emphasizes the importance of naming conventions for variables and constants to enhance code readability and maintainability. The chapter also covers the evaluation of mathematical and logical expressions, providing guidelines for setting up and modifying equations for computer use.

Uploaded by

Hamza Sayes
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

CH2-Beginning Problem-Solving Concepts for the Computer

Chapter 2 introduces fundamental problem-solving concepts in programming, focusing on constants, variables, data types, functions, operators, and expressions. It emphasizes the importance of naming conventions for variables and constants to enhance code readability and maintainability. The chapter also covers the evaluation of mathematical and logical expressions, providing guidelines for setting up and modifying equations for computer use.

Uploaded by

Hamza Sayes
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 62

Chapter 2: Beginning Problem-

Solving Concepts for the


Computer

SW 100: Principles of Programming and Problem Solving

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

1- Computational, problems involving some kind of mathematical processing;

2- Logical, problems involving relational or logical processing, the kinds of processing used in
decision making on the computer; and

3- Repetitive, problems involving repeating a set of mathematical and/or logical


instructions.

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

• A constant is a value—that is, a specific alphabetical and/or numeric value—that never


changes during the processing of all the instructions in a solution.

• 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

3. Naming conventions allow the code to be easily maintained.

4. The software should perform more efficiently by using consistent naming of modules
and variables.

5. There should be an increase in performance expectation.

6. Naming conventions should produce a clean, well-written program.

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.

2. Do not use spaces in a variable name; for example, use HoursWorked.

3. Start a variable name with a letter.

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

5. Consistent usage of variable name.


For example, if the data item hours worked has the variable name of Hours, Hours
must be used consistently. You may not use Hrs or HoursWorked to represent the same
data item.
Why?

• Consistent use of upper, lowercase characters in variable names.

• Use naming convention specified by your company.

11
Incorrect Variable Names

12
Data Types

• To process solutions, the computer must have data.

• 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

• The subtypes of numeric data include integers and real numbers.

• 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.

• within the limitations of the computer or the language.

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.

Example: “4” + “3” = “43” (not “7”).

Note: The concatenation operator varies with each language.

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

• These are used in making yes-or-no decisions.

• 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.

• Each language has a set of functions within it.

• Most languages allow programmers to write their own functions.

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.

• Example: The square root function, Sqrt(N).


• This function will calculate the square root of N.
• Sqrt is the name of the function.
• N is the data needed to calculate the square root and, therefore, it is the parameter.

27
Functions

28
Functions

29
Functions

30
Functions

31
Operators

• Operators are the data connectors within expressions and equations.

• The types of operators used in calculations and problem solving include:


• mathematical,
• relational, and
• logical 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

• For example, in the expression

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.

• An expression processes data, the operands, through the use of operators.

• 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

• An equation stores the resultant of an expression in a memory location in the computer


through the equal sign.

Area = Length * Width

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.

• The right-hand side is processed before the assignment is made.


N=N+1

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:

• The appropriate computer expression would be the following:

46
Setting Up a Numeric Expression

Follow these steps to complete the expression:


1- Write all parentheses, operands, and operators on a single line with the dividend first,
followed by the divisor:

2- Insert all implied operators.

3- Insert all parentheses where the hierarchy needs to be reordered.

47
Setting Up a Mathematical Equation

• A mathematical equation might be given to the programmer in the following form:

• The programmer has to modify the equation so that it is in the form of an assignment
instruction:

48
Setting Up a Mathematical Equation

Follow these steps to complete the equation:


1- Write all parentheses, operands, and operators on a single line:

2- Use mathematical rules to complete the equation so that there is one variable on the left
side of the equal sign.

3- Follow the steps in Example 1 to complete the equation

49
Setting Up a Relational Expression

• A relational expression is used to make decisions. Given the expression


X is less than Y + 5

The programmer would change its form to the following

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

• Assume the programmer has written the expression

• The programmer uses the following values to evaluate the expression:

53
Evaluating a Logical Expression

Assume the programmer has written the expression

The programmer uses the following values to evaluate the expression:

54
Evaluating a Logical Expression

55
Evaluating an Equation That Uses Both
Relational and Logical Operators

• Assume the programmer has written the following


equation:

• The programmer uses the following values to evaluate


the equation:

56
Evaluating an Equation That Uses Both
Relational and Logical Operators

57
Dr.Hanin Abdulrahman 2023 58
59
60
61
End Chapter 2

62

You might also like