Press enter to see results or esc to cancel.

Lesson 13: Do / while Looping Statement in kotlin Language

Share this
Table of Contents
      1. Code Explanation

The do / while Looping structure is another looping structure, which executes its repetitive block at least once, unlike the while that might not execute the block.
This repetitive structure is used when we know in advance that at least once the repetitive block will be executed.
The condition of the structure is below the block to be repeated, unlike the while that is at the top.

Graphic representation:

Lesson 13: Do / while Looping Statement in kotlin Language - Lesson 13: Do / while Looping Statement in kotlin Language - Lesson 13: Do / while Looping Statement in kotlin Language - Do / while Looping Statement in kotlin Programming Language
Do / while Looping

The operations block is repeated WHILE the condition is True.
If the condition returns False, the cycle stops.

It is important to analyze and see that operations are executed at least once.

Problem 1

Write a program that requests the loading of a number between 0 and 999, and shows us a message of how many digits it has. End the program when the value 0 is loaded.

Project42 – Principal.kt

fun main (parameter: Array <String>) {
     do { // simple do / while looping example
         print ("Enter a value between 0 and 999:")
         val value = readLine () !!. toInt ()
         if (value < 10)
             println ("The value entered has a digit")
             if (value <100)
                 println ("The entered value has two digits")
                 println ("The entered value has three digits")
     } while (value != 0)

In this problem at least a value is loaded. If a value less than 10 is entered it is a number of a number, if it is less than 100 it is a two-digit value, otherwise it is a three-digit value (it is not checked that the operator loads Negative values or values with more than three digits). This block is repeated until it is entered in the value variable the number 0 with which the condition of the while structure returns false and leaves the repetitive block ending the program.

Problem 2

Write a program that requests the loading of numbers by keyboard, get your average. End value loading when 0 is loaded.

When the completion depends on some value entered by the operator, it is advisable to use the do / while looping structure, at least one value will be loaded (in the most extreme case, 0 is loaded, indicating the completion of the value loading)

Project43 – coding180project.kt

fun main (parameter: Array <String>) {
     var cant = 0
     var sum = 0
     do {
         print ("Enter a value (0 to end):")
         val value = readLine () !!. toInt ()
         if (value != 0) {
             sum += value
     } while (value != 0)
     if (cant != 0) {
        val average = sum / cant
         print ("The average of the values ​​entered is: $average")
     } else
         print ("No values ​​entered.")

Code Explanation

The most common way to increase an Int variable is to use the ++ operator:


It is the same as writing:

             Cant = cant + 1

There is also the operator – which decreases the variable by one.

Also in Kotlin the + = operator is used for the accumulation:

             Sum + = value

Instead of writing:

             Sum = sum + value

In problem 2 every time you enter by an if we check if it is different from zero to accumulate it and count it:

         If (value != 0) {
             Sum += value

The cycle do / while is repeated while entering a value other than zero, when we enter the zero ends the cycle and by an if we verify if at least one information value was loaded to obtain the average:

     if (cant != 0) {
         Average val = sum / cant
         Print ("The average of the values ​​entered is: $average")
     } else
         print ("No values ​​entered.")

Problem 3

Carry out a program that allows you to enter the weight (in kilograms) of pieces. The process ends when we enter the value 0.
It should be reported:

A) How many pieces have a weight between 9.8 kg and 10.2 kg?, How many with more than 10.2 kg? And how many less than 9.8 kg?

B) The total number of pieces processed.

Project44 – Principal.kt

fun main (parameter: Array <String>) {
     var cont1 = 0
     var cont2 = 0
     var cont3 = 0
     do {
         print ("Enter part weight (0 to finish):")
         val weight = readLine () !!. toDouble ()
         if (weight> 10.2)
             cont1 ++
             if (weight >= 9.8)
                 cont2 ++
                 if (weight> 0)
                     cont3 ++
     } while (weight != 0.0)
     println ("Fit parts: $cont2")
     println ("Parts greater than 10.2: $ cont1")
     println ("Parts weighing less than 9.8: $cont3");
     val sum = cont1 + cont2 + cont3
     println ("Total quantity of pieces processed: $sum")

The three counters cont1, cont2, and cont3 are initialized to 0 before entering the repeating structure.

The variable sum is not initialized to 0 because it is not an accumulator, but saves the sum of the contents of the variables cont1, cont2 and cont3.

The structure is repeated until the value 0 is entered in the weight variable. This value is not considered a weight less than 9.8 kg, but indicates that the load of values has been completed by keyboard.

We must in the condition of the do / while looping compare the contents of the weight variable that is of type Double with the value 0.0 (this way we indicate that the value is Double, we can not put only 0)

Problems proposed

  • Perform a program that accumulates (sum) values entered by keyboard until entering 9999 (not to add this value, indicates that the load has finished). Print the accumulated value and inform if the value is zero, greater than zero or less than zero.
  • In a bank, data are processed from your clients’ current accounts. From each current account is known: account number and current balance. Data entry must end when a negative value is entered in the account number.
    We ask you to create a program that reads the current account data and report:
    A) From each account: account number and account status according to your balance, knowing that:

     Account status 'Creditor' if the balance is > 0.
    			 'Debtor' if the balance is < 0.
    			 'Null' if the balance is = 0.

    B) The total sum of the credit balances.



 fun main (parameter: Array <String>) {
     var sum = 0
     do {
         print ("Enter a value (end with 9999):")
         val value = readLine () !!. toInt ()
         if (value == 9999)
             sum += value
     } while (value != 9999)
     println ("Cumulative value is $sum")
     if (sum == 0)
         println ("Cumulative value is zero.")
         if (sum> 0)
             println ("Cumulative value is positive.")
             println ("Cumulative value is negative")

fun main (parameter: Array <String>) {
    var sum = 0.0
    do {
        print ("Enter account number:")
        val count = readLine () !!. toInt ()
        if (count >= 0) {
        print ("Enter balance:")
        val balance = readLine () !!. toDouble ()
        if (balance> 0) {
            println ("Creditor Balance.")
            sum += balance
        } else
                if (balance <0)
        println ("Debtor Balance.")
        println ("Null Balance.")
    } while (count >= 0)
    println ("Total creditors balances: $sum")



How useful was this Lesson?

Click on a star to rate it!

Average rating / 5. Vote count:

Be the first to rate this post.!

As you found this post useful...

Follow us on social media!

We are sorry that this lesson was not useful for you!

Let us improve this lesson!

Tell us how we can improve this lesson?

Share this