Kotlin forEach Loop

Before we jump on for each that is our topic let’s first discuss the for each loop in java.

For each loop in Java would like to be:

class For_Each  	
{
    public static void main(String[] arg)
    {
        {
            int[] marks = { 12, 32, 97, 126, 130 };
              
            int highest_marks = maximum(marks);
            System.out.println("The highest score is " + highest_marks);
        }
    }
    public static int maximum(int[] numbers)
    { 
        int maxSoFar = numbers[0];
          
        // for each loop
        for (int num : numbers) 
        {
            if (num > maxSoFar)
            {
                maxSoFar = num;
            }
        }
    return maxSoFar;
    }
}

Output:

Read more

Kotlin return Statement

We have often encountered scenarios where we want to use a function to return a value, like say a function to return a Boolean if passed String contains special characters, this is what exactly we will try to understand in this tutorial. Kotlin is a simple way of doing it by using return statement.

Read more

Kotlin continue Statement

Jump statements are evident to us from the last topic in which we discovered the use of break condition which can be used with or without labels. This topic can be taken as an extension of the same. Break is a statement which is used to end or terminate the flow of control once it enters loop, but what if we want to keep on executing the flow but after skipping the current flow. Let’s take an example, for all students who are failing we have to grace marks if difference is less than 3, we don’t need to perform the calculation if the student is pass. Let’s have a look at syntax of continue to understand its working.

Read more

Kotlin break Statement

We have seen the usage of Flow of Control which allows us to quickly move apply conditions, now in this tutorial we will encounter the scenarios to be catered when the flow needs to be broken or redirected.

The way kotlin has given us some altered and improved for loop and a modern day switch which have reduced the developer’s effort, now let’s have look on topic without which no conditional flow can ever be truly complete, these are the break and continue.

Read more

Kotlin while and do while Loop

Before we discuss loops, let’s go back in time and remember those days when we used to be punished and teachers told us to write: “I will not repeat the same mistake” a 100 times. Understanding that pain and easing that effort programming languages came up with repeated conditional statements which ease the programming effort by simply reducing effort.

Read more

Kotlin for Loop

Before we jump on see what Kotlin has to offer in terms of for loop, let’s first recap the one we have used in Java.

A typical Java for loop would like below:

for (int i = 0;i<10;i++){
	System.out.println("Number is " + i);
}

Read more

Kotlin when Expression

If you are familiar with Java, then no course in Java is possible without flow of controls in which the switch forms a very important lesson. A typical switch looks like below:

switch(weekNumber){
       case 1:System.out.println(" MONDAY ");        break;
       case 2:System.out.println(" TUESDAY ");       break;
       case 3:System.out.println(" WEDNESDAY ");     break;
       case 4:System.out.println(" THRUSDAY ");      break;
       case 5:System.out.println(" FRIDAY ");        break;
       case 6:System.out.println(" WEEKEND ");       break;
       case 7:System.out.println(" WEEKEND ");       break;
       default:System.out.println(" NOT POSSIBLE "); break;
}

Read more

Kotlin if else Expression

In this tutorial you will learn about kotlin if, else and nested if else expression.

The if expression is definitely one of those constructs which lets us simplify our task of determining conditions during a program flow. Or rather, for performing any decisions in the program we look up to the if statement first, before considering any other because of our sheer familiarity with the statement and efficiency of the same. The latter part however, by and large depends on our problem statement. The if statement hence, is an integral part of any programming language.

Read more