Skip to main content

How to learn a new programming language - Part 2

Lesson Objective

You learnt the simple and beginner friendly language “LOGO” in the previous lesson. This lesson is a continuation of the previous lesson and goes into further detailed features of the language.

Introduction to the lesson

You have already learnt around 20 basic commands used in LOGO programming in the previous lesson. We have also created a few shapes and figures using those commands. Let’s look at some more features and commands of LOGO that will give you more power for drawing more complex and amazing figures. This is a self-paced class too.

To run LOGO programs, you can use this online tool: Online LOGO Interpreter

The REPEAT Command

We often repeat a sequence of commands. Computer programs in general perform a lot of repetitive tasks. Just about every programming system has a way of carrying out this repetition, or iteration, as computer scientists call it.

Let us consider the following example, Let’s say we want to draw a square with sides of length 100, we can do this with the following sequence of commands –

fd 100 rt 90 fd 100 rt 90 fd 100 rt 90 fd 100 rt 90

We note that the commands – fd 100 rt 90 are repeated four times. Will it not be simpler to tell the computer that it should just repeat these two commands four times instead of writing them four times in a row? We can do exactly this by using the REPEAT command.

REPEAT n [instruction list]

  • This command repeats the action listed in the instruction list n number of times.

  • To draw the square like in the above example, we would just have to write it as follows

    repeat 4 [fd 100 rt 90]

  • Any command can be done inside a loop i.e. we can even use a loop within a loop. A loop inside another loop is called a nested loop. This gives us even more power because now you can draw figures with even less commands than before.

Problem 1: Try drawing an asterisk(*) using loops. The asterisk should have 18 arms instead of the five or six you normally see on a keypad. The figure should look something like this:

Check the Hint!

Hint: To draw eighteen arms we would rotate the circle each time by 360/18 = 20 degrees

Check the Solution!

cs repeat 18 [ fd 80 bk 80 rt 20]

We draw the line 18 times by using the repeat command and each time we also rotate the turtle by 360/18 = 20 degrees.

Problem 2: Draw the following grid of squares representing using LOGO.

Check the Hint!

Hint: Try drawing the figure by using the loop we used for drawing a square above and then using nested loops

Check the Solution!

cs rt 45 repeat 4 [repeat 4 [fd 50 rt 90 ] rt 90 fd 50]

  • First we rotate the turtle initially to draw an “X” instead of “+”

  • Then the inner loop draws a square as in the example we discussed above and the outer loop draws the square 4 times.

  • Each time after drawing one square we rotate the turtle by 90 degrees and move it forward by 90 units

Problem 3: Draw 30 octagons with edges that are each 50 units to formulate the following “Mandala” figure.

Check the Hint!

Hint: Try forming an octagon first using a loop and then calculate how many degrees to rotate to draw 30 octagons in 360 degrees.

Check the Solution!

cs repeat 30 [rt 12 repeat 8 [fd 50 lt 45]]

  • Repeat 8 [fd 50 lt 45] is used to draw an octagon
  • The outer repeat command is used to draw 30 octagons
  • The turtle is rotated by 12 degrees (360/30 = 12 degrees) each time before the inner loop draws an octagon

Problem 4: Draw the following spiral using loops

Check the Hint!

Hint: We can use a variable called “size” to move the turtle according to it and then every time the turtle turns right in each iteration we increase the value of size (by 5 here).

Check the Solution!

cs make "size 10 repeat 50 [ fd :size rt 90 make "size :size + 5 ]

Now let’s look at another very useful command. Check out the following thought bubble first and write down your thoughts.

Randomisation (The RANDOM Command)

In some situations, we need to generate random numbers, for e.g. random positions where your target will appear in shooting games or to play dice or tic-tac-toe. This can be achieved with the help of the RANDOM command.

RANDOM n

  • This command returns a random number that ranges from 0 to n-1.

  • Example: random 7 will give a number from 0 to 6 similar to a die

Let’s look at one example on how to draw a figure using random numbers. The output you get will be different from the figure shown…(that’s the beauty of something random, you create unique art everytime!)

Problem 5:Draw 20 squares all with one vertex at home position

  • with random pen colours (colour codes from 0 to 15)

  • with random pensizes (maximum 4)

  • with random side length(at max 199) and at random directions (at max 360)

Check the Hint!

Hint: We have to use a variable called “size” and assign a random value to it in each of the 20 iterations along with setting the size and colour of the pen as random. We should also change the direction of the turtle using the rt command in the repeat block.

Check the Solution!

cs repeat 20 [ setcolor random 16 setpensize random 5 make "side random 200 repeat 4 [fd :side rt 90] rt random 360 ]

Problem 6: Draw the following circular spiral with a random pen colour and pen size as 2.

Check the Hint!

Hint: Start with a variable that represents the size of arc for the iterations and draw arcs of 180 degrees in each repetition and then adjust the turtle so that the ends of the arcs from two consecutive iterations meet up.

Check the Solution!

cs rt 90 setpensize 2 setpencolor random 16 make "size 1 repeat 30 [ arc 180 :size pu bk 5 pd rt 180 make "size :size + 5 ]

  • First we adjust the turtle by rotating it to the right by 90 degrees because we want to draw the arcs from West to East or East to West.

  • Then we initialise a size variable by 1 and we draw arcs with radius equal to size in each iteration.

  • The most important step here is we move back the turtle by the same amount with which we are increasing the value of radii in each iteration. Try to observe a bit closely and you’ll understand why.

  • Then we flip the turtle(rotate by 180 degrees) to draw the arc in the opposite direction to the previous arc.

  • At last, we increase the value of size by 3 and this is repeated.

Now, try some more shapes.

Source: Giphy

Problem 7: Draw a heptagon with sides as 50 units

Check the Solution!

repeat 7 [fd 50 rt 52]

Problem 8: Draw the following diagram using LOGO

Check the Solution!

repeat 5 [ arc 360 50 fd 100 rt 90 ]

Problem 9: Draw the following staircase using LOGO

Check the Solution!

fd 50 rt 90 fd 50 rt 90 fd 50 lt 90 fd 50 rt 90 fd 50 lt 90 fd 50 rt 90 fd 50 rt 90 fd 250 rt 90 fd 50 rt 90 fd 50 lt 90 fd 50 rt 90 fd 50

Problem 10: Draw the following figure using squares of side 100.

Check the Solution!

repeat 5 [repeat 4 [fd 100 rt 90] rt 72]

Problem 11: Draw the following figure using LOGO programming

[Hint: This is just an advanced version of the previous problem.]

Check the Solution!

repeat 30 [repeat 4 [fd 100 rt 90] rt 12]

Problem 12: Draw the following dashed octagon using LOGO programming

Check the Solution!

repeat 8 [ repeat 5 [setwidth 1 fd 10 setwidth 4 fd 10] lt 45 ]

Problem 13: Draw 15 semi-circles all with their centres at home position

  • with random pen colours (colour codes from 0 to 15)

  • with random pensizes (maximum 9)

  • with random radii (at max 199) and at random directions (at max 360)

Check the Solution!

repeat 15 [ setcolor random 16 setpensize random 10 rt random 360 arc 180 random 200 ]

Problem 14: Draw the following triangular spiral pattern using LOGO

[Hint: The size of the first side of the innermost triangle is 20. Also, the number of repetitions in spiral patterns is not important.]

Check the Solution!

make "size 20 rt 30 repeat 60 [ fd :size rt 120 make "size :size + 7 ]

Woah, we have come this far!!

Source: Giphy