| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Constants and Variables

Page history last edited by PBworks 16 years, 4 months ago

How many coins have you got on you? At the moment I have 12 coins in my pocket. I could write:

number_of_coins_in_my_pocket = 12

 

If I buy lunch using five of these coins, there are only seven left. So I could write:

number_of_coins_in_my_pocket = 7

 

 

At any time the number of coins in my pocket varies, and the expression number_of_coins_in_my_pocket is an example of what is called a variable when used in computer programs. Although its value is fixed at any instant, it can be changed later in the program.

 

In contrast, there may be certain things that retain the samed, fixed value for the lifetime of a program. For example, if you were writing a program that was going to calculate how many times its wheels would have to turn round to travel a known distance, then you might define the radius of the wheel as a named constant that you could refer to throughout your program.

 

Which of the following are constants and which are variables?

number_of_coins_in_my_pocket

the_number_of_pennies_in_a_pound

the_diameter_of_the_wheels_on_Pedro

the_distance_Pedro_travels_in_a_second

 

 

My answer to the above is that the amount of money in my pocket varies all the time, so if this were used in a computer program it would be a variable. The number of pennies in a pound is always a hundred, so this is a constant. Pedro’s wheels are 50 mm in diameter, which is a constant. The distance Pedro travels in a second depends on how much power is supplied, so it is a variable.

 

Anything that will not change during the lifetime of the program should be defined as a constant, and anything that may change should be defined as a variable.

 

Variables are an essential ingredient of many computer programs. They allow the computer to store the value of things at a given instant of time.

 

Consider a program that will count. (countAloud.zip)

 

The program is as follows:

 

The second blockof this program declares a variable called "count". Like a constant, a variable can be given an initial value, in this case 0, which is set from the data block. Unlike a constant, the value of the variable can change anywhere in the program.

 

The conditional block that tests "If count == 3" means ‘look at the current value of count and if it is equal to 3, pass control to the Data "All DOne" block, ELSE go to the Calculate block. So, initially, N is 0, which is not equal to 3 so commands that follow on from the ELSE branch are executed.

 

The calculate block takes the value of count and calculates the value of count + 1 and then passes the result to variable block, where the count variable is set to the resulting value.

The variable is now passed back to the merge block, from where it is sent to both the conditional If block and the Calculate block that "calculates" the value of the message that is sent to the Text to Speech converter. The Text to Speech converter access a speech synthesizer thatcauses your PC to say speak aloud the sentence that is passed to it.

 

With the value of count now set to 1, the conditional if test fails again (1 is not equal to 3) and the else branch is followed once more. The message keeps passing round the bottom ELSE loop until the variable 'count' does indeed equal three, at which point the control thread passes to the Data block that creates the "All done" sentence that is passed to the TExt to Speech block.

 

You may notice the exclamation mark in the merge block. THis is a warning that your program complains a loop. IF you hover your mouse over the merge block, a tooltip will pop up that recommends you consider using recursion instead. Recurson is a very powerful concept in computing, and we shall see what it means later... 

 

Comments (0)

You don't have permission to comment on this page.