Sunday 30 October 2016

Control Structures : If Else Expressions : Day 6 Learnings

The Scala built-in control structures are if, while, for, try, and match expressions. Scala’s built-in control structures are sufficient to provide features that their imperative equivalents provide, but because all of Scala’s control structures (except while loops) result in some value, these control structures support functional approach as well.

If..Else Expression Blocks 


The If..Else conditional expression is a classic programming construct for choosing a branch of code based on whether an expression resolves to true or false


In many languages this takes the form of an “if .. else if .. else” block, which starts with an “if,” continues with zero to many else if ” sections, and ends with a final “else” catch-all statement.

As a matter of practice you can write these same “if .. else if .. else” blocks in Scala and they will work just as you have experienced them in Java and other languages. As a matter of formal syntax, however, Scala only supports a single “if ” and optional “else” block, and does not recognise the “else if ” block as a single construct.

So how do “else if ” blocks still work correctly in Scala? Because “if .. else” blocks are based on expression blocks, and expression blocks can be easily nested, an “if .. else if ..else” expression is equivalent to a nested “if .. else { if .. else }” expression. 

Logically this is exactly the same as an “if .. else if .. else” block, and as a matter of syntax Scala recognises the second “if else” as a nested expression of the outer “if .. else” block.

Let’s start exploring actual “if ” and “if .. else” blocks by looking at the syntax for the simple “if ” block.

If Expressions 


Syntax: Using an If Expression

if (<Boolean expression>) <expression>


The term Boolean expression here indicates an expression that will return either true or false .

Here is a simple if block that prints a notice if the Boolean expression is true:

scala> if ( 47 % 3 > 0 ) println("Not a multiple of 3")
Not a multiple of 3 


Of course 47 isn’t a multiple of 3, so the Boolean expression was true and the println was triggered. 

Although an if block can act as an expression, it is better suited for statements like this one. The problem with using if blocks as expressions is that they only conditionally return a value. If the Boolean expression returns false, what do you expect the if block to return? 

scala> val result = if ( false ) "what does this return?" 
result: Any = ()

The type of the result value in this example is unspecified so the compiler used type inference to determine the most appropriate type. Either a String or Unit could have been returned, so the compiler chose the root class Any . This is the one class common to both String (which extends AnyRef ) and to Unit (which extends AnyVal ). Unlike the solitary “if ” block, the “if .. else” block is well suited to working with expressions.


If-Else Expressions 


Syntax: If .. Else Expressions 

if (<Boolean expression>) <expression>
else <expression>


Here is an example: 


scala> val x = 10; val y = 20
x: Int = 10
y: Int = 20

scala> val max = if (x > y) x else y
max: Int = 20


You can see that the x and y values make up the entirety of the if and else expressions.  The resulting value is assigned to max , which we and the Scala compiler know will be an Int because both expressions have return values of type Int .

Note: Some wonder why Scala doesn’t have a ternary expression (popular in C and Java) where the punctuation characters ? and : act as a one-line if and else expression. It should be clear from this example that Scala doesn’t really need it because its if and else blocks can fit compactly on a single line (and, unlike in C and Java, they are already an expression).

Using a single expression without an expression block in if..else expressions works well if everything fits on one line. When your if..else expression doesn’t easily fit on a single line, however, consider using expression blocks to make your code more readable. if expressions without an else should always use curly braces, because they tend to be statements that create side effects.

if..else blocks are a simple and common way to write conditional logic. There are other, more elegant ways to do so in Scala, however, using match expressions.


Note: Sample examples on if..else

If Expression in Scala
scala> if (exp) println("yes")

Multi-line If Expression

scala> if (exp) {
     |  println("Line one")
     |  println("Line two")
     | }

Multi-line Else Expression

scala> if (exp) {
     |  println("Hello")
     |  } else {
     |  println("Line one")
     |  println("Line two")
     | }

Multiple If-Else and Else-If Expression
scala> if (exp1) {

     |  println("Line one")
     |  println("Line two")
     | } else if (exp2) {
     |  println("Line one")
     |  println("Line two")
     | } else if (exp3) {
     |  println("Line one")
     |  println("Line two")
     | } else {
     |  println("Line one")
     |  println("Line two")
     | }



Share this article with your friends.

2 comments :

Related Posts Plugin for WordPress, Blogger...