Scala Control structures(if-else) program

Category : Scala | Sub Category : Scala Programs | By Runner Dev Last updated: 2020-10-07 20:07:22 Viewed : 240


Scala  Control structures(if-else) program


Scala’s if/else control structure is similar to other languages.

There are various kinds of if-else statements:

- If statement,

- If-Else statement,

- Nested If-else statement,

- If-Else-If-Else statement.

 

if/else  Syntax:

  if (test1) {

    doA()

  } else if (test2) {

    doB()

  } else if (test3) {

    doC()

  } else {

    doD()

  }

The Scala ternary operator syntax:

val a = if (i == 1) x else y

Example:

Following example illustrates about scala If else condition.

Save the file as − IfElseCon.scala.

  

IfElseCon.scala // Filename

package runnerdev 

object IfElseCon extends App {

  var x: Int = 20;

  var y: Int = 30;

  if (x > y) {

    println("x is greater than y");

  } else {

    println("x is not greater than y");

  } 

}

compile and run the above example as follows 

scala> scalac IfElseCon.scala

scala> scala IfElseCon

 

OutPut:

  x is not greater than y

Search
Sub-Categories
Related Articles

Leave a Comment: