Professional Documents
Culture Documents
Lecture 07 W23
Lecture 07 W23
Car Truck
---------------------------------------------------------
(1 to 10).foreach(println)
Nested Loop
for (input <- 1 to 5)
{
for (innerInput <- 1 to 5)
{
println((10*input) + innerInput)
}
}
Loop with two index
for (i <- 1 to 10; j <- 1 to 3) {
println(i + " " + j)
}
Loop: while
Scala> var input =1
Scala>
while(input<5)
{
println(“value: “ + input)
Input = input+1
}
Scala> input <enter>
You see the value of input as 5
Loop: Do….while
Var b = 10
Do
{
println(“value” + b)
}
While (b <10)
➔ Will execute only once for the first time and print “value 10”, then it
will be false at the while condition
Variable with any datatype
Scala> var a = 100
Scala>
var b =
{
If (a==200)
{ 100 }
else
{ “Ahmed” }
}
Scala> mapvar
Will only see “address” → “Egypt”, “name” → “Mahmoud”
➔ The last value in the key “name” will remain
Tuple
Scala> var empTup = (1234, “John”, 200, “IT”)
➔ Collection of different types
Scala> empTup._1 ➔ see the first element
Scala> empTup_2 → 2nd element
Function
Scala>
def AddTwoNumbers(input1: Int input2: Int):Int =
{ var output = 0
output = input1+input2
output
}
object ReadFile {
def main(args: Array[String]): Unit = {
val fileName = "example.txt"
for (line <- Source.fromFile(fileName).getLines()) {
println(line)
}
}
}
Higher order functions
• functions that can
• accept other functions as arguments or
• return a function as a result.