while and the imperative style

Introduction to Scala

David Venturi

Curriculum Manager, DataCamp

Control structures

  • if else
  • while
Introduction to Scala

Hip hip hooray

Smiley face dancing

Dancing smiley by Krabat der Zauberlehrling

1 https://en.wikipedia.org/wiki/Hip_hip_hooray
Introduction to Scala

Loop with while

// 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 }
Introduction to Scala

Loop with while

// 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
}
Introduction to Scala

Loop with while

// 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
}
Introduction to Scala

Loop with while

// 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!
}
Introduction to Scala

i = 0

// 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!
Introduction to Scala

i = 1

// 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!
Introduction to Scala

i = 2

// 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!
Introduction to Scala

i = 3

// 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!
Introduction to Scala

Loop with while over a collection

// 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 }
Introduction to Scala

Scala is object-oriented

  • Rule of thumb: pretty much everything is an object in Scala
scala> var hands = Array(17, 24, 21)

scala> hands.length
res0: Int = 3
Introduction to Scala

Loop with while over a collection

// 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
}
Introduction to Scala

Loop with while over a collection

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

Like if, parentheses required for while

The while loop:

while (i < hands.length)

A green checkmark

The maxHand function:

if (handA > handB)

A green checkmark

Introduction to Scala

Like if, parentheses required for while

The while loop:

while i < hands.length

A red X

The maxHand function:

if handA > handB

A red X

Introduction to Scala

The imperative style

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

Let's practice!

Introduction to Scala

Preparing Video For Download...