Scala Tutorial Part 2 – Control Structures and Functions in Scala

In our previous blogs we covered the introduction to functional programming and the basics of Scala, in this blog we will be discussing about the Control structures and the Functions in Scala. Here we will be explaining about the Arithmetic, relational, logical operators, if-else statement, while loops, For loops and methods.

Arithmetic Operators

As like in other languages, Scala also supports the arithmetic operators like +, -, *, / & %.

Let us perform the operations in the Scala shell. Refer the below screen shot for the same.

In the below screen shot we can see that we have declared the variable a as 10 and variable b as 20. We have performed all the arithmetic operations on this two variables.

Relational Operators

As like in other languages, Scala also supports the relational operators like ==, !=, >, <, >=, <=

Let us perform the operations in the Scala shell and see the outputs.

In the above screen shot you can see that we have declared the variable a as 10 and variable b as 20 and we have applied all the Relational operators on these two variables and their respective outputs are captured.

Logical Operators

As like in other programming languages, scala also supports the logical operators like &&, ||, !

Let us try these logical operators in the Scala shell.

In the above screen shot you can see that we declared the variable a as true and the variable b as false and performed all the logical operations and their respective outputs are captured.

If-Else

If-Else is used to evaluate a condition like “ if it is true perform this logic else perform other ”. As like other programming languages, scala supports two types of If-Else statements.

1.If-Else – only two conditions can be evaluated with this.

Let us try this if-else in the Scala shell.

You can see that in the above screen shot we have declared a as 20 and we have given a condition that if a>20 then print “a is greater than 20” else print “a is not greater than 20”.

Since a=20 which is not greater than 20, it has printed “a is not greater than 20”

Now let us perform the nested if-else ladder.

In the above screen shot you can see that we have given 3 conditions using if-else ladder.

The first condition is that if a>20 print “a is greater than 20” and if it is not true check whether a==20 and if it is true print “a is equal to 20” and if it is not true check whether a<20 if it satisfies the last condition then print “a is less than 20” and finally if none of the above conditions are satisfied, finally “a is not a number” is printed.

Here in this case since we have declared a as 20 so it prints “a is equal to 20”.

While Loops

In similar to other programming languages, Scala also has two types of while loops i.e., while and do-while.

In the while loop it first checks for the condition, if the condition is satisfied then it will enter into the loop or else not.

In the do while loop, the body of the loop will be executed once and then it checks for the condition. So do-while loop executes at least once irrespective of the condition.

Let’s check for the same using scala shell.

Let’s try do-while

In the above screen shot we can see that we have given the wrong condition in while loop i.e., i==0 and despite of this loop got executed once and it has printed 1.

This is the major difference between while and do-while loop.

For Loops

Scala supports different types of for loops, let us discuss each of them separately.

1.Range

Inside the for loop we can give the range of numbers between which the loop should iterate. The standard way of giving the range in a for loop is as follows:

for(variable_name<-starting_number to ending_number)

Refer the below screen shot for the same

2.Until

The second type of for loop is until here in the for loop we can give an expression, until the expression is satisfied the loop will get executed. The standard way of declaring this type of for loops is as follows:

for(varibale_name<- expression1 until expression2)

Refer the below screen shot for the same

In the above screen shot you can see that we have defined two variables a & b and then in the for loop we have set the condition like starting with a and then we have given another expression b*2 which is 5*2=10 print i.

3.Multiple Variables

Here in Scala we can define multiple variables inside a single for loop, which acts like a nested for loop. The standard way of declaring this type of for loop is as follows:

for(varibale1<-range;varibale2<-range)

Refer the below screen shot for the same.

In the above screen shot we can see that we have declared two variables i & j in the same for loop and for i we have given the range from 1 to 5 and for j we have given the range from 6 to 10 and we are printing the combination of (i,j) the output is like nested for loop’s output as shown in the above screen shot.

4.Iterator

Iterator is something like we will give a collection as input to the for loop and then it iterates for every element inside the collection. The structure of this for loop is as follows:

for(varibale_name<-collection)

Now let us try the same in the scala shell

In the above screen shot we can see that we have declared a List as myArrayList which is ranging from 1 to 10 and we are performing the iterator for loop on this collection. We are printing the every value inside the collection.

5.For Loop with Filters

These are special types of for loops in scala, we can use conditions on the values inside the collection. The structure of this for loop is as follows.

for(varibale_name<-collection;if condition1 ;if condition2)

In the above screen shot we can see that we have generated a list of values from 1 to 10 and then in the first for loop we have given only one condition i.e., if the value is divisible by 2 then print statement is executed giving the output as 2,4,6,8,10 and then in the next for loop we have given two condition i.e., the number should be divisible by both 2 & 4 so we got the output as 4,8.

6.Yield

Whenever we want to save the output of the for loop, we use this yield kind of for loop. With this type of for loop after executing the loop once it will return the output of that iteration so we will assign this for loop to an another variable. The standard form of this for loop is as follows:

val varibale_name =for(varibale_name1<-collection;if condition1 ;if condition2) yield varibale_name1

In the above screen shot, you can see that we have used the keyword yield at the end of the for loop so the output of that iteration will stored in the variable result. At the last we have a List as the final output of the for loop.

Methods

Methods are also known as functions, functions are used for the readability and code re usability purpose. The following is the syntax for writing a method or function in Scala.

```def functionName([list of parameters]):
[return type]={
function body return [exprsn]
}```

In Scala functions are like first class citizens, we can use them as return types, parameters, variables, arguments we can do whatever we want just like variables.

Now let us write a function for adding two numbers in the command prompt.

In the above screen shot we can see that we have defined a function for adding two numbers and we have passed 10,20 as parameters to the function and we got 30 as output.

We can far simplify this function. Refer the below screen shot for the same.

We have still many more types of declaring functions in scala. You can get enrolled to free Scala course from Acadgild and learn Scala with scratch.

Hope this blog helped you in learning Arithmetic, relational, logical operators, if-else statement, while loops, For loops and methods in Scala.

In our next blog on Scala series, we will be discussing about Classes, inheritance, Abstract class, objects, case classes and finally traits.Keep visiting our site for more updates on BigData and other technologies.

kiran

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close