chapter 2 Beginning Problem-Solving Concepts for the Computer-revised111111.pptx

Full Transcript

BEGINNING PROBLEM-SOLVING CONCEPTS FOR THE COMPUTER Chapter 2. OVERVIEW ▪ Constants and Variables ▪ Rules for Naming and Using Variables ▪ Data Types ▪ Numeric Data ▪ Character Data—Alphanumeric Data ▪ Logical Data ▪ Other Data Types ▪ Rules for Data Types ▪ Examples of Data Types ▪ How the Computer...

BEGINNING PROBLEM-SOLVING CONCEPTS FOR THE COMPUTER Chapter 2. OVERVIEW ▪ Constants and Variables ▪ Rules for Naming and Using Variables ▪ Data Types ▪ Numeric Data ▪ Character Data—Alphanumeric Data ▪ Logical Data ▪ Other Data Types ▪ Rules for Data Types ▪ Examples of Data Types ▪ How the Computer Stores Data ▪ Functions ▪ Operators ▪ Expressions and Equations ▪ Examples OBJECTIVES When you have finished this chapter, you should be able to: 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. 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. CONSTANTS AND VARIABLES ▪ The computer uses constants and variables to solve problems. ▪ 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. ▪ Constants can be any type of data —numeric, alphabetical, or special symbols. ▪ A variable may change during processing. ▪ In many languages variables are called identifiers since the name identifies what the value represents. RULES FOR NAMING AND USING VARIABLES 1. All variable names must begin with a letter of the alphabet or an underscore( _ ). 2. The rest of the variable name can include any letter, any number, or the underscore. You can't use any other characters, including spaces, symbols, and punctuation marks. 3. Create as short a name as possible but one that clearly represents the variable. 4. Be consistent when using upper- and lower-case  After you have introduced variable name thatisrepresents characters. In somea languages HOURS a different a specific data item, this exact name must be used in all places where variable namevariable than Hours. the data item is used. EXAMPLES 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, stored in a file or database. PROCESSING DATA DATA TYPE CONT. ▪ The data the computer uses are of many different types. Computers must be told the data type of each variable or constant. ▪ The most common data types are ▪ numeric, ▪ character, ▪ and logical. ▪ Each data type has a data set. ▪ A data set is the set of values that are allowed in a particular data type. DATA TYPE: NUMERIC DATA ▪ Numeric data include all types of numbers. ▪ Numeric is the only data type that can be used in numeric calculations. ▪ 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. DATA TYPE: CHARACTER DATA— ALPHANUMERIC DATA ▪ The character data set, or 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. ▪ An upper case letter is considered a different character from a lower case letter. ▪ When more than one character are put together, the computer considers this item a String Data—derived from a string of characters. DATA TYPE: LOGICAL DATA AND OTHER DATA TYPES ▪ Logical data consist of two values in the data set— the words True and False. ▪ These are used in making yes-or-no decisions. Note that: There are other data types available to most programmers, such as the date data type and userdefined data types. DATA TYPE CONT. RULES FOR DATA TYPES 1. The data that define the value of a variable or a constant will most commonly be one of three data types: numeric, character (including character string), or logical. 2. The programmer designates the data type during the programming process. The computer then associates the variable name with the designated data type. 3. 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. 4. 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. 5. Any numeric item that must be used in calculations resulting in a numeric result must be designated as numeric data type. All other numbers should be designated as character or character-string data types, even if data are all numbers, EXAMPLES OF DATA TYPES HOW THE COMPUTER STORES DATA ▪ The computer stores data internally in memory locations. ▪ These data are found by the variable names used by a program. ▪ Each variable name is given a memory location, and each memory location can hold one and only one value at a time. ▪ When a user enters a new value into the variable location, the previous value is destroyed. ▪ These memory locations are temporary, as the internal memory is a volatile memory. ▪ Data and instructions are temporarily stored in the computer’s internal memory during the processing. HOW THE COMPUTER STORES DATA CONT. ▪ When data, information, or programs have to be kept for future use, they are stored externally on an external storage medium such as a hard disk drive in storage areas called files. ▪ There are basically two types of files: program files and data files. ▪ Program files contain the instructions to tell the computer what to do. ▪ Data files contain the data required to execute the program files and the result of program. OPERATORS ▪ Operators are the data connectors within expressions and equations. ▪ They tell the computer how to process the data. ▪ They also tell the computer what type of processing (mathematical, logical, or whatever) needs to be done. ▪ 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. ▪ Example in the expression 5 + 7, the + is the operator, 5 and 7 are the operands, and 12 is the resultant. ▪ Operands can be constants or variables. ▪ The data type of the operands and the resultant depends on the operator TYPES OF OPERATORS 1. Mathematical. 2. Relational. 3. Logical. MATHEMATICAL OPERATORS RELATIONAL OPERATORS LOGICAL OPERATORS HIERARCHY OF OPERATIONS ▪ These mathematical, relational, and logical operators have a hierarchy, or precedence, an order in which their operations take place (see Table 2.8). 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 ▪ An equation stores the resultant of an expression in a memory location in the computer through the equal (=) sign. ▪ The expression above would be used as part of an instruction in the equation Area = Length * width ▪ Evaluate the expression or equation means to test for correctness using actual data. EXPRESSIONS AND EQUATIONS CONT. EXAMPLE 1: SETTING UP A NUMERIC EXPRESSION ▪ modify the following mathematical expression for computer use: ▪ The appropriate computer expression would be the following: ▪ All variables, constants, and operators have to be on the same line. ▪ There must be an operator between variables and/or constants. EXAMPLE 2: SETTING UP A MATHEMATICAL EQUATION ▪ modify the following mathematical expression for computer use: ▪ The appropriate computer expression would be the following: EXAMPLE 3: SETTING UP A RELATIONAL EXPRESSION ▪ A relational expression is used to make decisions. Given the expression ▪ the programmer would change its form to the following: EXAMPLE 4: EVALUATING A MATHEMATICAL EXPRESSION ▪ Assume the programmer has written the expression ▪ The programmer uses the following values to evaluate the expression: EXAMPLE 5: EVALUATING A RELATIONAL EXPRESSION ▪ Assume the programmer has written the expression ▪ The programmer uses the following values to evaluate the expression: EXAMPLE 6: EVALUATING A LOGICAL EXPRESSION ▪ Assume the programmer has written the expression ▪ The programmer uses the following values to evaluate the expression: EXAMPLE 7: EVALUATING AN EQUATION THAT USES BOTH RELATIONAL AND LOGICAL OPERATORS ▪ Assume the programmer has written the expression ▪ The programmer uses the following values to evaluate the expression: QUESTIONS QUESTION

Use Quizgecko on...
Browser
Browser