Introduction to Visual BASIC (CS 143)

Decision Statements

What is a decision statement?

  • Something that represents a branching point in a solution
  • Outcomes are often dependent on initial conditions

Problem:

  • You’d like to go see a movie.
  • The movie costs $8.00, a soda costs $2.50 and a large popcorn costs $4.50.
  • Based on the amount of money in your pocket, determine whether you could...
    • (a) Just see the movie,
    • (b) See the movie and buy a soda, or
    • (c) See the movie, and buy soda and popcorn.

What do I know?

  • Movie costs $8.00
  • Soda costs $2.50
  • Popcorn costs $4.50
  • How much money I have in my pocket

What do I need?

  • Cost of movie and soda
  • Cost of movie, soda and popcorn
  • Way to select one of the three options (that is, make a decision!)

What do I do?

  • Option (a) costs $8.00
  • Option (b) costs $10.50
  • Option (c) costs $15.00
  • Is there a hidden option?
  • Yes! Stay home!

How about a Flowchart?

How about Pseudocode?

If (Money < $8) Then
    Stay home
    Else If (Money < $10.50) Then
        Movie
        Else If (Money < $15.00) Then 
                   Movie & soda
                   Else Movie, soda & popcorn

How about Visual BASIC?

Dim Money As Double
Dim Answer As String

' Get value of Money

If (Money < $8) Then
    Answer = "Stay home"
ElseIf (Money < $10.50) Then
    Answer = "Movie"
ElseIf (Money < $15.00) Then 
    Answer = "Movie & soda"
Else
    Answer = "Movie, soda & popcorn"
End If

' Display Answer