Input, Process, Output Chapter 1 1
Introduction A computer program is a solution to a problem Most useful computer programs do at least 3 things: Input input data process data output resulting information Process 2 Output
Internet Shopping Cart System INPUT PROCESS OUTPUT Customer ID Mailing address Credit card number Product I Connect to Database Determine product cost Calculate sales tax Bill customer Print customer receipt Generate shipping label Generate report for sales dept. 3
Logic and Syntax 4 An algorithm is the blueprint for a computer program/ software Tools for representing algorithms Flowcharts Pseudocode Visual Logic is a visual tool combining graphics of flowcharts and pseudocode-like syntax Visual Logic animates or executes the algorithm Visual Logic IS NOT a computer programming language Once an algorithm is developed it must be coded in a programming language to be used as a computer program
Simple Programming Formats An output statement displays information (in a window/ console/file) An input statement accepts data from the user and stores that data into a variable A variable is a storage location in computer memory that can be accessed and changed in the program 5
Simple Programming Formats (contd) A variable has a name (which does not change) and an associate data value (which may change during the execution) e.g. NAME is the name of a variable and its data value is the string Dave The name of a variable is also called an identifier. An identifier cannot have any spaces in it, only letters, numbers and underscore (_) Legal identifiers: NAME, num1, interestrate, Num_1, Num_2 Visual Logic is case insensitive 6
Hello Name Program The input string value MUST be entered inside quotes ( ) 7 But the output string is not displayed with quotes ( )
Input Statement Summary Input statements are used to get data into variables In Visual Logic, the input flowchart element is a parallelogram with the keyword Input following by the variable name (e.g. Input: NAME) When the input statement is executed the user is prompted to enter a value using the keyboard. The value typed is then stored in the variable for later use. String input must be placed inside quotes Numeric input must contain only digits and possibly one decimal point. Percent symbols (%), dollar signs ($) and commas (,) or any other symbols are not allowed. 8
Partial weekly paycheck solution 9 What s this?
Assignment Statement Assignment statements are used to perform calculations and store the result An assignment statement is a processing step, so its flowchart shape is a rectangle An assignment statement contains a variable on the left-hand side (LHS), an expression on the right-hand side (RHS) and an equal sign (=) in the middle When an assignment statement is executed, the RHS expression is evaluated first. This value is stored in the variable (e.g. The result of the RHS becomes the data value of the variable on the LHS) Variables hold values and have a name variables are named memory locations 10
Weekly paycheck (partial solution) 11 Pay will have the value:?? 2030 Do you see the output?
Weekly paycheck (no formatting) 12
Weekly Paycheck (complete solution) FormatCurrency is an intrinsic function that formats the variable within the parentheses with a $, a decimal point and 2 places after the decimal 13 Note: the shape of Output is different than Input in Visual Logic
Weekly paycheck (no formatting) What would you see if you used FormatCurrency? 14
Weekly paycheck (formatting) FormatCurrency rounds off to 2 decimal places 15
Arithmetic Expressions 4 5 + 2 1 + 3*7 5 2 +2*8 12/4*2 12/5+3 Order of Operations: PEMDAS Parentheses Exponents Multiplication (*) &Division (/) (from left to right) Addition (+) & Subtraction (-) (from left to right) 16
Operators in Visual Logic OPERATOR SYMBOL EXAMPLE 1. Exponentiation ^ 2^3 = 8 2. Multiplication and Division *, / 2*3=6, 5/2=2.5 3. Integer Division \ 5\2 = 2, 38\10= 3 4. Integer Remainder Mod 5 Mod 2 = 1 5. Addition and Subtraction +, - 2+15=17 Examples: New Operators only with Integers (whole numbers) Integer Division: 12\4 à 3, 17 \3 à 5 Integer Remainder: 12 Mod 4 à 0, 17 Mod 3 à 2 17
Quick Check 1-1, 1-B Pg 12 1-A: A=3, B=5 1. A+B*5 = 40 or 28? 2. (2*3)^2 = 3. 11\ A = 4. 2*3^2 = 5. 11 / A = 6. 11 Mod A = 1-B: 1. (Exam 1 + Exam 2 + Exam 3)/3 2. 1 + 1 /2 + 1/4 3. (4*A^2*B)/C (do you need the parentheses in 3.) 18
Example: Average Demo in Visual Logic Step Into 19
Examples Odd or Even? Problem: Given a whole number, N, determine if it is ODD or EVEN? Algorithm: If N Mod 2 equals 0 è N is even Otherwise è N is odd 20
Examples Coins Change Problem: Given a whole number, N < 100, determine how many quarters, dimes, nickels, pennies make up N cents with the least # of coins. Example: N = 92 # of Quarters = 3 # of Dimes = 1 # of Nickels = 1 # of Pennies = 2 92 / 25 = 3.68 92\25 = 3 ç # of Q 92 Mod 25 = 17 ç remainder or left over (92 3*25 = 92 75 = 17 == 92 Mod 25 17 \10 = 1 ç # of D 17 Mod 10 = 7 ç left over 21
22
23
Example Result Intrinsic Functions in VL FormatCurrency(12345) $12, 345.00 FormatCurrency(.02) $0.02 FormatPercent(0.0625) 6.25% FormatPercent(0.75) 75.00% Abs(-3.3) 3.3 Abs(5.67) 5.67 Int(3.8) 3 Int(7.1) 7 Round(3.8) 4 Round(7.1) 7 Random(5) A random integer between 0 and 4 Random (6) + 1 A random integer between 1 and 6 24
Table 1-1: Correct Programming Formats Value Written Format Programming Format Comment String Hello World Hello World Use quotes to delimit strings Percent 15% 0.15 Use decimal format Dollars $300 300 Dollar signs not allowed Large Numbers 12,345,678 12345678 Commas not allowed 25
Output Statement Summary Output statements are used to display information In Visual Logic, the output flowchart element is a parallelogram with the keyword Output followed by an output expression When executed, string literals (e.g. Dave or Quarters ) are displayed exactly as typed inside the containing quotes When executed, expressions are evaluated and the result is displayed The ampersand (&) operator maybe used to concatenate a series of string literals, variables and expressions into one large expression Carriage returns in the output expression appear as carriage returns in the displayed output 26