In this part, let us discuss the Control Flow of Kotlin programming language.
If-Else Expression
It is used for conditional branching of the statements. The first branch will execute when a condition is true, otherwise, the statements of the second branch will execute.
- if (Condition) {
- //First branch statements
- } else {
- //First branch statements
- }
Syntax
- fun main(args: Array < String > ) {
- var n: Int = 5
- if (n % 2 == 0) {
- println("Even number.")
- } else {
- print("odd number.")
- }
- }
- //output is - odd number.
Example
Kotlin program to check if an input number is even or odd.
Nested If-else Expression
If within "If" is known as "nested if" expression.
- If(condition) {
- If(condition) {} else {}
- } else {}
Syntax
- fun main(args: Array < String > ) {
- var age: Int = 20
- var marks: Float = 65.50 f
- if (age in 20. .30) { //if(age>=20 && age<=30)
- if (marks >= 55.00 f) {
- println("Valid Candidate")
- } else {
- println("aggregate marks should greater than or equal to 55%")
- }
- } else {
- println("Age is over")
- }
- //output : Valid Candidate
- }
Example
Write a Kotlin program to check if the candidate is eligible for Bank PO exam or not (where the age limit is 20-30 and minimum aggregate marks in graduation should be 55%).
If-else ladder
Example
Write a program to find the biggest among 3 numbers.
- fun main(args: Array < String > ) {
- var a: Int = 5
- var b: Int = 15
- var c: Int = 3
- if (a > b && a > c) {
- println("a is biggest")
- } else if (b > c) {
- println("b is biggest")
- } else {
- println("c is biggest")
- }
- }
- //output : b is biggest
- If(condition) { //statements
- } else if (condition) { //statement
- }
- else {
- //statement
- }
When Expression
It is used to execute multiple conditional statements of If-Else in a single place. It is a replacement for the Switch Expression used in C, C++, and Java programming languages.
Syntax
- When(value) { < case1 > - > < statement > < case2 > - > < statement > < case3 > - > < statement >
- else - > < statement >
- }
Example
Write a Kotlin program to print a person's favorite color.
- fun main(args: Array < String > ) {
- var colorcode: Int = 2
- when(colorcode) {
- 1 - > println("your favorite color is RED")
- 2 - > println("your favorite color is GREEN")
- 3 - > println("your favorite color is YELLOW")
- else - > println("please choose a valid no. from 1-3")
- }
- }
- //output : your favorite color is GREEN
While loop
It first of all checks the given condition. If the condition is true, then it executes the body statements otherwise, exits from the loop.
Syntax
- While(condition) {
- //body statement
- }
Write a program to print Natural Numbers.
- fun main(args: Array < String > ) {
- var naturalnum: Int = 1
- while (naturalnum <= 9) {
- print(" " + naturalnum)
- naturalnum++
- }
- }
- //output: 1 2 3 4 5 6 7 8 9
Do While loop
It first of all executes the body statement once, then checks the condition of the While loop to execute the statement for next time.
Syntax
- do {
- //body statement
- } while (condition)
Write a program to print the table of 2.
- fun main(args: Array < String > ) {
- var table: Int = 2
- do {
- print(" " + table)
- table = table + 2
- } while (table <= 20)
- }
- //Output: 2 4 6 8 10 12 14 16 18 20
For Each loop
It is used to fetch the data one by one from a collection. The loop will execute until whole elements of the collection are processed or the loop is terminated.
Syntax
- for ( < variablename > in collection) {
- //body statement
- }
Example
Write a program to print the days of a week.
- import com.sun.xml.internal.fastinfoset.util.StringArray
- fun main(args: Array < String > ) {
- var week = arrayOf("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
- for (day in week) {
- print(" " + day)
- }
- } //output: Monday Tuesday Wednesday Thursday Friday Saturday Sunday
Return and Jump
There are three structural Jump expressions in Kotlin.
- return. By default, it returns value from the nearest enclosing function or anonymous function.
- break. Terminates the nearest enclosing loop.
- continue. Proceeds to the next step of the nearest enclosing loop.
return Expression
val s = person.name ?: return
The type of these expressions is the Nothing type.
- You can use any expression or identifier to create a label.To create the label, you have to use @ notation after the label name, i.e., label@ and you can use @label to go to this label. Let's see with an example.
Return to label- fun myfun() {
- ints.forEach label @ {
- if (it == 0) return @label
- print(it)
- }
- }
Break and Continue
Continue proceeds the control for next execution of the loop while Break exits the control from the body of the loop.
- Loop(condition) { //body
- If(condition) {
- //statement
- continue
- } else {
- break
- }
- }
Example
Write a program to check if a number is a prime number or not. (Prime numbers are those number which can be divided only by 1 or itself).
- fun main(args:Array<String>){
- var num:Int=7
- var i:Int=2
- var count:Int=0
- while(i<num){
- if(num%i==0)
- {
- count=1
- println("Not prime")
- break
- }else
- {
- i++
- continue
- }
- }
- if(count==0)
- {
- println("Prime")
- }
- }

Join the conversation! Your thoughts help the community grow.