Conteúdo do Curso
Android Development with Kotlin
Android Development with Kotlin
Introduction to Loops
In this section, we will discuss how to create and work with large data sets efficiently and quickly.
Imagine we have 100 products, and we've been given a rather unusual task to increase the price of each by 10% because the brand is becoming more popular and more money is being spent on advertising.
It would be very inconvenient to increase each item individually by writing something like:
But we can write this line just once and use loops for this!
Loops in Kotlin allow you to repeatedly execute a block of code as long as a given condition is met, and include three types: for
for iterating over collections or ranges, while
for executing code as long as the condition is true, and do-while
for executing code at least once, regardless of the initial condition.
Let's look at the first type of loop - the while loop.
While Loop
The while loop executes a code block as long as the condition is true. This means we can read the code as "while the value of the variable i is less than or equal to 10, perform a certain action."
From this, we can understand that as soon as the condition becomes false, the loop execution will stop, and the program will continue running.
Main
fun main() { var i = 1 while (i <= 5) { println(i) i++ } }
This code will print the value of the variable i
until i
is equal to 5. Also, note the i++
construct. This is called an increment, and it is simply equal to i = i + 1
.
Increments are often used in loops to conveniently iterate over elements.
Let's take a closer look at how the loop works:
From the illustration above, it is clear that the code inside the loop will execute as long as the condition is true.
After that, the compiler will exit the loop's body and continue executing the code that follows.
Note that, along with the increment (i++
), there is also a decrement (i--
), which performs the same function as i = i - 1
.
Decrement is also useful and can be used for calculating the factorial, for example!
Note
A factorial is the product of an integer and all the integers below it; for example, the factorial of 5 (written as 5!) is
5 x 4 x 3 x 2 x 1
, which equals120
.
Main
fun factorial(n: Int): Int { var result = 1 var number = n while (number > 0) { result = result * number number-- } return result } fun main() { val number = 5 println("Factorial of $number is ${factorial(number)}") }
Here's how we used a while loop to write a simple function that can calculate the factorial of any number.
I won't fully explain the code written above; try to work through it step by step yourself. This way, you'll understand how the loop and the function work in detail!
Do-While Loop
A do-while
loop is similar to a while
loop, but unlike a while
loop, the code block executes at least once, even if the condition is initially false
.
Let's look at an example where we use these two types of loops with an initially false
condition:
Main
fun main() { //Using the `while` loop while (1 > 2) { println("There is the output from the `while` loop!") } //Using the `do-while` loop do { println("There is the output from the `do-while` loop!") } while (1 > 2) }
As you can see, we wrote two identical loops, and the only difference between them is the use of while
and do-while
.
In both loops, the condition is initially false
. The while
loop was executed 0 times, whereas the do-while
loop was executed once.
Overall, this is the only difference between these two loops.
The syntax of the do-while
loop looks as follows:
For Loop
The for
loop is used for iterating over elements of a collection (such as arrays or lists) or a range of numbers.
Note
We will cover what arrays and lists are a bit later; for now, let's focus on the range of numbers.
Simply put, the for
loop will execute as many times as we need. This is the loop we use to run a block of code a fixed number of times.
For example, if we want to print a message to the screen exactly 5 times, we use a for
loop:
Main
fun main() { for (i in 1..5) { println("$i message;") } }
Using this code, we printed messages from 1 to 5 using a for
loop.
Let's look at an illustration that shows the elements of such a loop:
So, if we run the code from the illustration, we will see the following result:
Main
for (i in 1 .. 10 step 2) { println("Iteration $i") }
The for
loop is very convenient for iteration when we execute code a fixed number of times and automatically increment the value of the variable i
(iterator).
This loop is also indispensable when working with collections of data, which we will discuss in the next chapter!
Obrigado pelo seu feedback!