You are currently viewing Beginner here. Trying to understand { } Curly Braces placement in Kotlin.

Beginner here. Trying to understand { } Curly Braces placement in Kotlin.

I am just learning Kotlin ( and code for that matter) and going through the Kotlin course on Code Academy. I am trying to understand where placement of the {} needs to go in the code. I know I need one after fun main() and at the very end of the code. The trouble I am having is where or when to place the {} when I am writing other stuff into the body of the code.

Is there an easy way to explain how or when I need to add { }.

p.s. Sorry I don’t know how to paste the “gray box” with a code example in reddit that I often see here in this sub.

Below is correct (from Code Academy)

fun main() {
var orbitsStar = true // Rule 1
var hydrostaticEquilibrium = true // Rule 2
var clearedOrbit = false // Rule 3
if (orbitsStar && hydrostaticEquilibrium) {
if (clearedOrbit) {
println(“Celestial body is a planet.”)
} else {
println(“Celestial body is a dwarf planet.”)
}
}
}

submitted by /u/Nerd-Rule
[link] [comments]