loops

In programming we must specify precisely all the steps necessary to complete a task. For this reason, when programming, it is important to be clear about how to define a loop, so that we can implement them correctly when writing the algorithms for our programs.

We have first let’s look at the main blocks of a loop.

  • Definition of the repeat characteristics
  • repeat bodywhich are the statements to be repeated.

loop body

The body of the loop is very easy to identify. It contains one or more statements that we need to repeat at each execution of the loop. For example, in the case of dishes, perhaps the repetition includes several sentences if we are more detailed.

repeat { grabAPlateFromStack; soapTheDish rinseTheDish leaveTheDishDraining } while_that(There_are_dishes_in_the_stack)

In many programming languages, the body of the repetition is expressed in braces. With them we include the block of sentences that are going to be repeated.

repeat characteristics

The repetition characteristics allow us to control the number of times that the statements in the loop body will be executed, that is, they allow us to define how many times it will be iterated, or if the loop will be repeated until a certain condition is met.

When we start programming and implement the first loops, it may be difficult for us to find a way to express the characteristics of the repetitions, so it is ideal to be clear about what they are. Depending on the programming language and the control structure for repetition that we are using, the characteristics of the loop can be expressed in different ways, but implicitly or explicitly the same things must always be indicated.

  • loop initialization
  • Condition which must be true to continue iterating the loop
  • increase, decrease, or variance to perform for each repetition
See also  How to see all open ports on Mac

Why do we need to express all this? Remember that to define algorithms it is necessary to give precise instructions to computers. If I ask someone to “wash the dishes in the stack,” that person knows implicitly that he will have to start with the first dish and finish with the last. However, a computer does not take anything for granted, so we must be very precise.

If we ask a computer to “Say Hello 10 times”, we need to provide the algorithm with a way to count those 10 greetings, so that it can perform its task and stop exactly when it is complete.

In the most basic cases, we will keep the count of the repetitions with a variable, which we will increase in each iteration to know how far we have to repeat the statements of the body of the loop. For example, we will count up to 10, initializing at 1 and increasing the variable by one at each iteration of the loop, to stop when the greeting has been performed exactly the 10 times that we have been asked to do.

For the example of greeting 10 times we will have the following repetition characteristics:

  • Initialization: create a variable with the value 1
  • Condition to continue the loop: that the variable has not exceeded 10
  • Variance: we will increase the variable by 1 in each iteration

Usually those characteristics of the repetition are expressed in the header of the loop, but that depends on the language or the type of repetition structure that we are using.

Loading Facebook Comments ...
Loading Disqus Comments ...