while and do-while loops execute their body continuously while their condition is satisfied. The difference between them is the condition checking time:
whilechecks the condition and,if it's satisfied, executes the body and then returns to the condition check.
do-while executes the body and then checks the condition. If it's satisfied, the loop repeats. So, the body of do-while executes at least once regardless of the condition.
x = 5
// it will run loop 5 times
while( X > 0){
x--
}
While loop Example
do{
val y = retriveData()
}while( y != null)
// y is visible here
Break and Continue in loops
Kotlin supports traditional break and continue operators in loops.
break - is used stop loop iteration and exit from loop
continue - is used to skip one iteration of loop and goes to next iteration