Example of an algorithm with repeating statements

In the previous tutorials you learnt about simple algorithms and conditional algorithms. In this tutorial you will learn how to write algorithms for problems which involve repetitive actions. Repetitive actions are called Loops in programming. Using these action you do the same thing over and over again depending on some condition. Lets take a very simple example to understand repeating statements.

Problem definition

Write an algorithm to print numbers from 1 to 1000 separate by commas ( , )

Let’s understand this problem

It’s important to understand what the above single line problem definition says. It says that you need to print the numbers from 1 to 10. Additionally it says that you need to print a comma after each number. Can you see some repeating statements in this problem? If not then think for a moment how will you write the algorithm to do this task. Will you simple print 1,2,3,4,5,6,7,8,9,10,… in a single step? No. This is not the correct way to do this. In this problem you need to write an algorithm in which some steps will repeat themselves.

Let’s look at the algorithm below

Start

Step 1 : Take a variable A
Step 2 : Set A = 1
Step 3 : If A > 1000 then Goto Stop
Step 4 : Print value of A
Step 5 : Print “ , “
Step 6 : Set A = A + 1
Step 7 : Goto Step 3

Stop

Notice how the lines between Step 3 and Step 7 are being repeated over and over again. The condition for repeating these lines is whether the value of A is less than 1000 or not. If not then we keep increasing the value of A and print it. These step will repeat themselves till the value of A becomes 1000. In Step 3 you can see that when the value of A becomes greater than 1000 we ask the algorithm to stop. Otherwise it will keep repeating these steps.

Learning to write repeating statements or loops in programming will help you in writing programs in less number of lines.

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp