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.

Livnat Hershkovitz avatar
Written by Livnat Hershkovitz
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:
3.times ->

# 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
x is 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.
Notice: the Variables table at the lower part of the editor (when you run the challenge). It shows the variables and their values. When the value of a variable is changed, it is also updated in the table.

Variables are powerful when used within loops. In the example below, d is a variable. It is first assigned with a value of 6. Then, in each iteration of the loop (i.e., 3 times) it is increased by 5. Therefore, the values of d will be:
Before the loop: d is 6
After the first iteration: d is 11 (6 + 5)
After second time: d is 16 (11 + 5)
After last time: d is 21 (16 + 5)


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[1], 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[1] and turtles[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_name

# 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 first replaces loop_variable 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[1], bananas[2], bananas[3]. We write the following code:
for b in bananas
    turnTo b
    step distanceTo b
The first time this loop will run, the computer will replace b with bananas[0]. The actual code that will run is:
turnTo bananas[0]
step distanceTo bananas[0]
Then, b will be replaced with bananas[1]. The actual code that will run is:
turnTo bananas[1]
step distanceTo bananas[1]
Then, b will be replaced with bananas[2]. The actual code that will run is:
turnTo 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.


Relevant Articles:


Did this answer your question?