Week 02 Topics 2 1 Designing a Program



























- Slides: 27

Week 02 Topics 2. 1 Designing a Program 2. 2 Output, Input, and Variables 2. 3 Variable Assignment and Calculations Precedence Rules Lab Exercises

2. 1 Designing a Program Designing a program involves 1. Understanding the tasks that the program is to perform • • Learning what the customer wants Requirement gathering is the often the most difficult and time-consuming design activity 2. Determining the steps that must be taken to perform the task • • Create an algorithm, or step-by-step directions to solve the problem Use flowcharts and/or pseudocode in this process Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2

2. 1 Designing a Program 1. The first step in programming is design. • Create flowcharts and/or pseudocode in this task. 2. Next, the code is written. 3. The code must be cleared of all syntax errors. 4. After the executable is created, the program is run and checked for logic errors. 5. If logic errors exist, the program must be debugged. Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3

2. 1 Designing a Program Types of Program Errors • Syntax – – – • An error in the use of the language key words, operators, punctuation or other rules for writing program statements Identified by compiling the program Prevent executable code from being created Logic – – – An error or omission in the algorithm for a program Identified when running the program and seeing incorrect or unexpected results Often difficult to isolate and fix Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4

2. 1 Designing a Program Pseudocode • Used as a model for programs • No syntax rules • Well written pseudocode can be easily translated to actual code Display “Enter the number of hours” Input hours Display “Enter the hourly pay rate” Input pay. Rate Set gross. Pay = hours * pay. Rate Display “The gross pay is $”, gross. Pay Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5

2. 1 Designing a Program Flowchart • A diagram that graphically depicts the steps that take place in a program with – – • • Geometric shapes for the various program activities Directed arrows connecting the shapes to represent the flow of the program An alternative to pseudocode See Appendix B for the symbols used in the textbook Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6

2. 1 Designing a Program Most-Used Flowchart Symbols Figure 2. 2 Flowchart for the pay calculating program Terminator used for start and end Parallelogram used for input and output Rectangle used for processing Diamond used for decisions Circle used as an onpage connector Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7

2. 2 Output, Input, and Variables • Program execution typically follows three steps: 1. Input may be received • Input is data that a program gets to work with. 2. Some Process is performed using any input • Variables are named storage locations in memory for data the program uses. 3. Output may be produced • Output is data that is generated and displayed. • See the pay calculating program in Figure 2 -3 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8

2. 2 Output, Input, and Variables • A Display statement shows output to the screen • Lines execute in the order they appear (known as a sequence structure) • A string literal is a sequence of characters enclosed in double quotes Figure 2 -4 The statements execute in order Figure 2 -5 Output of Program 2 -1 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 9

2. 2 Output, Input, and Variables • A Display statement often includes multiple items in the output line. – The textbook pseudocode separates the items using a comma. • Display "You are ", age, " years old. " – Raptor requires separating the items with a plus sign. • Display "You are “ + age + " years old. " – Visual Basic requires separating the items with an ampersand. • Display "You are " & age & " years old. " Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10

2. 2 Output, Input, and Variables • A Display statement that asks for the user to enter a value is called a prompt. • An Input statement is used to get values from the user of the program. • A variable is a program name for a storage location in memory. Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11

2. 2 Output, Input, and Variables • Programmers can define variable names following certain rules • • Must be one word, no spaces Punctuation characters are generally avoided The first character generally cannot be a number Name a variable something that indicates what may be stored in it • A popular naming convention is camel. Case • Examples: rate. Of. Pay, week. Days • Use of underscore characters in names is also common • Examples: rate_of_pay, week_days Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12

2. 3 Variable Assignment & Calculations • Assignment of a value to a variable does not always have to come from user input, it can also be set through an assignment statement • Example: Set price = 20 or price = 20 • but not 20 = price • Note that = means is assigned, not is equal to Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 13

2. 3 Variable Assignment & Calculations A program variable • Holds only one value at a time • May hold different values while a program is running • Must appear on the left side of the assignment statement that is storing its initial value or replacing its value with something else Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 14

2. 3 Variable Assignment & Calculations • Numerical calculations are performed using math operators. • The values operated on in a math expression are called operands. Table 2 -1 Common math operators Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15

2. 3 Variable Assignment & Calculations • The modulus (remainder) operator requires integer operands and is useful to: • Detect if an integer is odd or even • Determine the day of the week • Modulus operator examples: • 3 MOD 7 is ? 3 • 15 MOD 2 is ? 1 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 16

2. 3 Variable Assignment & Calculations • The symbol % is used instead of MOD in some languages. • To use a percentage, like 6%, in a program, you must convert it to a decimal number, like. 06 for 6% • Not all languages include an exponent operator for the product of a value by itself multiple times. • Exponent operator example: • 2 ^ 5 is ? 32 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 17

Checkpoint - Designing a Program You asked to design a stock transaction program. • What questions would you ask to gather the requirements? • What data requires creating variables? • What algorithm or sequence of steps is needed to meet the requirements? Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18

Expressions With Multiple Operations ▪ Which operation is done first? (3 + length) * width / (height * 1. 1 - 17) ▪ To specify the order of operations in complex expressions, computers use: ▪ Parentheses Precedence Rules ▪ Operator Precedence Rules ▪ Associativity Rules Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Parentheses Precedence Rules ▪ What is inside parentheses is evaluated first ▪ Example: (2 + 10) * 3 Result is 36 ▪ Nested parentheses - innermost set is evaluated first ▪ Example: (2 + (10 - 1)) * 3 Result is 33 ▪ Use parentheses to overrule or make obvious the natural operator precedence order Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Operator Precedence Rules VB operator order of evaluation: • • Exponentiation Multiplication and Division Remainder Addition and Subtraction Example: -3 + 7 * 2 Result is 11, not 8 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Associativity Rules If in the same sub-expression and at the same precedence level: • Right associativity evaluates from right-to-left. • Left associativity evaluates from left-to-right. • VB uses left associativity: 8 / 2 * 4 Result is 16, not 1 8 - 4 + 1 Result is 5, not 3 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Applying Precedence Examples These expressions evaluate to 2: 8 * (7 - 6 + 5) MOD (3 + 4 / 2) - 1 2 + 2 * (2 * 2 - 2) MOD 2 / 2 Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

2. 3 Variable Assignment & Calculations • Precedence rules define the order of operations performed in mathematical expressions with multiple operators by a computer. • For Visual Basic: 1. Perform any operations enclosed in parentheses 2. Perform any operations using the exponent operator 3. Perform any multiplications or divisions 4. Perform any modulus (remainder) operations 5. Perform any additions or subtractions ➢Operators with equal precedence are evaluated left to right in the order in which they appear in the expression. Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 24

2. 3 Variable Assignment & Calculations • Converting mathematical expressions for use in programming statements is often required. • Keep in mind the precedence rules, and use parentheses to override the rules or for readability. • Mathematical expressions involving multiplication must be converted to use the * operator: • 25 ab becomes 25 * a * b • (-128)(y) becomes -128 * y • 60 x hours becomes 60 * hours Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 25

2. 3 Variable Assignment & Calculations • Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 26

Checkpoint • Chapter 2 Review Questions, True or False • Week 02 Assignments • Week 02 Lab Videos