Page 74 - Digicode_AI_class_7
P. 74
Apply loops and Conditionals with Sequencing
Let us learn how to use loops and conditional with the help of sequencing.
Selection
Selection can be described as making a choice based on a criteria. Think of it as IF this THEN that.
Selection is an important part of coding. It helps us to choose different paths and react to situations.
Example: ‘Is it raining today?”
If the answer to the question is true, then the algorithm follows one path.
If the answer is false, the algorithm follows a different path.
Iteration
To run a block of code specific number of times, iteration is used. Iteration can be defined as doing
or repeating the same task again and again until the specified condition is met. Iteration is repeating
situations.
Example: In a car race, a car must go around a track five times. The car will keep going around the track
repeatedly until it completes five laps. Once it is done, it will exit the track.
Now let’s combine all the three building blocks of an algorithm: sequencing, selection, and iteration.
Let’s look at our cup of tea task with sequence, selection and iteration. We have the same number
of steps but our process has become more precise (how long to stir for), efficient (we don’t fill the
kettle if it has water in it already) and we have choice (milk or no milk).
Step 1 IF the kettle has no water THEN fill the kettle with water
Step 2 Turn the kettle on
Step 3 Put tea bag in the cup
Step 4 Pour hot water into the cup
Step 5 Remove tea bag
Step 6 IF you take milk THEN add milk
Step 7 If you take sugar THEN add sugar. Stir until the milk has been mixed into the tea
In real life, complex algorithms can have hundreds of steps in a sequence and there are certain activities
in the sequence which are repeated. We can reduce the number of steps of the sequence by using a loop
along with certain conditions to check when the loop should stop.
What is a Bug?
A bug is an unexpected problem in your program. You follow a defined sequence to write a program,
from which you expect to return a specific output. Any change in the expected and actual output of
the program is said to be the result of a bug.
So, we can say that bug is a general term which is used to describe any unexpected problem with
hardware or software.
Example: Suppose you are going out for a family picnic. You are ready to go, but you notice that
the tyre of the car is puncture, due to which your picnic will get delayed for few hours or you need
to postpone it for some other day. As first you need to repair the tyre. So, this puncture in the tyre
of the car can be termed as a ‘bug’ in programming.
72 DigiCode AI-VII

