Skip to main content

Posts

Kotlin Continue Jump Structure

Kotlin continue Jump Structure Kotlin, continue statement is used to repeat the loop. It continues the current flow of the program and skips the remaining code at specified condition. The continue statement within a nested loop only affects the inner loop. For example for(..){     //body of for above if     if(checkCondition){         continue     }     //body of for below if } In the above example, for loop repeat its loop when if condition execute continue. The continue statement makes repetition of loop without executing the below code of if condition. Kotlin continue example fun main(args: Array ) {     for (i in 1..3) {         println("i = $i")         if (j == 2) {         continue     }           println("this is below if")     } } Output: i = 1 this is below if i = 2 i = 3 this is below if

Kotlin Return and Jump

Kotlin Return and Jump There are three jump expressions in Kotlin. These jump expressions are used for control the flow of program execution. These jump structures are: break continue return Break Expression A break expression is used for terminate the nearest enclosing loop. It is almost used with if-else condition. For example: for(..){     //body of for     if(checkCondition){         break;     } } In the above example, for loop terminates its loop when if condition execute break expression. Kotlin break example: fun main(args: Array ) {     for (i in 1..5) {         if (i == 3) {             break         }     println(i)     } } Output: 1 2 In the above example, when the value of i became equal to 3 and satisfy the if condition(i==3) than the break expression execute and terminate for loop. Kotlin Labeled break Expression Labeled is the form of ident

Kotlin Do-While Loop

Kotlin do-while Loop The do-while loop is similar to while loop except one key difference. A do-while loop first execute the body of do block after that it check the condition of while. As a do block of do-while loop executed first before checking the condition, do-while loop execute at least once even the condition within while is false. The while statement of do-while loop end with ";" (semicolon). Syntax do{      //body of do block } while(condition); Example of do -while loop Let's see a simple example of do-while loop printing value 1 to 5. fun main(args: Array ){      var i = 1      do {           println(i)           i++      }      while (i<=5); }  Output: 1 2 3 4 5 Example of do -while loop even condition of while if false In this example do-while loop execute at once time even the condition of while is false. fun main(args: Array ){      var i = 6      do {  

Kotlin While Loop

Kotlin while Loop The while loop is used to iterate a part of program several time. Loop executed the block of code until the condition has true. Kotlin while loop is similar to Java while loop. Syntax while(condition){     //body of loop }  Example of while Loop Let's see a simple example of while loop printing value from 1 to 5.  fun main(args: Array ){     var i = 1     while (i<=5){          println(i)          i++          } } Output: 1 2 3 4 5 Kotlin Infinite while Loop The while loop executes a block of code to infinite times, if while condition remain true.  For example:     fun main(args: Array ){          while (true){                println("infinite loop")           }       } Output: infinite loop infinite loop infinite loop . . . . infinite loop infinite loop infinite loop infinite loop infinite loop infinite loop

Kotlin For Loop

Kotlin for Loop Kotlin for loop is used to iterate a part of program several times. It iterates through arrays, ranges, collections, or anything that provides for iterate. Kotlin for loop is equivalent to the foreach loop in languages like C#. Syntax of for loop in Kotlin:   for (item in collection){     //body of loop     }       Iterate through array Let's see a simple example of iterating the elements of array.          fun main(args : Array ) {          val marks = arrayOf(80,85,60,90,70)          for(item in marks){                    println(item)          }       } Output: 80 85 60 90 70 If the body of for loop contains only one single line of statement, it is not necessary to enclose within curly braces {}. fun main(args : Array ) {     val marks = arrayOf(80,85,60,90,70)           for(item in marks)               println(item)     } The elements of an array are iterated on the basis of i

Kotlin When Expression

Kotlin when Expression Kotlin, when expression is a conditional expression which returns the value. Kotlin, when expression is replacement of switch statement. Kotlin, when expression works as a switch statement of other language (Java, C++, C). Using when as an Expression Let's see a simple example of when expression. fun main(args: Array ){ var number = 4 var numberProvided = when(number) {     1 -> "One"     2 -> "Two"     3 -> "Three"     4 -> "Four"     5 -> "Five"     else -> "invalid number"     }           println("You provide $numberProvided")         }  Output: You provide Four Using when Without Expression It is not mandatory to use when as an expression, it can be used as normally as it used in other language. For Example fun main(args: Array ){     var number = 4     when(

Kotlin if Expression

Kotlin if Expression In Kotlin, if is an expression is which returns a value. It is used for control the flow of program structure. There is various type of if expression in Kotlin. if-else expression if-else if-else ladder expression nested if expression Traditional if Statement Syntax of traditional if statement if(condation){      //code statement } Syntax of traditional if else statement if(condation){      //code statement } else{      //code statement } Kotlin if-else Expression As if is an expression it is not used as standalone, it is used with if-else expression and the result of an if-else expression is assign into a variable.   Syntax of if-else expression val returnValue = if (condation) {      //code statement } else {      // code statement } println(returnValue) Kotlin if-else Expression Example Let's see an example of if-else expression. fun main(args: Array ) {      val num1 = 1