Control Structures in Scala- A brief overview

If Statements:

Lets consider a first entry example of If statments, without using much of Scala’s features.


if(5+6==10){

println("You are wrong in calculations")

}else{

println("Keep it up")

}

This is a pretty simple and straight forward examples. Now lets add some Scala flavor in the If-Statement. In Scala If-Statements are expressions, which means that the value from If-Statements can be assigned to some variable. Lets consider another example- Finding greatest of two numbers.

Note: I am using the scala interpreter.

scala>var num1=54
num1:Int=54
scala>var num2=76
num2:Int=76

I have created 2 variables. We will have to write a If-Statement for finding out the greatest value and assign it to another variable.

scala>var big=if(num1>num2){ num1}else{num2}
big:Int=76
scala>println(big)

One can see how the result from a if-expression is assigned to a variable. This feature of If construct has removed the need of a Select case construct. Also note that the Type Inference coming into play there- I haven’t mentioned tat big is a Int but it has inferred based on the If-Statement that it will hold an Int. If we had missed the else part then it would have inferred the type to be Unit (void in Java).

For Loops

In the Scala world they are also referred to as the “For Comprehension” or “For Expression”. Let’s consider a example list.

scala> var cities=List(“Bangalore,India”, “Delhi, India”, “Chennai, India”,”Tokyo, Japan”, “New York, USA”, “London, UK”, “Paris, France”)

The above code will create a List of Cities along with their countries.  Now lets use a for loop to “comprehend” the list.


scala> for(city <- cities)println(city)

Straight forward and simple. The left arrow operator (<-) is called as "generator” as it is used to generate elements from the list. Now lets print the cities in India with the help of the Scala’s features


scala&gt;for(city &lt;- cities if city.contains(&quot;India&quot;))println(city)

Bangalore,India

Delhi, India

Chennai, India

The above technique is called "Filtering“. One can add multiple filers (if condition) and the filers have to be seperated by using “;”.  Now suppose we want to store the Indian cities in a separate list, what do we do? Scala does have a solution to that as well. Lets look at the code:


scala&gt; var indianCities=for(city for(city &lt;-indianCities)println(city)

Bangalore,India

Delhi, India

Chennai, India

The type of the yielded list has not been mentioned. Scala infers the type from the Type of the Collection being used to yeild a new List.

While and Do-While Loops:

While loops, as in other languages, execute the code in the block until the condition is true. Do-While is similar to While the only difference being that the loop is executed at least once and checks to see if the condition is true or not.

Example to find sum of first 10 natural numbers


scala&gt; var i=1

scala&gt;var sum=0

scala&gt;while(iprintln(sum)

An example to show do while loop:


scala&gt;sum=0;i=0;

scala&gt;do{ sum +=i;i+=1}while(sumprintln(i)

That was a brief overview of If, For, While and Do-While constructs.

Leave a Reply

%d bloggers like this: