Visual Logic Instructions and Assignments: Hover Your Mouse Over The Red Arrow Between The Begin and
Visual Logic Instructions and Assignments: Hover Your Mouse Over The Red Arrow Between The Begin and
Visual Logic can be installed from the CD that accompanies our textbook. It is a nifty tool
for creating program flowcharts, but that is only half of the story. Until this software
came along, flowcharts were static diagrams that outlined the flow of execution of a
computer program. When a Visual Logic flowchart is done, you can run the program
behind the flowchart. This is a great learning resource for budding programmers.
You can even create interactive programs that allow the user to input values for variables
at runtime. Visual Logic is a terrific tool for grasping basic programming concepts such as
variables, operators, expressions, branching, and looping. It is excellent preparation for
more sophisticated programming with Visual Basic.NET. We hope you like it!
Install Visual Logic on your home PC by accepting all defaults in the installation wizard.
Then, put the CD away in a safe place.
symbols.
Hover your mouse over the red arrow between the Begin and
End symbols. It will turn green, indicating that you can click on it
to get a popup window. This popup window has the other symbols
we will need to create programs.
Enter your own name and be sure to enclose it within quotes. Later, we will use
variables that dont use quotes. Click on the OK button when done, and the Output symbol
will change to show your intended output.
Run the program by clicking on the button on the toolbar, or by pressing the F5
function key.
Click on the green arrow below the first Output symbol to add
another Output symbol. Make this second Output generate the
output Visual Logic is fun.
Run the program again. Did you remember the quotes? You should
get two popup dialogs, one after the other.
Edit the first Output by clicking the More >> button. Select console. An odd looking
line feed symbol, , will be appended to your name. This is normal for console output.
Click on OK.
Edit the second Output box in
the same manner to select
console output.
It may seem that nothing happened, but if you look on the taskbar at the bottom of your
screen, you will see that a new window has been created.
Summary
Text output must be enclosed within quotes
The More>> and <<Less toggle buttons will reveal or hide symbol options
Console output will be to one window and is indicated by a small rectangle
The symbol causes a line feed in a console window
Visual Logic Assignment One
Create a Visual Logic program named VLogic1.vls that uses four Outputs. The first three
Outputs should create a console window similar to the one shown here. Specify your name,
place of birth, and hobby or pastime. The fourth Output should create the popup dialog
box exactly as shown.
Start a new Visual Logic program with File | New on the menu.
Insert an Input symbol. The Input symbol is a parallelogram like the Output
symbol, but unlike Output, it leans to the right.
Double click on the Input symbol and click More>> to expand the window.
You can start receiving Social Security payments at age 62. The Assignment shown above
computes the number of years before a user could start receiving payments and stores it
in a variable named SocialStart.
Insert another Output above the End symbol. Configure it as shown below.
This Output concatenates the SocialStart variable between the two strings. Again, note
the spaces after in and before years are essential for good quality output.
Run the program by clicking on the button on the toolbar, or by pressing the F5
function key.
NOTE: When prompted to input your name, enclose it in quotes. Always use quotes around
textisInputs.
Here Dorun
a sample notof
enclose the number entered for your age. Never use quotes with
the program:
numeric input.
If you got a red error screen, check your work for quotes. Its easy to forget one.
Edit all Inputs and Outputs by clicking on More >> and selecting the console option.
The small white rectangle will indicate the console. In the first Output, press Enter with
the cursor in front of the line feed symbol . The program should appear as below on the
left.
Add two more Input symbols, one for the unit price of a
merchandise item, and the other for the quantity
purchased. Use console, appropriate variable names, and
suitable prompts for both Inputs.
Arithmetic Operators
The following table summarizes the Visual Logic arithmetic operators, and their usage in
arithmetic expressions. You will use most of these in future assignments.
NOTE: modulus is another term for the remainder after division. Some more examples:
22 Mod 5 evaluates to 2 ( 5 divides into 22 four times with 2 remainder )
36 Mod 7 evaluates to 1 ( 7 divides into 36 five times with 1 remainder )
Summary
Always enclose non-numeric input within quotes
Press enter in an Output symbol to get a line feed (for a new line)
Visual Logic has seven arithmetic operators
The FormatCurrency function can be used to display dollars and cents
Insert an IF condition symbol immediately after the Input. Note that the IF symbol is
a diamond. Note too that the IF creates two branches, True and False. The False branch is
also known as the ELSE branch.
Double click on the IF diamond to get the dialog box. This allows us to enter the
condition for the IF symbol. In our case, the condition must test the users age.
Enter Age >= 18 as the condition. Then click OK. The symbol
>= means greater than or equal to, and as you know the voting
age is 18 in the USA.
Insert an Output in the True branch. Configure it to output You are of voting age.
Insert an Output in the False branch that will output You are too young to vote.
Run the program a few times. Try ages older and younger than 18. Try 18, too.
Save the program as agevote.vls.
The program should respond properly with respect to the Age variable. This simple
program used only one symbol in both branches of the IF, but multiple operations in each
branch are common.
Operators Used for IF conditions
Conditional Operator Explanation
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
= Equal to
<> Not equal to
Nested IFs
The branches of an IF diamond can themselves contain
IFs that have their own branches. This is called nesting.
For example, a program that qualifies a voter in the
USA should ensure that the user is a US citizen, in
addition to the age test. Lets try this.
Save it again. Run the program with different ages and try both possible responses to
the Citizen Input. Make sure that you use quotes around your response to the Citizen
Input. The output should be what would be expected. If not, contact
your instructor. The entire program should now appear as shown next.
If your program gets too expansive for the display, reduce the % on the toolbar.
Compound Conditions
By using logical operators, programmers can create compound conditions for IF symbols.
Logical Operators
Operator Explanation
AND Less than
OR Greater than
NOT Less than or equal to
As an example of a compound condition, we will modify the previous program to make it
detect a teenager.
Load the voteage.vls program if necessary. Save it as teenager.vls (to make a copy).
Delete everything under the Age Input.
Add an IF diamond and edit the condition as
shown here. Click OK.
Run the program at least three times. Try ages for teenagers and ages that are too
young or too old for a teenager.
Note that the False branch executes for input that is too young or too old.
Save the program again as teenager.vls.
Summary
IF symbols can execute operations when the specified condition evaluates as True
Operations can be executed when specified condition evaluates as False
Conditional operators are used to create the expressions for IF conditions
Logical operators can be used to write expressions for compound conditions
IF symbols may be nested inside other IF symbols. This is termed nesting.
This program enhances payroll.vls from Assignment Three. You can save effort by loading
it and saving another console version as overtime.vls. In the new version:
Delete the Input for the users name ( to simplify things )
Add Input that asks if the employer pays overtime at time and a half for hours
over 40 in one week ( 42 hours = 2 hours of OT, which earns 3 hours of pay )
Add an IF branch to determine if OT pay applies based on this Input
Add an IF branch to determine the OT pay (if any is deserved )
Add Output(s) showing the total pay ( regular pay and OT, if any and applicable )
In the above run, note that OT is not an issue since the weekly hours are not over 40.
In the above run, since hours exceed 40, the prompt about the employers OT policy
appears but the employer does not pay Time and a half, so no OT is paid
In this final run, hours exceed 40 and the employer pays time and a half. The 4 hours of
OT earn 4 x 1.5 x 10 = $60.00 of OT pay.
NOTE: Your program should run in a like manner for these inputs.
Save as overtime.vls.
1. an Initial Value
this is the starting value for the Count
variable
2. a Final Value
this is the stop value for the Count variable
3. a Step
this is the amount by which the Count
variable will change in each loop cycle NOTE: Any / all of the three settings
could be variables that hold numbers.
Insert a For Loop symbol and configure it as shown here. Click OK when done.
The dialog box shown here is setting up a loop that will cycle 10 times. Count will start at 1
and increase by 1 in each loop cycle, stopping when Count reaches a value of 10. This means
that Count will have the values 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10. We can prove this.
Inside the loop, insert an Output symbol and configure it as shown. Run the program.
NOTE: The line feed symbol, , causes the Count values to be displayed one per line
Heres another example that outputs a times table like the ones that can be found on the
back cover of a grade school notebook.
Start a new Visual Logic program. Insert an Input and configure it as shown.
Insert a For Loop symbol and configure it to cycle from 1 to 12 as shown above.
Inside the loop, insert an Output using concatenation to display the times table.
For Loops are best suited for programs in which the number of loop
cycles, or iterations, are predictable or known at the outset.
Summary
Loops use a loop control variable to run one or more enclosed instructions repeatedly.
Each run through the loop is called a cycle or iteration.
The number of cycles depends on the Initial Value, Step, and Final Value of the loop
control variable
Any or all of the three For Loop settings for the loop control variable can be numeric
variables
Exit Loop is also useful inside intentional endless loops. An endless loop isnt always a
programming error. A Visual Logic program might use an intentional endless loop to
repeatedly run one or more instructions until a certain IF condition is met. When the IF
condition is True, Exit Loop is used to end the endless loop. This is especially useful for
validating user input to a program. An endless loop can run over and over again until the
user enters the input as desired. While Loops can be made intentionally endless by
entering True as the while condition. Here is an intentional endless While Loop that
validates the entry of a test score. The loop wont allow a TestScore higher than 100.
Summary
While Loops will continue to execute when the condition evaluates as True.
Without an Assignment that modifies the variable in the condition, an endless loop results.
A Pre-Test While Loop might never run at all (if the condition is False on starting).
A Post-Test While Loop runs at least once since the condition is at the end of the loop.
An endless While Loop using the Exit Loop symbol can be a useful programming technique.
After the loop (just above End), insert an Assignment to calculate the Average of the
numbers that were entered.
Summary
An accumulator is like a bucket that increases by the value of another variable
Accumulators should be initialized to a starting value of 0
The syntax for adding on to an accumulator has this form:
Accumulator = Accumulator + SomeVariable
A sentinel watches out for a particular value in a variable. Typically, a sentinel is used in
an IF symbol to detect a specific value input by the user. When the special sentinel value
is detected, the program reacts in some desired way.
In the following example, a counter and a sentinel are used to write a different version of
the averaging program outlined in Lesson 7. If you have trouble grasping the following
step-by-step instructions, the entire program and two sample runs can be seen on the next
page.
Start a new Visual Logic program with an Assignment symbol for an accumulator
named Total. Initialize Total to zero (the same as Lesson 7).
Insert a While Loop and make it endless by setting the while condition as True.
Inside the loop, insert an Input symbol for a variable named Number. In the prompt,
ask the user to enter a number, but this time, notify the user that entering a value
of 0 will end the process. This program will use a sentinel value of 0 for the Number
variable to determine that the user has no more numbers to average.
Directly below the Input for Number, insert an IF symbol. Set the IF condition to be
Number = 0 (the sentinel value). Insert an Exit Loop symbol in the True branch of
the IF.
Insert two Assignment symbols beneath the IF. One should accumulate the Total
variable by the value of Number, and the other should increment the Count variable
by 1.
Just above the End symbol (in the False branch of the While Loop), insert an
Assignment that determines the Average using the Total variable and the Count
variable.
Finally, insert an Output symbol to display the average. Run the program a few times.
The entire program, and two sample runs, can be viewed on the following page.
This program is impressive since the user does not have to indicate in advance how many
items are being purchased.
All DONE!
B. Yourth
St. Petersburg College, FL