Download presentation
Published byBerenice Rose Modified over 9 years ago
1
Chapter 2 Beginning Problem-Solving Concepts for the Computer
CMPF144 Introduction to Problem Solving and Basic Computer
2
Overview Constants and Variables Data Types Functions Operators
Expressions and Equations
3
Objectives Differentiate between variables and constants.
Differentiate between character, numeric, and logical data types. Identify operators, operands and resultants. Identify and use functions Identify and use operators according to placement in hierarchy chart. Set up & evaluate expressions and equations using variables, constants, operators, and the hierarchy of operations.
4
Constant A value (content in memory cell, can be alphabetic and/or numeric) that will never change during the execution of computer program is referred to by its name (location of memory cell) Name is given in CAPITAL LETTER to differentiate from variable Example: PI = 3.142
5
Variable A value (can be any data type)that might change during the execution of computer program is referred to by its name also known as identifier there are rules for naming and using variables Example: number_of_student = ?? total_amount =?? depends on program execution
6
Rules for Naming and Using Variables
Name a variable according to what it represents. Do not use spaces. Start a variable name with a letter. Do not use a dash or any other symbol that is used as a mathematical operator. Consistent usage of variable name. Consistent use of upper, lowercase characters in variable names Use naming convention specified by your company
7
Incorrect 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 item ClientName Rate of pay Rate-Pay Uses a mathematical operator PayRate Quantity per customer Quantity/Customer QuantityPerCustomer 6% sales tax 6%_sales_tax Starts with a number SixPercentSalesTax @ SalesTax Client address Client_address_for_client_of_XYZ_corporation_in_California Too long ClientAddress Variable name introduced as Hours Hrs Inconsistent name Hours Hours_worked pp 51, WHAT’s WRONG WITH THIS? Q1
8
Data Type a classification of the type of data that a variable or constant can hold in computer programming Each data type consists of a set of permitted values, which is known as data set Common categories of data type: Numeric Character String Logical Date
9
Numeric Include all types of numbers
Can be used for numeric calculations 2 + 1 = ?? Subtypes include: Integer Whole number Can be positive of negative Example: 5297, -376 Real number Also known as floating point numbers Can be represented in scientific notation Example: , 2.3E5 (2.3 x 105)
10
Numeric Data Types and Their Data Sets
11
Character / String Consists of all single-digit numbers, letters and special characters available to the computer Placed within quotation marks Example: “a”, “2”, “=“ Cannot be used for calculation even though some are numbers “2” + “1” = ?? Uppercase letter is different from lower case letter “A” ≠ “a” more than one character are combined together to form a string
12
Character / String (cont.)
Can be compared and sorted in alphabetical order (computer gives each character an ASCII number) Banana > Apple Joan > James A < a Can be joined together via concatenation (by using + operator) “Hello” + “World” = “HelloWorld” “4” + “4” = ??
13
Character / String Data Types and Their Data Sets
14
Logical Only consists of two value – True or False
Used to make yes-or-no decisions Example: result = True badInput = “yes” CreditOK = 1
15
Logical Data Types and Their Data Sets
16
Date Is a numeric data type as mathematical operations can be performed onto the value Allows user to subtract one date from another date
17
Examples of Data Types
18
Examples of Data Types (cont.)
pp. 47, QUESTIONS, Q5
19
Functions Small sets of instructions that perform specific tasks and return values. Requires parameter General syntax : FunctionName (parameter) Example: Sqrt(N) – What is this?? (╥﹏╥) Syntax might vary for different programming language Can be used repeatedly in a program to shorten the problem-solving time and increase program’s readability
20
Examples of Functions
21
Examples of Functions (cont.)
22
Examples of Functions (cont.)
23
Examples of Functions (cont.)
24
Operators Tells the computer HOW to process data (example: add, subtract…) Informs the computer WHAT type of processing (example: mathematical, logical…) Two main concepts: OPERAND and RESULTANT Types of operators: Mathematical (standard mathematical calculation) Relational (to program decision) Logical (to connect relational expression and to perform operations on logical data)
25
Mathematical Operators and Their Computer Symbols
26
Relational Operators and Their Computer Symbols
27
Logical Operators and Their Computer Symbols
28
NOT Logical Operators
29
AND Logical Operators
30
OR Logical Operators pp. 48, QUESTIONS, Q9
31
Hierarchy of Operations
pp. 48, QUESTIONS, Q10
32
Expressions and Equations
Used to process data (operands) through operators Example: width * height Equation: Also known as assignment statements Stores the resultant of an expression in a memory location of a computer through equal “=” symbol (assignment operator) area = width * height
33
Expressions and Equations (cont.)
34
Setting Up a Numeric Expression
Mathematical expression: X (3Y + 4) - Appropriate computer representation: 4Y X + 6 X * ( 3 * Y + 4) – 4 * Y / ( X + 6) pp. 49, PROBLEMS, Q1
35
Setting Up a Mathematical Equation
Example: Appropriate computer representation: Only one variable on the left and an expression on the right of the “=” sign Y + 3 = X ( Z + 5) Y = X * ( Z + 5) - 3 pp. 49, PROBLEMS, Q2
36
Setting Up a Relational Expression
Given an expression : Appropriate computer representation: Computer can’t understand “is lesser than” X is lesser than Y + 5 X < Y + 5
37
Evaluating a Mathematical Expression
pp. 49, PROBLEMS, Q5
38
Evaluating a Relational Expression
39
Evaluating a Logical Expression
40
Evaluating Equation That Uses Both Relational and Logical Operators
pp. 51, PROBLEMS, Q14
41
Developing a Table of All Possible Resultants of a Logical Expression
One unknown - A. Two combinations: A can be either True or False
42
Developing a Table of All Possible Resultants of a Logical Expression (cont.)
Two unknowns - A and B. Four combinations: B can be either True or False for each value of A.
43
Developing a Table of All Possible Resultants of a Logical Expression (cont.)
Three unknowns - A, B, and C. Eight combinations.
44
Developing a Table of All Possible Resultants of a Logical Expression
pp. 50, PROBLEMS, Q13
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.