if-else statements

if-else statements work very much like the if-else statement you are used to from Java, with a couple of minor differences.

The test in a Swift if-else statement does not require parentheses:

val admission = 10.5
if age <= 12 {
  admission = 7.5
}

if-else expressions

In Java if-else is always a statement. In both Kotlin and Swift if-else can also be an expression that returns a value.

let admission = if age <= 12 { 7.5 } else { 10.5 }

loops

Neither Kotlin nor Swift offer the traditional for loop. Instead, both languages use the for-in loop.

let numbers = [1,2,3,4,5]
var sum = 0
for number in numbers {
  sum += number
}
println(sum)

If you want something closer to the traditional for loop, you can iterate over a range expression.

let numbers = [1,2,3,4,5]
var sum = 0
for n in 0..(numbers.size-1) {
  sum += numbers[n]
}
println(sum)

Both languages offer while loops that are very similar to while loops in Java.