Introduction to Scala
David Venturi
Curriculum Manager, DataCamp
val fourHearts: Int = 4
var aceClubs: Int = 1
val hands: Array[Int] = new Array[Int](3)`
// Define a function to determine if hand busts
def bust(hand: Int) = {
hand > 21
}
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
// 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!
// This hand's point value
val hand = 18
// If this hand busts, print to output
if (hand > 21) {
println("This hand busts!")
}
def maxHand(handA: Int, handB: Int): Int = {
if (handA > handB) handA
else handB
}
// 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)
// 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)
}
// 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)
// 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
// 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
// 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)
// 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
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
Greater than: >
Less than: <
Greater than or equal to: >=
Less than or equal to: <=
Equal to: ==
Not equal to: !=
And: &&
Or: ||
Not: !
Greater than: >
Less than: <
Greater than or equal to: >=
Less than or equal to: <=
Equal to: ==
Not equal to: !=
And: &&
Or: ||
Not: !
Greater than: >
Less than: <
Greater than or equal to: >=
Less than or equal to: <=
Equal to: ==
Not equal to: !=
And: &&
Or: ||
Not: !
Introduction to Scala