Introduction to Scala
David Venturi
Curriculum Manager, DataCamp
if
else
while
Dancing smiley by Krabat der Zauberlehrling
// Define counter variable var i = 0
// Define the number of times for the cheer to repeat val numRepetitions = 3
// Loop to repeat the cheer while (i < numRepetitions) { // BODY OF LOOP }
// Define counter variable
var i = 0
// Define the number of times for the cheer to repeat
val numRepetitions = 3
// Loop to repeat the cheer
while (i < numRepetitions) {
println("Hip hip hooray!")
i = i + 1
}
// Define counter variable
var i = 0
// Define the number of times for the cheer to repeat
val numRepetitions = 3
// Loop to repeat the cheer
while (i < numRepetitions) {
println("Hip hip hooray!")
i += 1 // i = i + 1
}
// Define counter variable
var i = 0
// Define the number of times for the cheer to repeat
val numRepetitions = 3
// Loop to repeat the cheer
while (i < numRepetitions) {
println("Hip hip hooray!")
i += 1 // ++i and i++ don't work!
}
// Define variables for while loop
var i = 0
val numRepetitions = 3
// Loop to repeat the cheer
while (i < numRepetitions) {
println("Hip hip hooray!")
i = i + 1
}
Hip hip hooray!
// Define variables for while loop
var i = 0
val numRepetitions = 3
// Loop to repeat the cheer
while (i < numRepetitions) {
println("Hip hip hooray!")
i = i + 1
}
Hip hip hooray!
Hip hip hooray!
// Define variables for while loop
var i = 0
val numRepetitions = 3
// Loop to repeat the cheer
while (i < numRepetitions) {
println("Hip hip hooray!")
i = i + 1
}
Hip hip hooray! Hip hip hooray!
Hip hip hooray!
// Define variables for while loop
var i = 0
val numRepetitions = 3
// Loop to repeat the cheer
while (i < numRepetitions) {
println("Hip hip hooray!")
i = i + 1
}
Hip hip hooray!
Hip hip hooray!
Hip hip hooray!
// Define counter variable var i = 0
// Create an array with each player's hand var hands = Array(17, 24, 21)
// Loop through hands and see if each busts while (i < hands.length) { // BODY OF LOOP }
scala> var hands = Array(17, 24, 21)
scala> hands.length
res0: Int = 3
// Define counter variable
var i = 0
// Create an array with each player's hand
var hands = Array(17, 24, 21)
// Loop through hands and see if each busts
while (i < hands.length) {
println(bust(hands(i)))
i = i + 1
}
var i = 0
var hands = Array(17, 24, 21)
while (i < hands.length) {
println(bust(hands(i)))
i += 1
}
false
true
false
The while
loop:
while (i < hands.length)
The maxHand
function:
if (handA > handB)
The while
loop:
while i < hands.length
The maxHand
function:
if handA > handB
var i = 0
var hands = Array(17, 24, 21)
while (i < hands.length) {
println(bust(hands(i)))
i += 1
}
false
true
false
Introduction to Scala