Input, Process, Output Chapter 1 1 A Guide to Working with Visual Logic Introduction A computer program is a solution to a problem Most useful computer programs do at least 3 things: Input input data process data Process output resulting information Output 2 A Guide to Working with Visual Logic Internet Shopping Cart System INPUT Customer ID Mailing address Credit card number Product I 3 A Guide to Working with Visual Logic PROCESS Connect to Database Determine product cost Calculate sales tax Bill customer OUTPUT Print customer receipt Generate shipping label Generate report for sales dept. Logic and Syntax 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 4 A Guide to Working with Visual Logic 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 A Guide to Working with Visual Logic 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 A Guide to Working with Visual Logic Hello Name Program The input string value MUST be entered inside quotes (“ “) But the output string is not displayed with quotes (“ “) 7 A Guide to Working with Visual Logic 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 A Guide to Working with Visual Logic Partial weekly paycheck solution 9 A Guide to Working with Visual Logic 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 A Guide to Working with Visual Logic Weekly paycheck (partial solution) Pay will have the value: ?? 11 A Guide to Working with Visual Logic 2030 Do you see the output? Weekly paycheck (no formatting) 12 A Guide to Working with Visual Logic 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 A Guide to Working with Visual Logic 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 A Guide to Working with Visual Logic Weekly paycheck (formatting) FormatCurrency – rounds off to 2 decimal places 15 A Guide to Working with Visual Logic Arithmetic Expressions 4–5+2 1 + 3*7 52+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 A Guide to Working with Visual Logic Operators in Visual Logic OPERATOR 1. Exponentiation 2. Multiplication and Division 3. Integer Division 4. Integer Remainder 5. Addition and Subtraction SYMBOL EXAMPLE ^ 2^3 = 8 * , / 2*3=6, 5/2=2.5 \ 5\2 = 2, 38\10= 3 Mod 5 Mod 2 = 1 + , 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 A Guide to Working with Visual Logic 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 = 18 A Guide to Working with Visual Logic 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.) Example: Average Demo in Visual Logic Step Into 19 A Guide to Working with Visual Logic 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 A Guide to Working with Visual Logic 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 21 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 … A Guide to Working with Visual Logic 22 A Guide to Working with Visual Logic 23 A Guide to Working with Visual Logic Example Intrinsic 24 Functions inResult VL FormatCurrency(12345) FormatCurrency(.02) FormatPercent(0.0625) FormatPercent(0.75) Abs(-3.3) Abs(5.67) Int(3.8) Int(7.1) Round(3.8) Round(7.1) Random(5) $12, 345.00 $0.02 6.25% 75.00% 3.3 5.67 3 7 4 7 A random integer between 0 and 4 Random (6) + 1 A random integer between 1 and 6 A Guide to Working with Visual Logic Table 1-1: Correct Programming Formats 25 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 A Guide to Working with Visual Logic 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 A Guide to Working with Visual Logic