[go: up one dir, main page]

0% found this document useful (0 votes)
812 views6 pages

Code Monkey - 1

code monkey _1_

Uploaded by

heng Chong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
812 views6 pages

Code Monkey - 1

code monkey _1_

Uploaded by

heng Chong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 6
Coding Adventure Part 1- Coding Concepts This article will provide you with in-depth explanations of the coding concepts taught on Coding Adventure Part 1, how and when to use them. Wirtton by Livnat Hershkovite Updated over a week ago These are the coding concepts that are taught on Coding Adventure Part 1 and will be reviewed in this article: 1. Simple Loops (times loop) - challenges 21-30 2. Variables - challenges 31 - 50 3. Array and Indexes - challenges 51-60 4. For Loops - challenges 61-75 Simple Loops (times) A simple loop is a block of instructions that are repeated for a specified number of times. This number is specified together with the command "times’, separated by a dot. For example, to write a loop that repeats 3 times, use: Sutimes -> # Your code here The instructions inside the loop are indented. To indent code, use the tab key. Using a loop can make your code shorter. For example, in the challenge below, we repeat the same code three times for stepping and turning to catch all the bananas: Instead, we could use a loop! Variables A variable can be thought of as a container that holds one value at a time. The value can be any kind of data, for example, number, string, or array. A variable is referred to by a name and its value can be changed. To assign a value to a variable, use the "=" symbol. So, in the following code: x= 10 xis the variable name and 10 is the value assigned to it If you want to change the value of x, you can assign a different number to x. For example: x=15 Now the value of x is 15. When you use step with a variable, the computer replaces the variable with its assigned value. Hence, the monkey will step a number of steps according to that value. eanceTo bonanas(t] ‘turnTo bonanas(1} step d cBei Variables A variable can be thought of as a container that holds one value at a time. The value can be any kind of data, for example, number, string, or array. A variable is referred to by a name and its value can be changed. To assign a value to a variable, use the "=" symbol, So, in the following code: x= 10 xis the variable name and 10 is the value assigned to it If you want to change the value of x, you can assign a different number to x. For example: xais Now the value of x is 15. When you use step with a variable, the computer replaces the variable with its assigned value. Hence, the monkey will step a number of steps according to that value. sey “Boo!” d = distanceTo bononas[@] turnfo bananas(0) step ¢ step -¢ d = distonceTo bononas[1] 1) cBe4 eet 1.) Array and Indexes An array is a named collection of items. The items have order in the array. These items are accessed by using square brackets -[]. To refer to an item in an array, use the array's name and square brackets, inside the brackets write the index number of the item. The first index is 0. The last index will be the number of items in the array minus 1 (since the first index is 0) A commonly used array in CodeMonkey is called bananas. It holds the bananas in a challenge. To refer to the first banana, use bananas{0}; to refer to the second banana, use bananas{fJ, and so on. Other examples of arrays in the game are: beavers, crocodiles, turtles, and even bushes. In the example below, turtles is an array, and we move the turtles by using turtles[0), turtles(t) and turties{2). For Loops ‘A for loop is another type of loop. It is used when you have an array and we want to repeat the same action for each item in the array. The for loop will keep going until all the actions were completed on all the items in the array. The syntax to use a for loop is: for loop_variable in array_nane # Your code here The loop_variable is a name we assign; it can be any name we want. It is common to name it after the first letter of the array's name. When the computer executes a for loop, it frst replaces Joop_voriable with the first item in the array and runs the instructions inside the loop for that item. Then, it continues to the second item in the array, and so on. So, the loop will run as many times as the number of items in the array. For example, suppose we have an array with 4 bananas, which is called bananas. The names of the items in the array are: bananas/0}, bananas{!), bananas(2}, bananas{3}. We write the following code: for b in bananas turns b step distanceto b The first time this loop will run, the computer will replace b with bananas{O]. The actual code that will run is: ‘turnTo bananas [0] step distanceTs bananas [0] Then, b will be replaced with bananas{1), The actual code that will run i ‘turnTo bananas [1] step distanceTo bananas [1] Then, b will be replaced with bananas[2). The actual code that will run is: turaTo bananas [2] step distanceTo bananas (2) Finally, the last run will be: ‘turnTo bananas[3] step distanceTo bananas [3] Note how effective a for loop can be. In the example below, the array name is turtles and the loop variable is t. Each time, a different turtle in the array will step 20, and eventually all the turtles will move. for € in turtles step 18

You might also like