7 Wloop
7 Wloop
While loop
Kotlin do-while loop
Content
2 While loop
While loop
while(condition) {
// code to run
}
Continue....
Continue....
Output:
1
fun main(args: Array<String>) { 2
var number = 1 3
4
while(number <= 10) {
println(number) 5
number++; 6
}
} 7
8
9
10
Continue....
Kotlin program to print the elements of an array using while loop: In the
below program we create an array(names) and initialize with dierent
number of strings and also initialize a variable index by 0. The size of an
array can be calculated by using arrayName.size. Put the condition (index
⟨ names.size) in the while loop. If index value less than or equal to array
size then it enters into the block and print the name stored at the
respective index and also increment the index value after each iteration.
This repeats until the condition becomes false.
Continue....
Like Java, do-while loop is a control ow statement which executes a block
of code at least once without checking the condition, and then repeatedly
executes the block, or not, it totally depends upon a Boolean condition at
the end of do-while block. It contrast with the while loop because while
loop executes the block only when condition becomes true but do-while
loop executes the code rst and then the expression or test condition is
evaluated.
do-while loop working
First of the all the statements within the block is executed, and then the
condition is evaluated. If the condition is true the block of code is executed
again. The process of execution of code block repeated as long as the
expression evaluates to true. If the expression becomes false, the loop
terminates and transfers control to the statement next to do-while loop.
Continue....
It is also known as post-test loop because it checks the condition after the
block is executed.
Syntax of the do-while loop-
do {
// code to run
{
while(condition)
}
Continue....
Continue....
do {
factorial *= number Output:
number-- Factorial of 6 is 720
}while(number > 0)
println("Factorial of 6 is
,→ $factorial")
}