Working with Variables

1. To be perfectly clear...

Study the following program. Predict its output, then run it and compare the result with your prediction.

2. #commentlikeandsubscribe

In the program above, what do you think the purpose of the lines beginning with # is? Are these lines necessary to run the program? Does removing them affect the program’s output? Edit the code to find out!

3. Do we need some space?

Modify and run the program above to help answer the following questions:

  • Is it necessary to leave a space before and after the + and – operators? If not, why might we do it anyway?
  • Could we simply use 3x instead of 3*x?

4. The cart before the horse.

The following program is supposed to calculate and display the value of y based on the given value of x. Why doesn’t it work? Edit the code to fix the error.

5. Dan's difficulty.

Dan created the following program to calculate the total cost of an item after a 13% tax is applied to its price. Unfortunately, his program contains three errors. Edit the code to correct the errors and get the program operating as intended. Be sure to test your work by running the program with several different starting prices!

6. Your turn.

The height of a ball is described by the equation h = -4.9t² + 20t + 2, where h is the height of the ball above the ground (in metres) and t is the number of seconds after it is thrown. Write a program that finds the height of the ball after 2 seconds. Use variables for time (t) and height (h) in your program.

7. How high?

Change the time value in your program above to determine the height of the ball above the ground 3.5 seconds after it is thrown. Can you determine ball’s maximum height above the ground? 

Previous

What is a Variable?

Next

User Input