Loops in Kotlin
The course is part of this learning path
This course explores Kotlin loops, including for loops, forEach loops, while loops, infinite loops, and do-while loops.
This course is ideal for anyone who wants to learn how to use Kotlin for developing applications on Android.
This content will take you from a beginner to a proficient user of Kotlin and so no prior experience with the programming language is required. It would, however, be beneficial to have some development experience in general.
Hello everyone. All right, so in this video, we're going to learn about the Do - While loop. Now, the Do - While loop could be considered similar to the while loop. With the only difference is that it checks for the condition after executing the statements in the loop body. So, in other words there's no checking of any condition at the outset. So, after the execution of the statements in the loop body and the update of the variable value, the condition is then checked to be true or false. So, then if it's evaluated to be true while the next iteration of the loop starts. If the condition returns false, the loop terminates and jumps to the next statement after the Do - While loop. So, the statements in the loop body are executed at least once before condition controlling. That is the most important point. So, why don't I show you a few examples with this
Do - While loop. First up, I'll create a new kotlin file. I'll name, this file as DoWhileLoop. And now let's create the main method. So, we'll write a program that prints int numbers from 1 to 15. Let's declare a variable number with initial value to be 1. Now, let's start the DoWhileLoop with the do keyword.
Now, in loop we'll use the print method to display numbers. So, I'll put a dollar sign before the number. We need to update the variable number so we increase the value of number by one and now we add a loop condition with the while keyword. Our condition is number is <= 15. All right, so let's run the codes here we go. And look what about that? Here are the numbers From 1 to 15 right there on the screen. So, in the Do - While loop, the print process and increment part of the body's executed at least once before the condition. All right my friends, that's how the Do - While loop works. So we've learned about the loops that we use in Kotlin. So, I just want to take a quick break here and we'll talk about methods in the next video. All right, so see you in the next one.
Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.