Archive

Posts Tagged ‘while’

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)

&#91;/sourcecode&#93;

Straight forward and simple. The left arrow operator (&lt;-) is called as "<strong>generator</strong>" 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>for(city <- cities if city.contains("India"))println(city)

Bangalore,India

Delhi, India

Chennai, India

&#91;/sourcecode&#93;

The above technique is called "<strong>Filtering</strong>". 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> var indianCities=for(city <- cities if city.contains("India"))yield city

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

Bangalore,India

Delhi, India

Chennai, India

&#91;/sourcecode&#93;

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.

<strong>While and Do-While Loops:</strong>

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> var i=1

scala>var sum=0

scala>while(i<=10){ sum=sum+i;i=i+1}

scala>println(sum)

An example to show do while loop:


scala>sum=0;i=0;

scala>do{ sum +=i;i+=1}while(sum<100)

scala>println(i)

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

Categories: Scala Tags: , , , , , ,