Note: This is a part of an ongoing series about the essentials of programming and coding. Click here for the first part. Show
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.
A-Level Problem solving and programming (16-18 years)
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 IterationThere 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.
Count Controlled Loop Example
A count controlled loop iteration example The above program works as follows:
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
Code example The above program works as follows:
The program iterates as many times as
is necessary and will keep iterating as long as the condition is met. Further Readings:
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.
|