The first loops—do and do/while—are identical in Kotlin as they are in Java. Learn how to use them and what the difference is between them.
- [Instructor] The first two loops, while and do-while, behave exactly as they do in Java. The while loop checks the conditional expression. If it is true, it executes the block of code and then repeats the process until the conditional is no longer true. We'll create a variable, call it x, set it equal to zero. We'll create a while loop. We'll execute this loop while x is less than 10.
We'll just print out what x is. We will increment x by three. If we execute this code, we get zero, three, six, nine, and then the next value, 12, is not shown because x is greater than 10. The do-while is similar except it executes the code block first then checks the conditional repeating until it is false.
If we take the same loop, we'll create a do, we'll do a print again, and display the value of x. This time we will decrement x by three. We will keep doing this while x is greater than zero. If we execute the code, we see x goes zero, three, six, nine then in the do-while, it's going to go 12, nine, six, three, getting down to zero.
At that point, the loop ends and that's the end of the code. The only difference between while and do-while is that do-while executes the code block at least once. Perhaps the most classical of the loops is the For statement. The For statement is a bit complicated. It consists of four parts, a loop initializer, a loop check, a loop iterator, and a loop body. The first three parts are contained within the parentheses of the For keyword.
The loop body is either a single statement following the parentheses or a series of statements included with curly braces. The complexity of the For statement is the source of a lot of bugs. Luckily, Kotlin's For statement doesn't work this way. Many languages including Java 8 have a forEach statement. forEach iterates over collections. Kotlin doesn't have a forEach statement. Instead, Kotlin's For statement works in conjunction with any type that has an iterator.
Conceptually, this is similar to how a forEach statement works. Let's go ahead and create one. I'm going to say, "forItem in "1..10." Inside this loop, we'll just print the value of the item. Now one thing that's really important to note is that Item is a value type. It is an immutable.
If we try to modify it, you notice that we get the red squiggly line underneath because "val" cannot be reassigned. Now if we execute this code, I'm going to get the numbers one through 10 displayed. The important parts of Kotlin's For statement are the iteration value which is indicated by item, the iterator which is the range, one to 10.
Finally, the For body which is everything inside the curly braces. The iteration value is a "val" and can't be modified. The range is closed so all the numbers from one to 10 including one to 10 are rendered. Any iterator can be used in the For statement. Kotlin includes iterators on many types including strings. If we'd like to iterate over a characters in a string, here's the code. We create another For. This time our iterator's going to be ch.
We're going to do it on a string so we give ourselves a string. We'll just print out the values of the string one letter at a time. When we execute this code, we can see the letters of the word "biscuit" going down inside the console.
- Kotlin as a better Java
- Setting up a Kotlin programming environment
- Val vs. var
- Understanding basic Kotlin programming concepts
- Object-oriented programming
- Using Java from Kotlin
- Using Kotlin from Java
- Annotations, reflection, and DSL construction
- Functional programming in Kotlin