Unlike Java and JavaScript, which use the ternary operator to perform a logical operation, Scala uses another method. Scala uses the if-else
conditional statement to achieve this purpose.
if(condition) expression1 else expression2
condition
: This is the specified condition, which returns true or false.
expression1
: This is executed if the condition returns true.
expression2
: This is executed if the condition returns false.
object Main extends App {var num1 = 100var num2 = 2var num3 = 200var num4 = 9000// set large to 0var large = 0// compare num1 and num2large = if (num1 > num2) num1 else num2printf("Largest between %d and %d number is: %d\n", num1 , num2, large)// compare num3 and num4large = if (num3 > num4) num3 else num4printf("Largest between %d and %d number is: %d\n", num3 , num4, large)}
large
which holds the larger of two numbers.