Make decisions with if and else

Introduction to Scala

David Venturi

Curriculum Manager, DataCamp

A program for playing Twenty-One

Variables
val fourHearts: Int = 4
var aceClubs: Int = 1
Collections
val hands: Array[Int] = new Array[Int](3)`
Functions
// Define a function to determine if hand busts
def bust(hand: Int) = {
  hand > 21
}
Introduction to Scala

Control structures

A control structure is a block of programming that analyses variables and chooses a direction in which to go based on given parameters. The term flow control details the direction the program takes (which way program control "flows").

  • if/else
1 https://en.wikiversity.org/wiki/Control_structures
Introduction to Scala

A single if

// This hand's point value
val hand = 24

// If this hand busts, print to output if (hand > 21) { println("This hand busts!") }
This hand busts!
Introduction to Scala

A single if

// This hand's point value
val hand = 18

// If this hand busts, print to output
if (hand > 21) {
  println("This hand busts!")
}

Introduction to Scala

if-else control flow

def maxHand(handA: Int, handB: Int): Int = {
  if (handA > handB) handA
  else handB
}
Introduction to Scala

if-else control flow

// Point values for two competing hands
val handA = 17
val handB = 19

// Print the value of the hand with the most points
if (handA > handB) println(handA)
else println(handB)
Introduction to Scala

if-else control flow

// Point values for two competing hands
val handA = 17
val handB = 19

// Print the value of the hand with the most points
if (handA > handB) {
  println(handA)
}
else {
  println(handB)
}
Introduction to Scala

if-else control flow

// Point values for two competing hands
val handA = 17
val handB = 19

// Print the value of the hand with the most points
if (handA > handB)
  println(handA)
else
  println(handB)
Introduction to Scala

if-else control flow

// Point values for two competing hands
val handA = 17
val handB = 19

// Print the value of the hand with the most points
if (handA > handB) println(handA)
else println(handB)
19
Introduction to Scala

if-else control flow

// Point values for two competing hands
val handA = 17
val handB = 19

// Print the value of the hand with the most points
if (handA > handB) println(handA) else println(handB)
19
Introduction to Scala

if-else if-else

// Point values for two competing hands
val handA = 26
val handB = 20

// If both hands bust, neither wins if (bust(handA) & bust(handB)) println(0)
// If hand A busts, hand B wins else if (bust(handA)) println(handB)
// If hand B busts, hand A wins else if (bust(handB)) println(handA)
// If hand A is greater than hand B, hand A wins else if (handA > handB) println(handA)
// Hand B wins otherwise else println(handB)
Introduction to Scala

if-else if-else

// Point values for two competing hands
val handA = 26
val handB = 20

// Find and print the best hand
if (bust(handA) & bust(handB)) println(0)
else if (bust(handA)) println(handB)
else if (bust(handB)) println(handA)
else if (handA > handB) println(handA)
else println(handB)
20
Introduction to Scala

if expressions result in a value

scala> val handA = 17
handA: Int = 17
scala> val handB = 19
handB: Int = 19
scala> val maxHand = if (handA > handB) handA else handB
maxHand: Int = 19
Introduction to Scala

Relational and logical operators

Relational

Greater than: >

Less than: <

Greater than or equal to: >=

Less than or equal to: <=

Equal to: ==

Not equal to: !=

Logical

And: &&

Or: ||

Not: !

Introduction to Scala

These "operators" are actually methods!

Relational

Greater than: >

Less than: <

Greater than or equal to: >=

Less than or equal to: <=

Equal to: ==

Not equal to: !=

Logical

And: &&

Or: ||

Not: !

Introduction to Scala

These "operators" are actually methods!

Relational

Greater than: >

Less than: <

Greater than or equal to: >=

Less than or equal to: <=

Equal to: ==

Not equal to: !=

Logical

And: &&

Or: ||

Not: !

1 "Future music" is an oft-used phrase within DataCamp. It means "something you want or aspire to have but not for the near future" and is of Dutch origin.
Introduction to Scala

Let's practice!

Introduction to Scala

Preparing Video For Download...