Break Expression

Reading Time: 3 minutes

Suppose that you are working with loops. It is sometimes desirable to terminate the loop immediately without checking the test expression. In such case, break is used. It terminates the nearest enclosing loop when encountered (without checking the test expression).

How break works?

Almost always it is used with if..else statements. For example: If expression is evaluated to true, break is executed which terminates the for loop.

Read more

Follow and like us:

Recursion and Tail Recursion

Reading Time: 3 minutes

A function that calls itself is known as recursive function. This technique is known as recursion. A physical world example would be to place two parallel mirrors facing each other. Any object in between them would be reflected recursively.

How does recursion work in programming?

Here, the recurse() function is called from the body of recurse() function itself. Here’s how this program works.


Example

Here, the recursive call continues forever causing infinite recursion. To avoid infinite recursion, if…else (or similar approach) can be used where one branch makes the recursive call and other doesn’t.

Read more

Follow and like us:

Continue Expression

Reading Time: 3 minutes

Suppose you are working with loops. It is sometimes desirable to skip the current iteration of the loop. In such case, continue is used. The continue construct skips the current iteration of the enclosing loop, and the control of the program jumps to the end of the loop body.

How continue works?

Almost always it is used with if..else statements. For example: If the expression is evaluated to true, continue is executed which skips all the codes inside while loop after it for that iteration.

Read more

Follow and like us:

Data Class

Reading Time: 4 minutes

While building any application, we often need to create classes whose primary purpose is to hold data/state. These classes generally contain the same old boilerplate code in the form of getters, setters, equals(), hashcode() and toString() methods.

Kotlin has a better solution for classes that are used to hold data/state. It’s called a Data Class. A Data Class is like a regular class but with some additional functionalities.

With Kotlin’s data classes, you don’t need to write/generate all the lengthy boilerplate code yourself. The compiler automatically generates a default getter and setter for all the mutable properties, and a getter (only) for all the read-only properties of the data class. Moreover, It also derives the implementation of standard methods like equals(), hashCode() and toString() from the properties declared in the data class’s primary constructor.

Example Customer data class


Syntax of data class

Read more

Follow and like us:

Getters and Setters

Reading Time: 3 minutes

Getters and Setters are used to effectively protect your data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value. Getters and setters are also known as accessors and mutators, respectively.

Accessing properties directly is not a good practice.

Sometimes we need to check the value in case of an invalid input, or do some other data validation or calculation before assigning or accessing the property value.

Read more

Follow and like us:

stay informed!

Subscribe to receive exclusive content and notifications