ClassVII Coding Student Handbook Chapter 02
ClassVII Coding Student Handbook Chapter 02
11 | P a g e
Every day, we do many activities in a 2.4 Examples of Sequence,
sequence. For example, think of how you Selection and Iteration
would make a sandwich. If you had to
write down the steps for making a To understand concept of sequencing in
sandwich, would it be like the steps programming, look at the below
given below? algorithm to swap two numbers:
Step 1: Start
Step 2: READ num1, num2
Step 3: temp = num1
Step 4: num1 = num2
Step 5: num2 = temp
12 | P a g e
considered a senior citizen if his age is follow the flow as shown in the Fig 2.1
above 60 years old. flowchart.
print(“Person is a senior
citizen”);
else
print(“Person is not a senior
citizen”);
13 | P a g e
2.5 What is a Bug? For example, suppose you create a boat
which is expected to sail in ocean. Now
A bug is a terminology
when the boat is ready and you try to
used to describe any
sail it in the water, you realize that there
unexpected problem in
is a small hole in the bottom on the boat
your program. Just like
from where water is seeping in. This
we learnt in the past water seeping in the boat at a slow speed
topics, we follow a sequence of activities
may create a problem for the boat in the
to complete a program. This program is long run. Thus, this hole in the bottom
expected to return a specific output. Any of the boat can be termed as a “bug” in
deviation in the expected and actual programming.
output of the program is known as a
bug.
Let us now see how we can make a rectangle in Minecraft using the above steps. You
should try this exercise on Minecraft using the MakeCode editor for Minecraft, which
can be found here https://minecraft.makecode.com/
14 | P a g e
Once you complete this exercise, the final output should look like as shown in the
screenshot below:
Let us now follow below steps to get this output on our screen:
15 | P a g e
Now follow the following steps
16 | P a g e
17 | P a g e
18 | P a g e
19 | P a g e
If you complete the steps above, you should be able to create a rectangle.
Note: Minecraft is just one of the platforms to achieve this output. You can use many
similar platforms available online to achieve similar output like – Scratch
(https://scratch.mit.edu/) and Code.org (https://code.org/).
In this activity, we will create a game with 2 sprites, a Player sprite and an Apple sprite.
The objective of this game is to chase and catch the wandering apple and collect as
many points as possible before the time runs out. Every time the apple is caught, points
are added, and the timer is restored.
You should try creating this game on Arcade using the MakeCode editor which can be
found here https://arcade.makecode.com
20 | P a g e
Now follow the steps mentioned below to create the game.
21 | P a g e
22 | P a g e
23 | P a g e
24 | P a g e
25 | P a g e
26 | P a g e
27 | P a g e
28 | P a g e
Note: Arcade is just one of the platforms to achieve this output. You can use many
similar platforms available online to achieve similar output like – Scratch
(https://scratch.mit.edu/) and Code.org (https://code.org/)
29 | P a g e
success condition of outer loop triggers
Activity the inner loop which runs and reaches
completion. This combination of loops
• Create a square of size 10 and inside loop is helpful while working with
spawn 2 sheep in it. requirements where user wants to work
• Create two squares such that they of multiple conditions simultaneously.
have a common side. Spawn 2 There is no restriction on how many
chickens in one square and 2 cows loops can be nested inside a loop.
in the other.
As we have studied, use of loops make Now, let us discuss the other two
our code manageable and organized. important aspects of an algorithm –
Mainly, there are three different types of selection and iteration.
loops:
Selection refers to the situation in which
the algorithm needs to make a choice
1. While Loop between two or more alternatives. It is
2. For Loop like asking questions like “Is it raining?”.
3. Nested Loop If the answer to the question is true, the
algorithm follows one path. If the answer
is false, the algorithm follows a different
While Loop path.
The While Loop can execute a set of Iteration refers to the process of doing
commands till the condition is true. the same task over and repeatedly, until
While Loops are also called conditional a certain task is complete, or a certain
loops. condition is met. The iteration can also
Once the condition is met then the loop be set to run for a specific number of
is finished. times. For example, think of a race in
which a car must go around a track five
times. The car will keep going around
For Loop the track repeatedly until it completes
For Loop is needed for iterating over a five laps. Once that is done, it will exit
sequence. A for loop executes for a the track.
specific number of times. Now let us see how we can combine all
Nested Loops the three building blocks of algorithms –
sequencing, selection, and iteration.
Any loop in program may contain
another loop inside it. When there is a In real life, complex algorithms can have
loop inside another loop, it is called as a hundreds, if not thousands, of steps in
nested loop. How it works is that first a sequence. However, often while
working on a sequence, you will notice
30 | P a g e
that certain activities in the sequence For example, in the last exercise, while
are repeated. We can reduce the number creating a rectangle, we had to turn right
of steps of the sequence by using a loop after drawing a line and we had to do so
along with certain conditions to check three times.
when the loop should stop.
Can we use a loop to reduce the number
of steps
With an example, let us now understand sequencing with loops and conditionals. What
would be the steps to draw a square of side 5 cm on your screen?
Do you notice a pattern getting repeated in the steps? Let us see how we can use a loop
to reduce the steps by using the Minecraft platform.
First, create a new project called “Make Square”. Once you create your project, you
should see the editor below
At the end of this exercise, the final output should look like the one shown in the image
below:
31 | P a g e
32 | P a g e
33 | P a g e
Note: Minecraft is just one of the platforms to achieve this output. You can use many
similar platforms available online to achieve similar output like – Scratch
(https://scratch.mit.edu/) and Code.org (https://code.org/).
34 | P a g e
Take a look at flowchart in Fig 1.0 understand how Arun uses concept of iteration while
distributing birthday sweets with the class.
If you notice, there is a pattern which Arun follows while distributing sweets. He takes
the chocolates out from his bag, gives one chocolate to a student, moves to the next
student and repeats the same steps again till the sweets are distributed within all the
students.
This is an example of an iterative process. Repetition of a set of steps with a defined end
– in this case the repetition ended when all the students in the class were given
chocolates.
35 | P a g e
2.12 Quiz Time
36 | P a g e
Standard Questions
Applied Project
Create a rectangle of length 5 and height 3 using a loop (repeat block) in Minecraft.
By now you:
37 | P a g e