0% found this document useful (0 votes)
16 views

7 Wloop

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

7 Wloop

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Kotlin while loop

While loop
Kotlin do-while loop

Ranjit Patnaik Kotlin While Loop 1 / 12


Kotlin while loop
While loop
Kotlin do-while loop

Content

1 Kotlin while loop

2 While loop

3 Kotlin do-while loop

Ranjit Patnaik Kotlin While Loop 2 / 12


Kotlin while loop
While loop
Kotlin do-while loop

Kotlin while loop

In programming, loop is used to execute a specic block of code repeatedly


until certain condition is met. If you have to print counting from 1 to 100
then you have to write the print statement 100 times. But with help of
loop you can save time and you need to write only two lines.

Ranjit Patnaik Kotlin While Loop 3 / 12


Kotlin while loop
While loop
Kotlin do-while loop

While loop

It consists of a block of code and a condition. First of all the condition is


evaluated and if it is true then execute the code within the block. It
repeats until the condition becomes false because every time the condition
is checked before entering into the block. The while loop can be thought of
as repeating of if statements.
The syntax of while loop-

while(condition) {
// code to run
}

Ranjit Patnaik Kotlin While Loop 4 / 12


Kotlin while loop
While loop
Kotlin do-while loop

Continue....

Kotlin program to print numbers


from 1 to 10 using while loop: In
below program, we print the numbers
using while loop. First, initialize the
variable number by 1. Put the
expression (number <= 10) in while
loop and checks is it true or not?. If
true, enters in the block and execute
the print statement and increment
the number by 1. This repeats until
the condition becomes false.

Ranjit Patnaik Kotlin While Loop 5 / 12


Kotlin while loop
While loop
Kotlin do-while loop

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

Ranjit Patnaik Kotlin While Loop 6 / 12


Kotlin while loop
While loop
Kotlin do-while loop

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.

Ranjit Patnaik Kotlin While Loop 7 / 12


Kotlin while loop
While loop
Kotlin do-while loop

Continue....

fun main(args: Array<String>) {


var names =
Output:
,→ arrayOf("Praveen","Gaurav","Akash","Sid ⌋ Praveen
,→ hant","Abhi","Mayank") Gaurav
var index = 0
while(index < names.size) { Akash
println(names[index]) Sidhant
index++
}
Abhi
} Mayank

Ranjit Patnaik Kotlin While Loop 8 / 12


Kotlin while loop
While loop
Kotlin do-while loop

Kotlin do-while loop

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.

Ranjit Patnaik Kotlin While Loop 9 / 12


Kotlin while loop
While loop
Kotlin 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)
}

Ranjit Patnaik Kotlin While Loop 10 / 12


Kotlin while loop
While loop
Kotlin do-while loop

Continue....

Ranjit Patnaik Kotlin While Loop 11 / 12


Kotlin while loop
While loop
Kotlin do-while loop

Continue....

Kotlin program to nd the factorial of a number using do-while loop -


fun main(args: Array<String>) {
var number = 6
var factorial = 1

do {
factorial *= number Output:
number-- Factorial of 6 is 720
}while(number > 0)
println("Factorial of 6 is
,→ $factorial")
}

Ranjit Patnaik Kotlin While Loop 12 / 12

You might also like