Programming Course for Beginners - ZedLX

The Easiest Online Computer Programming Course, for Free

Variables [10/14]

This program draws many small discs of small radius. The radius is a random value in the range from 2 to 7 .
[Run the program]

All variables introduced in a program have a certain scope. The scope of a variable defines parts of the program where the variable can be used.

For example, the scope of variable i in this program is restricted to the for loop. Variable i is not available outside the for loop.

The variable radius is available everywhere below the place where it was introduced.

The variable x is available only in its block, not outside it. More generaly, the scope of any variable is restricted to the block in which the variable is introduced.

Exercises:

1. Make the program print out the value of variable radius by adding a println statement at the end of the source code. Also, print a string
"The radius is:"
before the value of variable radius. Run and restart the program several times.

2. Un-comment the println statement below the comment "Error 1", to see the error message.

Put the line in the comment again (comment it out).

3. Un-comment the println statement below the comment "Error 2", to see the error message.

Put the line in the comment again (comment it out).

Loading...