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:
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
This content was released on May 22 2024. The official support period is 6-months
from this date.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.