These are the coding concepts that are taught on Coding Adventure Part 2 and will be reviewed in this article:

  1. Functions - challenges 76 - 90
  2. Conditional Loops (until) - challenges 91 - 105
  3. Conditional Statements (if, if-else) - challenges 106 - 124
  4. Boolean Operators (and, or) - challenges 125 - 145

Functions

A function is a set of instructions that performs a specific task. The computer will only execute the function when you use it in your code.

You should give a meaningful name to the function to describe what the function does. This will help others easily understand what the function does. For example, in Coding Adventure you define a function that picks up a match and brings it to the pile. The function's name is "collect". Another example is a function that makes the monkey or the rat go to an object. This function's name is "goto".

A function can use parameters, which enable you to use the same code with different values. These values are sent to the function by the main code when using the function. For example, when we use the command "goto banana", we trigger the function "goto" with the value "banana", which tells the function where to go.

The syntax to define a function with no parameters is:

functionName = () ->

# Your code here

To use this function, write:

functionName()

The syntax to define a function with one parameter is:

functionName = (parameter) ->

# Your code here

To use this function, write:

functionName argument

When argument holds the value to be sent to the function.

The syntax to define a function with more than one parameter is (below is an example of defining a function with three parameters):

functionName = (parameter1, parameter2, parameter3) ->

# Your code here

To use this function, write:

functionName argument1, argument 2, argument 3, ...

All the function's code is indented under the function's definition.

Let's go back to the function "goto", which makes the monkey or rat turn to an object and step towards the object. This function uses one parameter, named t.

The function is defined as:

goto = (t) ->

turnTo t

step distanceTo t

Let's see what happens when we use this function with three different arguments. When the computer executes the code it starts with the "main" code, which is the code outside function definitions. When it encounters a function, it moves to the function's code and replaces the function's parameter with the argument used.

The computer starts with line 5 - goto bridge. It moves to line 1 to execute the goto function and replaces t with bridge. So, the actual code that runs is:

turnTo bridge

step distanceTo bridge

Then it continues to line 6 - goto bush. It moves again to line 1 to execute the goto function and replaces t with bush. So the actual code that runs is:

turnTo bush

step distanceTo bush

Finally, it continues to line 7 - goto banana. It moves again to line 1 to execute the goto function this time replacing t with banana. So the actual code that runs is:

turnTo banana

step distanceTo banana

Here is an example of using the goto function:

Try it!

Notice: the value of the function's parameter (when you run the challenge) next to the function's definition. Each time the function is called, the value of the parameter is displayed.

Functions save us from repeating the same code over and over. Look at the example below: the instructions turnTo and step distanceTo are repeated 12 times and the instruction turtle.step is repeated 6 times.

Now, let's see how the same challenge can be solved with two functions. In this solution, the instructions turnTo, step distanceTo and turtle.step are repeated only once each.

Try It!

Conditional Loops

An until loop contains a block of code that will continue to run until a specific condition is met. This condition is called the loop condition. You can think of the condition as a question; every time before running the loop, the computer asks what is the answer to the question. If the answer is no, the loop will keep going and the code inside the loop will be executed. It will only stop once the answer is yes. For example, if the condition is "cat.sleeping()", the computer will ask whether the cat is sleeping.

The syntax to use an until loop is:

until condition

# Your code here

In Coding Adventure we use the until loop with two different conditions:

  1. near object
  2. cat.sleeping()

The function near returns yes when the monkey or rat is near an object (the object is the argument of the function near), otherwise it returns no.

When writing the following code:

until near banana

step 1

The computer asks if the monkey is near the banana. If it is not, then the code inside the loop is executed and the monkey will step 1. In each iteration, the computer checks this condition again. When the monkey reaches the banana, the answer changes to yes and the loop ends.

Try It!

The function sleeping() returns yes when the animal (cat/bear/tiger) is sleeping, otherwise it returns no.

When writing the following code:

until cat.sleeping()

wait()

The computer asks if the cat is sleeping. If it is not, then the code inside the loop is executed and the monkey or rat will wait. In each iteration, the computer checks this condition again. When the cat is sleeping, the answer changes to yes and the loop ends.

Try it!

If we don't pay attention, we might write a condition that will never be met. This will cause the loop to keep on going forever, and may even cause the program to crash.

