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.


println("You are wrong in calculations")


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
scala>var num2=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}

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)


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)


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


An example to show do while loop:


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: