Ross Sheppard High School
Computer Programming 20
Student Learning Guide
Controlling Program Flow with Decision Structures

The Decisions Program


  When you have finished this project, you will be able to:
create a series of decision structures
describe how each decision structure works
explain the logic of decision structures
  1. Create a new folder called Decisions on your personal drive.

  2. Start Visual Basic and create a new project. Save the form and Project as Decisions.

  3. Add your programmer information and Option Explicit information to the beginning of the program.

  4. Your first task to to create a simple If . . . Then statement. Such a statement checks to see if a certain condition is true, then acts upon that condition.

  5. Examine the code shown below:

  6. The statement means that if the user guessed the same number as the secret number, then display a caption in the label.

  7. Use the chart below to create the objects for this part of the program.

    Objects Table
    Object Property Setting
    Form Name frmDecisions
      Caption The Decisions Program
    TextBox Name txtGuess
      Text No text
    Label Name lblGuessMessage
      Caption Blank
    Label Name lblInstruction
      AutoSize True
      Caption Guess a number between 1 and 10
    Label Name lblMyGuess
      Caption My Guess
    Command Button Name btnCheckGuess
      Caption Check Guess
    Command Button Name btnExit
      Caption Exit


  8. Your form should be similar to the one illustrated below.



  9. Next, write the code for the Exit button.

  10. Now, write the code for the Check Guess button.



  11. Save your project and run it. You should be able to explain code such as this for a simple If . . . Then statement.

  12. For the next part of this project, you will use Boolean logic to add to an If . . . Then statement. Boolean logic is similar in concept to what you have learned about mathematical operators in math class. The chart below describes the Boolean expressions that you can use.

    Operator

    Meaning

    =

    equals

    <

    less than

    <=

    less than or equal to

    >

    greater than

    >=

    greater than or equal to

    <>

    not equal to



  13. Boolean operators can be used within If . . . Then statements to evaluate certain conditions.

  14. Use the objects table below to add to your Decisions program.

    Objects Table
    Object Property Setting
    TextBox Name txtGrade
      Text No text
    Label Name lblEnterGrade
      AutoSize True
      Caption Enter a mark
    Label Name lblResult
      AutoSize False
      Caption Blank
    CommandButton Name btnCheckGrade
      Caption Check Grade
  15. Your form should be similar to the one illustrated below.



  16. Next, write the code for the Check Grade button that makes use of a boolean operator. If the mark is greater than or equal to 70, then the label caption should read "Good job!". The code is



  17. Save and run your program. Try entering a mark less than 70, then one greater than 70. The appropriate caption should be displayed in the label.

  18. Next, change the code so that the caption will read "Study more!" if the mark is less than 70. This will make use of additional code to create an If . . . Then . . . Else statement. In other words, "if a certain condition is true, then do something, otherwise do something else."



  19. Save and run your program. The appropriate caption should be displayed in each case.

  20. Another type of decision structure that is useful is makes use of the ElseIf statement. ElseIf statements are used to solve a complex series of steps in a program - called an algorithm.

  21. Use the Object table below to add the objects listed to your form.

    Object Table
    Object Property Setting
    TextBox Name txtGuess2
      Text No text
    Label Name lblGuessMessage2
      Caption Blank
    Label Name lblInstruction2
      AutoSize True
      Caption Guess a number between 1 and 10
    Label Name lblMyGuess2
      Caption My Guess 2
    Command Button Name btnCheckGuess2
      Caption Check Guess 2


  22. Add the code to the Check Guess 2 button.



  23. Make sure you understand what the above code does - you will need to know that when you write the pseudo-code for the Program Documentation form.

  24. Save your project and run it. Try using various numbers to ensure all of the options work.

  25. Add a command button to your form. The Name of the button is Print. You can complete the remainder of the properties yourself.

  26. Add the following code to the Print button.



  27. Save you program and run it. Test the Print button.

  28. Next, remove the project by clicking on File in the Menu Bar, then on Remove Project. Start a new project and save it as Password in your Decisions folder.

  29. Add the objects listed on the Object Table below to your form.

    Object Table
    Object Property Setting
    Form Name frmPassword
      Caption The Password Program
    TextBox Name txtPassword
      Text No text
      PasswordChar *
    Label Name lblMessage
      Caption Type a password to log on to the computer
    Label Name lblPassword
      Caption Password:
    Command Button Name btnOK
      Caption OK
    Command Button Name btnCancel
      Caption Cancel
  30. Save the project. It should look similar to the illustration below.



  31. Write the code for the Cancel button - use the same code as you usually use for an Exit button. Save your project

  32. Shown below is the pseudo-code for the OK button.



  33. The actual code is shown below.



  34. It would be useful if the password form actually started the Decisions Program. Here is an overview of how to do this:
  35. Click here to see the marking guide for this project.

  36. When you have finished, send an email to your teacher asking that this project be marked.


© 1998-2000 N.F. Mathew, EdD
File name: Decisions.htm
Last updated on October 18, 2000