Visual Logic - Making Decisions (Chapter 2)

Making Decisions

  • Visual Logic uses conditional statements to control program flow.
  • Pseudocode expresses decisions with If-Then (and ElseIf/EndIf) structures.

Example 2 — Spending allowance

  • Problem: If you buy a shirt you won’t have enough left for a CD.
  • Pseudocode: If you buy a shirt Then you cannot buy a CD

Example 3 — Which show to go to

  • Problem: Decide which movie show to attend based on timing; early show lets you watch TV, later show can be recorded.
  • Pseudocode:
    • If I go to the early show Then I can reach home and watch my favorite program
    • Else If I go to the later show Then I should set my TiVo to record my favorite program
    • EndIf
  • Note: there is no final Else for this If; EndIf pairs close the blocks.

Boolean - Data Type

  • A boolean variable has two possible values: true and false.
  • Example: x = (2 > 3) results in x being FALSE

Conditions – Boolean Expression

  • A boolean expression yields true or false.
  • Examples: A < B? and num1 = 0?
  • Assignment vs equality in Visual Logic:
    • In Visual Logic, A = 3 as an assignment reads as "A gets 3".
    • In a condition, A = 3 can be read as "Is A equal to 3?" (boolean test).
  • A condition is a boolean expression; involves relational operators.
  • Common operators:
  • Note: Visual Logic uses = for assignment; equality tests in conditions are expressed with the same symbol in this context.

Relational Operators

  • Operator and meaning (assume X = 2, Y = 3):
    • = Equals; X = 2 is TRUE; X = Y is FALSE
    • <> Not Equal; Y <> 5 is TRUE; Y <> 3 is FALSE
    • > Greater Than; X > 1 is TRUE; X > Y is FALSE
    • < Less Than; X < Y is TRUE; X < 2 is FALSE
    • >= Greater Than Or Equal; X >= 2 is TRUE; X >= Y is FALSE
    • <= Less Than Or Equal; X <= 2 is TRUE; X <= 1 is FALSE

Demo: Odd or Even?

  • Problem: Display whether an input is odd or even.
  • Common approach: if (n mod 2 == 0) then even, else odd.

Demo: Weekly Paycheck

  • Overtime calculation example:
    • Pay = 40 × Rate + (Hours − 40) × Rate × 1.5 if Hours > 40
    • Otherwise Pay = Hours × Rate
  • LaTeX representation:

    • \text{Pay} = \begin{cases}
      40 \cdot \text{Rate} + (\text{Hours}-40) \cdot \text{Rate} \cdot 1.5, & \text{Hours} > 40 \
      \text{Hours} \cdot \text{Rate}, & \text{otherwise}
      \end{cases}
  • Output example: "Pay due is " & Format Currency(Pay)