What term refers to repeating instructions until some condition is met in an algorithm?

Note: This is a part of an ongoing series about the essentials of programming and coding. Click here for the first part.

WHAT ARE LOOPS?

A loop is a programming structure that repeats a sequence of instructions until a specific condition is met. Loops allow you to repeat a process over and over without having to write the same (potentially long) instructions each time you want your program to perform a task.

Two major types of loops are FOR LOOPS and WHILE LOOPS. A For loop will run a preset number of times whereas a While loop will run a variable number of times.

TYPE OF LOOP — FOR LOOP

For loops are used when you know how many times you want to run an algorithm before stopping. Let’s use loading dirty dishes into a dishwasher as an example of a For loop.

Imagine that you have three dishes in your sink and you want to run an algorithm that has two steps: 1) pick up a dirty dish from the sink and 2) put the dish in the dishwasher. You know you would need to run this process three times to get all of your dishes out of the sink.

TYPE OF LOOP — WHILE LOOPS

A While loop is similar to a For loop but a While loop runs a variable number of times and uses a CONDITIONAL. Let’s go back to the dishwasher example. Say that you have run your dishwasher and you are now unloading it but one of the dishes is still dirty with food that is stuck to it. You decide to run a scrubbing algorithm which has the following steps:

1) use a scrub brush to scrub the dirty dish​, 2) rinse the dish with water, 3) check if the dish is still dirty​, 4) (CONDITIONAL) if the dish is still dirty then repeat the scrubbing algorithm but if it’s clean then put it away in the cabinet. This is a While loop because there is a conditional involved and because you do not know how many times you may have to repeat the scrubbing algorithm until you get your desired result of having a clean dish.

MAJOR TYPES OF LOOP USES

Uses of loops include cycling through values, adding sums of numbers, repeating functions, and many other things. Two major categories of loop uses are producing output and searching for information.

LOOP USE — PRODUCING OUTPUT

You can reduce a long series of repetitive instructions down to one instruction by using a loop.

For instance, a long series of multiplying numbers can be rewritten and condensed into a For loop. You are able to complete the exact task you want to without explicitly writing repetitive instructions line after line after line.

LOOP USE — SEARCHING FOR INFORMATION

Having an algorithm check through items in a list is incredibly useful. Let’s say that you have a list and you want to find a specific piece of information contained in that list.

In this example we would set a loop to iterate through every item until we found the one we wanted. Once the answer we wanted was found, in this case a pet by the name of Fluffy, then the loop would stop and the leftover items in the list would not have to be checked.

To summarize, a loop is a sequence of instructions that is repeated until a condition is met. The key difference between the two major types of loops is that a For loop will run a set number of times whereas a While loop will run a variable number of times. Two major uses of loops are to produce output and to search for information.

In the next part of this series I’ll cover an introduction to VARIABLES.

  • YouTube Channel: https://www.youtube.com/channel/UC121Llka7PK_A56uiQojxqQ
  • GitHub: https://github.com/PurpleDogEnterprises
  • Blog on Medium: https://medium.com/@seanguthrie
  • LinkedIn: https://www.linkedin.com/in/sean-patrick-guthrie-07384494/
  • Purple Dog Enterprises website: http://purpledogenterprises.com

GCSE Programming Resources (14-16 years)

  • An editable PowerPoint lesson presentation
  • Editable revision handouts
  • A glossary which covers the key terminologies of the module
  • Topic mindmaps for visualising the key concepts
  • Printable flashcards to help students engage active recall and confidence-based repetition
  • A quiz with accompanying answer key to test knowledge and understanding of the module

A-Level Problem solving and programming (16-18 years)

  • An editable PowerPoint lesson presentation
  • Editable revision handouts
  • A glossary which covers the key terminologies of the module
  • Topic mindmaps for visualising the key concepts
  • Printable flashcards to help students engage active recall and confidence-based repetition
  • A quiz with accompanying answer key to test knowledge and understanding of the module

Iteration is the term given to the repetition of a block of instructions (code) within a computer program for a number of instances or until status is encountered.  When the first group of instructions is carried out again, it is called an iteration.  When a cycle of instructions is carried out in a repeated manner, it is called a loop.  It is the replication of a process in a computer program, commonly executed with the use of loops.

Iteration is essential as it lets a programmer streamline a design by declaring that definite steps will be repeated.  It is also briefer since a number of irrelevant steps are removed.  Steps that are part of the loop are indented.  Indentation is used to show which steps are to be repeated.  Many computer programs use iterations to execute specific tasks, resolve problems and provide solutions.

Types of Iteration

There are two types of iteration:

Count-controlled loops

– used for iterating steps a specific number of times. It is used when the number of iterations to take place is already known.  It uses a counter to keep track of how many times the set of commands has been repeated.

Count-controlled loops are executed using FOR statements.

  • for – specifies the starting point of the iteration
  • range – indicates how many times the program will iterate

Count Controlled Loop Example

total = 0
for count in range (4):
number = int (input (“Type in a number: “))
total = total + number
print (“The total is: “)
print (total)

A count controlled loop iteration example

The above program works as follows:

  • The variable ‘count’ is used to keep track how may times the iteration happened.
  • The ‘for’ statement is used to determine where the loop starts.
  • The ‘range’ declaration signifies the number of instances the loop is to occur.
  • The variable ‘count’ regulates the repetition.
  • In every repetition, program consequently adds 1 to the value of ‘count’.
  • The program continues to iterate if the value of ‘count’ is in the range declared in the loop. Once it gets to the end of the range, the repetition stops.

In the above example, the program repeats 4 times.  To have a different number of repetitions, the value ’4’ would simply have to be changed to the number of times preferred.

Condition-controlled loops

– used for iterating steps continuously until a condition is met. It is used when the number of iterations to take place is unknown.  The algorithm tests the condition to see if it is true.  If true, the algorithm executes a command.  If false, the algorithm goes back to step 1 and will continue to go back until the condition becomes true.

Condition-controlled loops are executed using WHILE statements.

Condition Controlled Loop Example

total = 0
answer = “yes”
while answer = “yes”:
number = int (input (“Type in a number: “))
total = total + number
answer = input (“Any more numbers? yes/no “)
print (“The total is: “)
print (total)

Code example

The above program works as follows:

  • The ‘while’ statement is used to indicate where the iteration starts.
  • The condition ‘answer = “yes”’ is used to manage the iteration. It must be set to “yes” at the start so the code runs at least once.
  • The program assesses the condition by checking if the variable ‘answer’ equals “yes”.
  • If the condition is true, the program repeats.
  • If the condition is false, the program progresses to the next step.

The program iterates as many times as is necessary and will keep iterating as long as the condition is met.
There are instances where a condition-controlled loop loops forever.  This is called an infinite loop.  An infinite loop ensures that a program runs forever which can be used in controlling a traffic light and other similar scenarios.

Further Readings:

  • Iteration

What term refers to repeating instructions until some condition is met in an algorithm?

What is repetition in an algorithm?

Repetition allows algorithms to be simplified by stating that certain steps will repeat until told otherwise. This makes designing algorithms quicker and simpler because they don't need to include lots of unnecessary steps.

What is the process of repeating sections of a program called?

The process of performing the same task over and over again is called iteration, and C++ provides built-in iteration functionality. A loop executes the same section of program code over and over again, as long as a loop condition of some sort is met with each iteration.

What is the process of repeating a block of instructions?

Iteration is the term given to the repetition of a block of instructions (code) within a computer program for a number of instances or until status is encountered.

What is the correct term for looping or repetition?

Iteration is a term similar to repetition: it means to continue repeating an action until you achieve the correct outcome.