‫Welcome back.
‫In Kotlin.
‫As in many other programming languages, loops are used to repeat block of code multiple times.
‫Kotlin supports several types of loops, including for, while, and do while loops.
‫Let's start with the for loop.
‫The for loop is used to iterate over a range, a collection, or a sequence of elements.
‫It has a defined starting and ending point.
‫To use the for loop, I'll use for keyword, and inside these parentheses I'll include the range.
‫So in this example I am iterating over a range I in one to dots to five.
‫What I want to do I need to print new line I.
‫If we run the program, we can see that the for loop iterates from 1 to 5 inclusive and prints the value
‫of I at each iteration.
‫Here you define the range so you can get rid of these hands by using don't show range hands I in 1 to
‫5.
‫This is a range.
‫This range represents all integers from 1 to 5 inclusive.
‫The loop iterates through the values within the specified range.
‫In this case it starts with I equal to one and goes up to five.
‫For each iteration of the loop, the println I statement is executed.
‫It prints the current value of I.
‫So in this example the loop starts with I equal to one and it increments by one in each iteration until
‫it reaches five.
‫This is a simple example of how you can use a for loop to perform a repetitive task.
‫Let's move to the while loop.
‫The while loop repeats a block of code as long as a specified condition is true.
‫For that, I'll create a variable called count equals to zero, and I'll use the while keyword to create
‫a while loop, and inside this while loop parentheses, I'll include the condition count less than five.
‫Then I'll include these curly braces.
‫Then I'll use println display for me the count and increase the count by one.
‫So we created this variable called count and initialize it to zero.
‫The while loop is initiated with the while keyword, followed by the condition enclosed in parentheses
‫count less than five.
‫The loop will continue executing as long as the condition is true.
‫As long the count is less than five.
‫Keep executing these codes inside the loop.
‫The code block is executed.
‫In this case, the code block contains two statements.
‫Print ln.
‫This line prints the current value of count, and this will increase the value of count by one.
‫After executing the code block, the program returns to the condition again to check if it is still
‫true.
‫If the condition is true, the loop continues to the next iteration and if the condition becomes false,
‫the loop terminates and the program proceeds to the next statement.
‫After the loop, let me run the program to see it.
‫This is from the for loop and this is from the while loop.
‫At first the condition was true zero is less than five, so go and execute.
‫After executing the code block, the program returns to the condition count less than five.
‫If the condition is true, the loop continues to the next iteration, and if the condition becomes false,
‫the loop terminates and the program proceeds to the next statement after the loop.
‫Here, if we run the program, we can see that the count starts from zero, one, two, three, and
‫four.
‫Go back to the loop.
‫Here we have the count starts from zero zero.
‫Less than five.
‫Yes.
‫Go and enter the loop.
‫Print.
‫The count equals to zero and this is the case.
‫Increase count by one, so count becomes one.
‫Again, the value of count becomes one.
‫One less than five.
‫Yes it's true.
‫So go and execute this code.
‫Print count value.
‫It's one, increase count by one.
‫And so on until reaching 4 or 5.
‫Five is less than five.
‫No, it's less than or equals to five.
‫Yes.
‫But it's not less than five.
‫It will terminate the while loop and execute the other codes after the loop.
‫So the while loop starts with count equals to zero, and continues to increment count by one with each
‫iteration, it stops when count reaches five, at which point the condition count less than five becomes
‫false and the loop terminates.
‫Now let's move to the do while loop.
‫The do while loop is similar to the while loop, but it guarantees that the loop body is executed at
‫least once because the condition is checked after the loop body.
‫For that, I'll create var x equals to one do keyword print.
‫I'll display this message and increase x by one.
‫And we have an error here because we need to include the while keyword x less than zero.
‫Pay attention X is one.
‫One is not less than zero.
‫It's greater than zero.
‫So this condition is false.
‫But I'll show you this block of code will be executed at least once.
‫So if we run the program.
‫Despite the condition is false.
‫The program will print this and execute this line and increase x by one.
‫So even though the condition x less than zero is false from the beginning, the code block is executed
‫once because it's a do while loop.
‫If the condition were initially true, the loop would continue executing until the condition becomes
‫false.