Lesson 15

Part III (Introduction to Programming, continued)

A very important part of writing programs is to be able to do something based on a particular value. For example, checking to make sure the user is inputting valid values. Would it be reasonable for a user to input a test score of -20 or 152? Well, 152 might be reasonable if the test was based on 200 points, but -20 is not reasonable!

1. The Ctrl Menu

2. Using an If Then Statement

We can use an If Then statement to check for valid input or perform different actions based on input. The “syntax” of an If Then statement is as follows:

3. Walking Through a Program

An excellent way to understand programming is to do what is called a code walk through. To “walk” through code, we make up values as needed and then walk the value through the code. For example, consider these programs:

(a) This is a walk through of program (a) shown below.
If we run this program, what happens?

If we run the program again, what happens?

(b) Using a pencil and paper, try walking through program (b) with n=1, n=15 and n=-3. Write down what you think the output will be for each. When finished, type in the program and then run it with n=1, n=15 and n=-3. How do these answers compare with your walk through results?

(c) Try walking through program (c) with n=1, n=15 and n=-3. This program is organized a little better. Remember, programs will do exactly what we instruct them to do.


Part III


Practice Exercises

  1. Begin a new program named Drive by clicking and inputting the program name.
  2. Write a program (using an If Then Else statement) that will do the following:
    • Ask the user what their name is.
    • Ask the user their age.
    • Display the user's name.
    • On the next output line:
      • If they are 15 years or older, display the message: You can drive the car.
      • If they are between 5 years and 14 years of age, display the message: You can ride the bicycle.
      • If they are under 5 years of age, display the message: Ask your mother or father for a ride.
      • If they are less than 0 years old, display the message: You are not born yet!
  3. Run your program and input an age of 15. With the output window showing, get a screen capture. Paste it into your Lesson15 document (under a title of PART III).
  4. Run your program and input an age of 8. With the output window showing, get a screen capture. Add two blank spaces following the first screen capture and then paste this one.
  5. Run your program and input an age of 3. With the output window showing, get a screen capture. Add two blank spaces following the last screen capture and then paste this one.
  6. Run your program and input an age of -43. With the output window showing, get a screen capture and then paste it on the next line.

Part IV

Reflection Exercises

You have just completed the fifteenth lesson in ClassPad 101. We will continue to write programs in Lesson 16; try to complete Lesson 16 right away. Please take a few moments to copy and paste the following three questions at the end of your Lesson15 document and answer them.

  1. Approximately how long did it take you to complete this lesson?
  2. Which activity did you find particularly useful?
  3. Did you find any part of this activity difficult to follow? If so, which part? Also, how did you overcome the difficulty?

Assessment 15: Introduction to Programming