These last two weeks have just been programming or analyzing scratch
One of the things we have been doing is to discover where a number is after a program. Example follows:
When space key pressed
Set X to 4
Set Y to 7
Repeat 4
If X>Y
Set Y to Y + X
Set X to X - 1
Else
If Y>X
Set X to X + Y
Set Y to Y - 1
Else
Add 10 to Y
Say Join (X)(Y)
To discover the result, you could make it and see, or you could work through it. Example Continues
So, at the first part, X = 4 and Y = 7
Then the first loop plays
first it checks, Is X greater than Y? NO
this places it into the "Else" category
now it checks, Is Y greater than X? YES
now it makes X equal X + Y. 4 + 7 = 11
now it makes Y equal Y - 1. 7 - 1 = 6
Now it repeats the loop
first it checks, Is X greater than Y? YES
now it makes Y equal Y + X. 6 + 11 = 17
now it makes X equal X - 1. 11 - 1 = 10
3rd repeat
first it checks, Is X greater than Y? NO
this places it into the "Else" category
now it checks, Is Y greater than X? YES
now it makes X equal X + Y. 10+ 17 = 27
now it makes Y equal Y - 1. 17 - 1 = 16
4th (and final) repeat
first it checks, Is X greater than Y? YES
now it makes Y equal Y + X. 16 + 27 = 43
now it makes X equal X - 1. 27 - 1 = 26
Now comes the Say command
Join means list them (in that order), so if X was he and Y was llo, join (X)(Y) would result in "hello"
In this case, X = 26 and Y = 43, so it would say "2643"
No comments:
Post a Comment