Kotlin tutorial for Beginners – Kotlin Expression, Statements and Blocks

Kotlin Expression, Statements and Blocks

In this article, you will learn about Kotlin expressions, Kotlin statements, difference between expression and statement, and Kotlin blocks.

Kotlin Expressions

Expressions consist of variables, operators etc that evaluates to a single value.

Let’s take an example,

val score: Int
score = 90 + 25

Here, 90 + 25 is an expression that returns Int value.


In Kotlin, if is an expression unlike Java (In Java, if is a statement). For example,


fun main(args: Array<String>) {

    val a = 12
    val b = 13
    val max: Int

    max = if (a > b) a else b
    println("$max")
}

Here, if (a > b) a else b is an expression. Then value of the expression is assigned to max variable in the above program. Visit this page to learn more about Kotlin if expression.


Kotlin Statements

Statements are everything that make up a complete unit of execution. For example,

val score = 90 + 25

Here, 90 + 25 is an expression that returns 115, and val score = 9*5; is a statement.

Expressions are part of statements.

Some examples:

println("Howdy")
var a = 5
++a
max = if (a > b) a else b

Kotlin Blocks

A block is a group of statements (zero or more) that is enclosed in curly braces { }. For example,


fun main(args: Array<String>) {  // main function block
    val flag = true

    if (flag == true) {      // start of if block
        print("Hey ")
        print("jude!")
    }                        // end of if block
}                            // end of main function block

There are two statements print("Hey ") and print(" jude!") inside if branch block.

print("Hey ")
print("jude!")

Similarly, the main() function also has a block body.

val flag = true

if (flag == true) {      // start of block
    print("Hey ")
    print("jude!")
}                        // end of block

 

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.