In the following example, the loop will stop only when the monkey is near the banana. But when running the loop, the monkey is stepping backwards. So, the monkey will never get closer to the banana, and the loop will never stop.

Here is an example of a challenge in which you need to use two until loops in order to solve it:

  1. until the cat is sleeping
  2. until the monkey is near the banana

Try it!

Conditional Statements

Conditional statements are if and if-else statements. They are used for decision making.

Sometimes when writing code, we want something to happen only if a certain condition is met. For example - if it is raining, then I will take an umbrella. Another example: if I am sick, I will stay in bed.

The syntax for an if statement is:

if condition

# Your code here

The computer checks the condition. If the condition is met (the answer is yes), then the code inside the if statement is executed.

In Coding Adventure, the if statement is used to check whether the bananas are frozen; if they are - the goat hits them.

Try it!

Sometimes we want one thing to happen if a condition is met and something different to happen if the condition is not met. For example: if I am sick, I will stay in bed, otherwise, I will go to school.

In this case, we will use an if-else statement. The syntax for an if-else statement is:

if condition

# Your code here

else

# Your code here

An else statement follows an if statement. If the answer of the condition in the if statement is no, then the code inside the else statement is executed.

When using an if-else statement, either the code inside the if statement is executed or the code inside the else statement is executed.

In Coding Adventure, the if-else statement is used to check whether the bananas are green, if they are, then the goat goes to catch them, otherwise the monkey goes.

Try it!

Boolean Operators

The boolean operators taught in Coding Adventure part 2 are and and or.

The operator and combines two conditions. It returns yes when the answer to both conditions is yes, otherwise it returns no. For example, if it is Monday and it is morning, take the garbage out. In this case, if it is Monday but it is not morning, then there is no need to take the garbage out. If it is Wednesday and it is morning, there is also no need to take the garbage out. Of course, if it is Wednesday and it is not morning, there is no need to take the garbage out.

The and operator can be used in an until loop or in an if statement. The syntax to use the and operator (with an until loop) is:

until conditionA and conditionB

# Your code here

The condition of the until loop consists of two parts (conditionA and conditionB). Only when the answer to both parts is yes then the condition of the until-loop will be yes.

The following table shows when the and operator returns yes:

In Coding Adventure, the and operator is used:

  1. with an until loop - to check if two animals (usually, a tiger and a bear) are sleeping before the monkey can pass
  2. with an if statement - to move only the penguins that have both glasses and bow tie

Here is an example of a challenge with the operator and in an until loop.

You need to wait until both bears are asleep before the monkey can get the banana.

Try it!

Here is an example of a challenge with the operator and in an if statement.

You need to move only the penguins with both bow tie and glasses.

Try it!

The operator or combines two conditions. When the answer to at least one of the conditions is yes - the or condition evaluates to yes. Otherwise it is no. For example, if it is summer or it is weekend, sleep late, otherwise wake up early. If it is summer (no matter which day it is), sleep late. If it is the weekend (no matter which season it is), sleep late. In any other case, wake up early.

Another example: if it is raining or it is snowing, wear boots, otherwise wear shoes. You will wear boots when it is either raining or snowing. You will wear shoes when it is not raining and not snowing.

The or operator can be used in an until loop or in an if statement. The syntax to use the or operator (with an if statement) is:

if conditionA or conditionB

# Your code here

The condition of the if statement consists of two conditions (conditionA, conditionB). When the answer to at least one of the conditions is yes, the condition of the if statement will be met.

The following table shows when the or operator returns yes:

In Coding Adventure, the or operator is used:

  1. with an until loop - to check if an animal (tiger/bear) is sleeping or playing before the monkey can pass
  2. with an if statement - to move the penguins that have either glasses or bow tie (or both)

Here is an example of a challenge with the operator or in an until loop.

You need to wait until the tiger is either asleep or playing before the monkey can get the banana.

Try it!

Here is an example of a challenge with the operator or in an if statement.

You need to move the penguins with either bow tie or glasses (or both). Penguins without bow tie or without glasses do not need to move.

Try it!

Here is another example of a challenge with and and or. All penguins with either bow tie or glasses will step. However, penguins with both glasses and bow tie will step more.

Try it!

Relevant Articles:

How to Access the Challenges' Solutions
Coding Adventure - Coding Concepts part 1

Coding Adventure - Coding Concepts part 3

Did this answer your question?