An If expression:
If expression is defined: In Scala, the IF expression is a value, which is the value returned by the If live in the last line of else.
For example:
Scala> val Age =20age:int = 20scala> if (age>18) 1 Else 0res2:int = 1scala> val Isadult = if (age >18) 1 El Se 0isadult:int = 1
Another way of writing
Scala> val Age =20age:int = 20scala> if (age>18) 1 Else 0res2:int = 1scala> val Isadult = if (age >18) 1 El Se 0isadult:int = 1scala> if (age >18) Isadult =1 else isadult =0scala> isadultres2:int = 1
Type inference for an If expression:
Because if expressions are valued, and if and else clauses may have different value types, what is the value of the IF expression? Scala automatically infers, taking two types of public parent types
For example:
Scala> val age = 20;age:int = 20scala> if (age>18) 1 Else 0res3:int = 1scala> if (age >18) "adult" Else 0r Es4:any = Adult
If and else values are string and int respectively, the value of the expression is Any,any is the public parent type of string and int
If the if is not followed by else, the value of the default else is unit, also denoted by (), similar to void in Java or null.
For example, Val age = 12; if (age >) "adult". This is equivalent to if (age >) "adult" else ().
Place the IF statement in more than one line: By default, REPL can only interpret a single line of statements, but an if expression usually needs to be placed on multiple lines.
You can use the {} method, such as the following, or using: Paste and ctrl+d.
Statement terminator, block-expression
By default, Scala does not require statement terminator, and each line is defaulted as a statement
• Put multiple statements on one line: If you want to put more than one statement in a row, you must use the statement terminator
• For example, use a semicolon as the statement Terminator, var A, b, c = 0; if (a <) {B = b + 1; c = c + 1}
• In general, curly braces are used for multiline statements
if (a < 10) {
b = B + 1
c = C + 1
}
Block expression: A block expression that refers to the value in {}, which can contain multiple statements, and the value of the last statement is the return value of the block expression.
For example:
scala> var A, b, c = 0; if (a <) {B = b + 1; c = c + 1}a:int = 0b:int = 1c:int = 1scala>:p aste//Entering paste mode (ctrl-d to Fini SH) var d = if (a <) {B = b + 1; C + 1}//Exiting paste mode, now Interpreting.d:anyval = 2
Input and output
print and Println:print do not add line breaks when printing, and a newline character is added println printing.
• For example, print ("Hello World"); println ("Hello World")
printf:printf can be used for formatting
• For example, printf ("Hi, my name is%s, I ' m%d Years old.\n", "Import", 20)
Readline:readline allows us to read user input data from the console, similar to the role of system.in and scanner in Java.
• Integrated Case: Arcade Access
Val name = ReadLine ("Welcome to the Game house. Please tell me your name: ")
Print ("Thanks. Then click me your Age: ")
Val age = ReadInt ()
if (age > 18) {
printf ("Hi,%s, you is%d years old, so" is Legel to come here! ", name, age)
} else {
printf ("Sorry, Boy,%s, is only%d years old. You is illegal to come here! ", name, age)
}
Cycle
while do Loop: Scala has a while Do loop with the same basic semantics as Java.
scala>:p aste//Entering paste mode (ctrl-d to finish) var n = 10while (n > 0) {println (n) N-= 1}//Exiting paste mode , now Interpreting.10987654321n:int = 0
· Scala does not have a for loop and can only use the while override for loop, or use the simple version of the For statement
• Simple version for statement:
scala> var n = 10; for (i <-1 to N) println (i) 12345678910n:int = 10
Scala>
• Or use until, the table does not reach the upper limit:
Scala> for (i <-1 until N) println (i) 123456789scala>
• Strings can also be traversed, similar to Java's enhanced for loop
Scala> for (c <-"Hello World") print (c+ " ") H e l l o W o r L Dscala >
Jump out of a looping statement
Scala does not provide a break statement similar to Java.
• However, you can use the break function of a Boolean variable, return, or breaks instead.
scala> import scala.util.control.breaks._import scala.util.control.breaks._scala>:p aste//Entering paste mode ( ctrl-d to finish) breakable {var n = 10for (c <-"Hello World") {if (n = = 5) break;print (c) N-= 1}}//Exiting paste mode, Now interpreting. Helloscala>
Advanced for Loop
Multiple for loop: 99 multiplication table
scala>:p aste//Entering paste mode (ctrl-d to finish) for (I <-1 to 9, J <-1 to 9) {if (j = = 9) {println (i * j)} else {print (I * j + "")}}//Exiting paste mode, now Interpreting.1 2 3 4 5 6 7 8 92 4 6 8 10 12 14 16 183 6 9 12 15 18 21 24 274 8 12 16 20 24 28 32 365 10 15 20 25 30 35 40 456 12 18 24 30 36 42 48 547 14 21 28 35 42 49 56 638 16 24 32 40 48 729-Si-81scala>
If guard: Take even
Scala> for (I <-1 to If I% 2 = = 0) println (i) 2468101214161820scala> for (I <-1 to If I% 2 = 1) printl N (i) 135791113151719scala>
For derivation: Construct a collection
Scala> for (i <-1 to ten) yield ires13:scala.collection.immutable.indexedseq[int] = Vector (1, 2, 3, 4, 5, 6, 7, 8, 9 , ten) scala>
scala-condition Control and circulation