70 likes | 88 Views
Learn how to work with variables in algorithms, assigning and changing values. Understand the concept of initialization and practice writing your own algorithms.
E N D
Algorithms with variables that are assigned a number By Phil Bagge HIAS Computing Inspector/Advisor CAS Master Teacher
Variable are like whiteboards name my_num 4 value
Assigning a value name my_num When we write a value onto a variable we call it assigning a value 4 value
Read the name get the value name my_num I am my_num years old. Becomes I am 4 years old I have my_num friends. becomes 4 value I have 4 friends.
More read the name get the value Assign 3 to variable called my_num Stand up Bow my_num times Sit down Wave my_num times Say my_num my_num Now write your own algorithm Don’t assign too large a number Assign 4 to fav_num Say Hi fav_num times Hum for fav_num seconds Do fav_num times stand sit stand Jump fav_num times
Now write your own algorithm with a changed value Don’t assign too large a number We can change the value assigned to a variable Assign 5 to variable called my_num Stand up Bow my_num times Subtract 2 from my_num Say my num Sit down Wave my_num times Add 1 to my_num Say my_num Assign 2 to fav_num Say Hi fav_num times Add 3 to fav_num Hum for fav_num seconds Subtract 2 from fav_num Do fav_num times stand sit Say fav_num You can use a whiteboard to keep track of the value
Assigning a value to all variables at the beginning is called initialising the variables Initialised variable Assign 5 to variable called my_num Stand up Bow my_num times Subtract 2 from my_num Say my num Sit down Wave my_num times Add 1 to my_num Say my_num On which line has the variable been initialised?