if Expressions

There are some useful videos on YouTube created by Peter Somerhoff and it is well worth subscribing to his channel. In particular, there are a couple of tutorial playlists of Lotlin tutorials.

	Kotlin Tutorial - 22 videos.
	Kotlin for Android and Java Developers - 12 videos.

These notes are from video 5 in the Kotlin for Android and Java Developers playlist.

The Android Studio provides a facility that may be quite useful when developing code and that is the Kotlin REPL (REPL stands for Read-Eval-Print-Loop). This can be accessed via the Tools menu by selecting first Kotlin and then Kotlin REPL and this opens up a small integrated command line at the bottom of the screen. It should have a tab labelled something like Kotlin REPL (in module app) where app will be the name of your project and you may have to select it.

Here, you can type in Kotlin code a line at a time and it is interpreted in much the same way as Python would be if using a command line interface or IDLE in interpreter mode.

Another interesting point is really expanding on a point made in the section here entitled “Evaluate Conditions With If and Else” which mentions the fact that you can combine an assignment expression with an if statement. The video takes this a little further and notes that for any block of code in an if/if else/else block, the last statement returns the value for the assignment and this can be a simple expression such as a string or a variable name.

	val i=17
	val x=if (i < 15) {
		println("i is pretty small")
		"small"
	} else if (i >=15 && i <= 25) {
		println("it's okay")
		"medium"
	} else {
		println("it's pretty large")
		"large"
	}

The result of executing this code is that the string “it’s okay” is printed. We don’t print the value of x here but we can print this after the code has executed and we can see that the value “medium” has been assigned to it.