Instruction

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

The loop is a fundamental concept in programming. It allows you to repeat the same code for different data. A loop is a block of code that repeats a group of statements a specified number of times or as long as a particular condition is met.

If you know the number of iterations in advance, use the for loop. In this case you will have the loop index that will be incremented or decremented on each iteration.

Note: This is important!

Using the variable name i for a loop index is a common practice in programming. Though you may in your code choose to name it however you want. Another key point to note is that the first index position in a loop always starts from 0 (and not 1).

If you want to iterate over a consecutive range of numbers, you can use the ranges. A range is a sequence of numbers. The elements of the range don’t have to be in the natural order. You can, for example start from 10 and go down to 1. You can also skip some numbers. For example, take every third number to achieve the sequence: 1, 4, 7, 10. The range is a very useful concept in programming. You will use it a lot in loops.

If you want to loop until a certain condition is met, use the while loop or do-while loop. What is the difference between them? Check the diagram below to find out:

Start Body Is condition met? End True False While Loop Start Body Is condition met? End False Do-While Loop True While vs Do-While Loop

The while loop checks the condition before each iteration. Including the very first one. If the condition is false at the beginning, the loop will not execute at all. The do-while loop checks the condition after each iteration. So, it will always execute at least once.

Imagine you are a janitor in the school. You have to clean ten classrooms. They are all the same from your perspective, so you can use the same cleaning algorithm for all of them. For example, you first clean the floor, then the windows, and finally the blackboard. You repeat this process for all the classrooms. This is a perfect example of a loop. You repeat the same process for different subjects. In programming, you can use loops to repeat the same code for different data. This is a very powerful technique, and you will learn how to use it in this lesson.

For the simplicity of the lesson, the cleaning process will be represented by the one-line message printed to the console. The message will be Cleaning classroom i… where i is the ordinal number of the classroom. In the real program, there may be some function calls or other, more complex code there.

The i mentioned above denotes the index position of the loop. For example, you would need a positional index to print classroom number as: Cleaning classroom 1…, Cleaning classroom 2…, Cleaning classroom 3… etc.

Before creating the loop iterating over all the classrooms, you need to know how many classrooms there are. Iterating means to go through the subsequent elements of the range, one by one. You can use the range for that.

See forum comments
Download course materials from Github
Previous: Introduction Next: Demo