26
Chapter 1 A Guide to Working with Visual Logic 1 Input, Process, Output

VL Ch1 InputProcessOutput

Embed Size (px)

DESCRIPTION

text

Citation preview

Chapter 1

A Guide to Working with Visual Logic 1

Input, Process, Output

Introduction

A Guide to Working with Visual Logic 2

� A computer program is a solution to a problem

� Most useful computer programs do at least 3 things: � input data � process data � output resulting information

Input

Process

Output

Internet Shopping Cart System

A Guide to Working with Visual Logic 3

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.

INPUT PROCESS OUTPUT

Logic and Syntax

A Guide to Working with Visual Logic 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

A Guide to Working with Visual Logic 5

�  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

Simple Programming Formats (contd)

A Guide to Working with Visual Logic 6

�  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

Hello Name Program

A Guide to Working with Visual Logic 7

The input string value MUST be entered inside quotes (“ “)

But the output string is not displayed with quotes (“ “)

Input Statement Summary

A Guide to Working with Visual Logic 8

�  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.

Partial weekly paycheck solution

A Guide to Working with Visual Logic 9 What’s this?

Assignment Statement

A Guide to Working with Visual Logic 10

�  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

Weekly paycheck (partial solution)

A Guide to Working with Visual Logic 11

Pay will have the value: ?? 2030 Do you see the output?

Weekly paycheck (no formatting)

A Guide to Working with Visual Logic 12

Weekly Paycheck (complete solution)

A Guide to Working with Visual Logic 13

FormatCurrency is an intrinsic function that formats the variable within the parentheses with a $, a decimal point and 2 places after the decimal

Note: the shape of Output is different than Input in Visual Logic

Weekly paycheck (no formatting)

A Guide to Working with Visual Logic 14

What would you see if you used FormatCurrency?

Weekly paycheck (formatting)

A Guide to Working with Visual Logic 15

FormatCurrency – rounds off to 2 decimal places

Arithmetic Expressions

A Guide to Working with Visual Logic 16

�  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)

Operators in Visual Logic

A Guide to Working with Visual Logic 17

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

Quick Check 1-1, 1-B Pg 12

A Guide to Working with Visual Logic 18

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.)

Example: Average

A Guide to Working with Visual Logic 19

�  Demo in Visual Logic �  Step Into

Examples – Odd or Even?

A Guide to Working with Visual Logic 20

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

Examples – Coins Change

A Guide to Working with Visual Logic 21

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 …

A Guide to Working with Visual Logic 22

A Guide to Working with Visual Logic 23

Intrinsic Functions in VL

A Guide to Working with Visual Logic 24

Example Result 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

Table 1-1: Correct Programming Formats

A Guide to Working with Visual Logic 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

Output Statement Summary

A Guide to Working with Visual Logic 26

�  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