Presentation on theme: "Introduction to Data and control structures Problem Solving Learning Objectives......................................page 2page 2 Introduction to Data."— Presentation transcript:
Introduction to Data and control structures Problem Solving Learning Objectives......................................page 2page 2 Introduction to Data and Control Structures Computer Programming and the Data Processing Cycle.......... page 3page 3 Overview of Data Data Hierarchy -- Units of Data.............................. page 4page 4 Data Types -- Numeric and Alphanumeric.................... page 5page 5 Statements and Expressions.............................. page 6page 6 Overview of Control Structures Understanding Structure -- Controlling Logic Flow............. page 7page 7 The Three Control Structures............................. page 8page 8
Introduction to Data and control structures Learning Objectives Upon completion of this lesson, the student will be able to 1.List and give examples of three phases of data processing cycle. 2.Define and compare and contrast counter and accumulator variables 3.Identify and give examples of data hierarchy and the units of data. 4.Identify and give examples of numeric and alpha numeric data. 5.Define and compare and contrast the use of a statement and expression in a programs source code and object code. 6.List and describe and use the assignment operator, arithmetic operators, relational operators, and logical operators. 7.Evaluate and write conditional statements and define Boolean values. 8.List and describe the three control structures.
Introduction to Data and control structures Data and control structures Computer programming and data processing cycle All computer programs are designed and written to implement data processing cycle for a given problem. The data processing cycle consist of three phases. The data processing cycle : Understanding this basic concept is understanding what computer programming is all about. For a give problem, the programmer’s job is to identify the order of the instructions that must be executed during each of the three phases in order to produce the desired outcome. No mater what kind of program is being developed, it must implement the data processing cycle. They are Input phase -> Processing phase -> Output phase Input : it is generally helpful to know what the program is supposed to produce prior to identifying what data will be needed to produce the desired output. If the programmer is writing a program that will play, the programmer will have to decide what input is needed for the game to work, and where the input will come from. For instance, what input will come from the keyboard or the mouse? Will counters and accumulators be needed to calculate the scores? Will the programmer generate random numbers for the game.? As these decisions are made, the programmer is actually deciding what variables are needed, what they are going to be named, and what type of data will they store. Counters : Counters are variables that keep track of the number an event has occurred, by adding a certain value (such as 1) to the counter. Accumulators : accumulators are variables that keep track of a series of values (such as a total) by adding various values (such as grocery items) to the accumulator.
Introduction to Data and control structures Data and control structures Processing : One you understand the output requirement and the input has been identified, the processing phase determines how the data is transformed into the desired information for the output form the program. If the input data is from the keyboard, or data read from a file, or data that is calculated by the processor, place to store those values (variables) will be needed. Separating the values that will be input into the program from values that will be calculated by the program will help the programmer determine what process the program will need to generate the desired output. Output : Often the end-user who ask a programmer to develop a program already has a mind what the program should produce. For example, if the programmer is writing a program that will produce a payroll report, the manager who will use that report will tell the programmer what the finished report should look like. Their specifications will indicate what fields should become included, if the report will be divided into sections for each department, and if it will include sub-totals or other summary values at the end.
Introduction to Data and control structures Over view of data Data hierarchy – Units of data Bit : Computer hardware stores and processes data one but at a time. Electronically, the bit represents the “on” and “off” electrical statement of a circuit. Digitally an “on” bit is represented as a 0. Bits are the smallest data of unit storage. Byte : A byte is a series of bits that represent characters. The fourth generation of computers introduced the microprocessor that stores and processes data in eight-bit-bytes. Electronically, a byte is a series of eight “on “ or “off” electric voltage states. Digitally a byte of data is represented as a series of 1’s and 0’s. Character : In computing, a character of data is the representation of letters, numbers, punctuation marks, and control characters in bytes. To assure compatibility among hardware and software, characters are encoded so that the same series of “on” and “off” bits are assigned to the same character consistently. Two of the most common encoding schemes are ASCII and Unicode. For instance the user sees the uppercase “A”. Digitally in ASCII, it is represented as 01000001. Electronically to the hardware, It is off-on-off-off-off-off-off-on ASCII : American standard code for information interchange (ASCII) was developed by the American National standards Institute (ANSI) for standardized characters encoding of Latin alphabet (the English language alphabet) Unicode : Unicode was developed by the Unicode consortium for standardized character encoding of the alphabet of every language.
Introduction to Data and control structures Over view of data When electrical bits and digital 1’s and 0’s have been transformed into bytes that represent character that describes some attribute about a person, place, or thing. These facts are composed of the smaller units of data (bits, bytes, and characters) as described above. The large units of data represent specific values that are stored in fields that make up the records in data file Values : When data characters are combined in a meaning full way to represent value, such as putting together the characters “J” “A” “C” “K” to represent a students name, the value of “Jack” is stored in a field that contains the values of the first name of the students file. Fields : The fields in this data file store the data values for the students first name, students last name, student identification number, and semester average. Each of these fields describes one attribute of a student. Records : This file contains two records: One for Jack Sprat and one for Mary Contrary. The value of “Jack” in the first name field represents the beginning of the first record. The value of ”95” in the semester average field representing the ending of the first record. All of the information on that row is part of the record is belonging to Jack Sprat. The value of “Mary” in the First Name field begins the second record in the file. The values of “92” in the semester average fields ends the second record. File : A data file is a set of related records. This file contains two (the rows), four fields per record (the columns), and eight values (the actual content of the individual fields) First name Last name Student ID Semester average JackSprat11111111195 MarryContrary22222222292
Introduction to Data and control structures Over view of data Data types - Numeric and Alphanumeric Computers can store and process two types of data : Numeric and Alphanumeric Numeric : Numeric data are data that will be used in mathematical expressions. An example of Numerical data is Semester average because this value is calculated by averaging together all of the students semester grades. Numeric data types require more memory for storing and processing the Alphanumeric data. Integer (or whole) number values and real (or decimal) number values. Alphanumeric : Alphanumeric values consist of alphabetic characters and numeric digits that represent non-numerical values. Examples of alphanumeric data are First Name, Last Name, and Student Identification. Although the values in the Student Identification field contain only digits, they are alphanumeric values because they only identify the student with numbers, no mathematical expressions will be performed on them.
Introduction to Data and control structures Over view of data Statements and Expressions Each program language has its own unique set of rules (syntax) to write statements and expressions that accomplish the programs operations. Programmers write statements and expressions that accomplish the programs operation. Programmers write statements and expressions to create the source code to implement algorithm (solution). Each language utilizes various operators to identify specific processing within a statement or expression. Statements : In the source code, every expression is a statement, but every statement is not an expression. The fundamental difference is that an expression represents a value. Some source code statements do not directly represent a value. Some source codes statements do not directly represent data or manipulated data. These statements do not appear in the translated object code. Expressions : Expressions represent values and the operations performed on those values.
Introduction to Data and control structures Over view of data Categories of statements include Comment statements (source code only) : Comment statements are used to document and explain the program to human readers. The statements are not translated into object codes. There for they do not appear in the final executable form. Directive Statements (source code only) : Direct statements are used to command and control the language translator program (assembler, compiler, interpreter) during conversion from source code to object code. Declarative statements (Source code and object code) : Declarative statements are used to create identifiers and names for values that the program will use (variables and constants procedures (functions, methods, etc.), and any other named entity that is defined by the programmer. Instruction statements (Source code and object code) : Instruction statements perform the input, processing, and output operations on the data values. Operators : Operators are symbol (s) that represent an action or an operation to be performed. Operators generally have two operands (values). Some typical operators found in most programming languages are : Assignment operators : The assignment operator (=) is used in assignment statements to take the value on the right of the operator and store it in the named memory location (variable) on the left of the operator. For example, the following assignment statement stores the value of zero. In the variable named count : count = 0
Introduction to Data and control structures Over view of data Arithmetic operators : Arithmetic operators direct the program to mathematic calculations on numeric data. They include the additional operators ( + ), the subtraction operator ( - ), the multiplication operator ( * ), the division operator ( / ). Some languages include the modulo ( % ) and the exponentiation operator ( ^ ). Mathematical expressions that include more than one arithmetic operator follow the order of operator precedence. ( + ) : Expressions using the addition operator ( + ) direct the processor to calculate the sum of the numbers to the left and to the right of the operators. For instance, the expression 1+2 generates the sum of 3. ( - ) : Expressions using the subtraction operator ( - ) direct the processor to calculate the difference of the numbers to the left and to the right of the operator. For instance, the expression : 3-2 generates the difference of 1. ( * ) : Expressions using the multiplication operator ( * ) direct the processor to calculate the product of the numbers to the left and to the right of the operator. For instance, the expression 3*2 generates the product of 6. ( / ) : Expressions using the division sign ( / ) direct the processor to calculate the quotient of the numbers to the left and to the right of the operator. ( % ) : Expressions using the modulo operator ( % ) Direct the processor to calculate the remainder of the integer number on the left divided by the right of the operator. The modulo operator cannot be used on the decimal values. For instance, the expression 5% generates the remainder 1 ( the quotient is 2, but the % generates only the remainder.) ( ^ ) : Expressions using the exponentiation operator ( ^ ) direct the processor to calculate the value of number on the left of the operator (the base) raised to the power of the number on the right of the operator (the exponent). For instance, the expression 3^2 generates the value of 9 Operator precedence : Mathematical expressions using more than one operator evaluate each operator in the following order, parentheses, exponentiation, multiplication and division, addition and subtraction. In the case of expressions which two operator share the same order, the expression is evaluated from left to right.
Introduction to Data and control structures Over view of data Here is an example of how the order of operator precedence changes the results of a mathematical expression: 4+3-2*3 generates the value 15 if the order of precedence is not followed. How ever, when the order of operator precedence is followed, the same expression generates the value 1, because: 2*3 is evaluated first, generating the value 6 4+3 is evaluated second, generating the value 7 7-6 is evaluated third, generating the value 1 Using parentheses will override the order of operator precedence. For example, the same expression with parentheses generates the value 7 : 4+(3 2) * 3 3-2 is evaluated first, generating the value 1 1*3 is evaluated second, generating the value 3 4+3 is evaluated third, generating the value 7
Introduction to Data and control structures Over view of data Relational operator : Relational operators are used in condition statements to generate what are know as Boolean values. These operators evaluate how to data values “relate” to each other. Conditional statements : Conditional statements are expressions that are used in the decision structure to control the flow of instruction processing. For example, if the evaluation of the condition generates true, the execution of the programs instructions will take a certain path. If the result is false, al alternate path will be followed. Boolean values : Boolean values are either true or false, which is equivalent to either (true) or off (false) states of the electronic circuit. = equal to : “Equal to” evaluates true if the value on the left of the operator is the same as the value on the right. It evaluates false if they are not. For example: 10=10 generates true “dog”=“dog” will generate false > Greater than : “Greater than” evaluates true, if the value on the left of the operator is greater than the value on the right it evaluates false if it is not. For example: 5>0 will generate true, 5>10 will generate false Or equal to : “Greater than or equal to” evaluates true if the value on the left of the operator is greater than the value on the right, or if it equal to the value on the right. It evaluate false if it is neither greater nor equal to, For example: 1>=1 will generate a true, 1.=5 will generate a false
Introduction to Data and control structures Over view of data <= Less than or equal to : “Less than or equal to” evaluates true if the value on the left of the operator is less than the value on the right, or if it is equal to the value on the right. It evaluates false if it is neither greater than nor equal to. For example: 2< =2 will generate true, 2 < = 1 will generate false. not equal to : “Not equal to” evaluates true if the value on the left of the operator and the value on the right of the operator are not the same, it evaluates false, if the are the same. For example: “a” “A” will generate a true, 10 10 will generate a false Logical operators : Logical operators, like rational operators, generate true and false (Boolean) values. While rational operators form simple relational expressions (the Boolean relationship between the values on the right of the relational operator and the value on the left of the relational operator), logical operators form compound relational expressions. For instance, the use of logical operators in conjunction with allows the programmer to generate true and false values from a combination of individual relational expressions. There are three logical operators. NOT : The logical NOT operator inverts the value that is generated by a rational expression. For example: NOT “dog” = “DOG” will generate true. NOT 10 = 10 will generate false. AND : A conditional expression using the logical “and” operator evaluates to true when the value true is generated for each and every individual relationship in the expression. For Example: 5>0 and 0<5 will generate true 5>10 and 1<=1 will generate false 5>0 and 0<5 and 1<=1 will generate true 5>0 and 0<5 and 1<1 will generate false
Introduction to Data and control structures Over view of data OR :A conditional expression using the logical “OR” operator evaluates to true when the value true is generated by the last one relationship in the expression. For example: 5<0 OR 0<5 will generate true 5>10 OR 1<1 will generate false 5>0 OR 0>5 OR 1<=1 will generate true 5 5 OR 1<1 will generate false
Introduction to Data and control structures Over view of control structure Understanding structure – Controlling logic flow Any computer program is a set of instructions that precisely describe what operations the processor should perform and when that operations should occur. During the program development cycle, the programmer’s responsibility is to identify exactly what these operations must be in order to produce the desired information. Simply identify the operations is the first step. However, the order of executions of the operation must also be considered. In other words, the programmer must determine the correct Logic flow for the execution of the instructions. Logic Flow : individual statements represent operations to be performed during program executions. These instructions must be written in the precise order necessary to produce the desired output. The programmer is responsible for determining this order using appropriate planning tools, such as hierarchy charts, flow charts and Pseudocode.
Introduction to Data and control structures Over view of control structure The three control structures. In order to control instruction executions, programmers employ three basic Control structures : Sequence, Selection, and Repetition. Sequence : The simplest flow control structure is the sequence structure. The sequence structure consist of one or more processing operations written in the order in which they should be performed. The sequence structure is not limited to the computer programming. There are many everyday examples of activities that must follow a specific order, such as following a recipe, planting a garden, changing a flat tire, etc. Sequence structure : Consider a program to calculate and display the average of two numbers: computing average, get first number, display average, get second number, complete sum. As written, all necessary processing operations are represented in the set of instructions. However, it is obvious, as written, these object will not produce meaningful results. There must be a correct logic order (sequence) of executions for these steps in order to produce the desired output. In other words, the programmer must control the flow of structure of the execution sequence. The sequence of steps in the following example will produce the desired output.
Introduction to Data and control structures Over view of control structure Selections structure : The selection (or decision) structure is necessary when analysis of the problem indicates that all sets of data will not be processed exactly the same. The selection structure provides a alternative mechanism for alternative processing operations. The programmer must identify the condition(s) that will control which alternative processing is performed for each data sets. Conditions : Consider a payroll program that computes the gross pay for each hourly employee, Those employees working 40 hours or less receive only regular pay, based upon their rate of pay. On the other hand, employees working more than 40 hours, and regular pay for the first 40 hours. What data value will determine how an individual employee’s gross pay is calculated? Answer, how many hours did they work? If hours worked <= 40 then gross pay = hours worked * pay rate, Else gross pay=(hours worked 40)*(pay rate * 1.5) + (40* pay rate) End if.
Introduction to Data and control structures Over view of control structure Repetition : The repetition or (looping) structure is necessary when analysis of the problem indicates that one or more that one time. The looping structure provides a mechanism to reduce duplication of processing statements. One set of the processing statements can be placed within the looping structure and repeated many times. Program size is reduced, and efficiency increases. The loop structure also uses conditional expressions to control the number of iterations and ultimately provide a means of ending the loop. Repeated : Consider a program to compute the average of two quiz grades for each student in a class of 5 students. Without a looping structure, the steps in the following example will produce the desired output, but would have to be written into the program code 5 separate times With a looping structure, the same code is written once inside the body of the structure. The loop Control the number of repetitions the code…. In this case 5 times. Set count = 1 Repeat while count < = 5 get first quiz grade get second quiz grade compute sum computer average display average add 1 to count End repeat
Introduction to Data and control structures Over view of control structure Quick Check during the input stage of the data processing cycle, data can be read from a filecan be generated within the program can be input from the keyboard all of the above
Introduction to Data and control structures Over view of control structure Quick Check Determine whether the input data for a program will come from is helpful in deciding what types of variable are needed for the program, and what they will be named. TRUE FALSE OR
Introduction to Data and control structures Over view of control structure Quick Check Counter variables add a certain value to the counter; accumulator variables add various values to the accumulator. TRUE FALSE OR
Introduction to Data and control structures Over view of control structure Quick Check Data hierarchy, from smallest unit to largest is: byte, bit, character, record, file field bit, byte, character, field, file, record bit, byte, character, record field, and file bit, byte, character, field, file, record
Introduction to Data and control structures Over view of control structure Quick Check The three control structures are: Sequence, iteration, repetition Sequence, selection, decision Sequence, selection, repetition Selection, repetition, iteration
Introduction to Data and control structures Over view of control structure Quick Check The decision structure allows one or more processing operations in a program to be executed more than one time. TRUE FALSE OR