Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Building Applications with Scala
Source

Scala conditional and loops statements

Like any other language, Scala has support for conditional statements like if and else. While Java has a switch statement, Scala has a more powerful and functional structure called Pattern Matcher, which we will cover later in this chapter. Scala allows you to use if statements during variable assignments, which is very practical as well as useful.

If statements in Scala REPL

We will see how to use if statements in Scala REPL as follows:

$ scala

Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_77).

Type in expressions for evaluation. Or try :help.

scala>val x = 10

x: Int = 10

scala>if (x == 10)

| println ("X is 10")

X is 10

scala>val y = if (x == 10 ) 11 y: AnyVal = 11 scala>y

res1: AnyVal = 11 scala>

In the preceding code, you can see that we set the variable y based on an if condition. Scala if conditions are very powerful, and they also can be used in return statements.

If statements in return statements in Scala REPL

We will see how to use if statements in return statements in Scala REPL as follows:

$ scala

Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_77). Type in expressions for evaluation. Or try :help. scala>val x = 10 x: Int = 10

scala>def someFunction = if (x == 10) "X is 10"

someFunction: Any

scala>someFunction

res0: Any = X is 10

scala>

Scala supports else statements too, and you also can use them in variables and return statements as wellas follows:

~$ scala

Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_77).

Type in expressions for evaluation. Or try :help.

scala>val x = 10

x: Int = 10

scala>if (x==10){

| println("X is 10")

| } else {

| println ("X is something else")

| }

X is 10 scala>

Now you will learn how to use for loops in Scala. For loops are very powerful in Scala. We will start with the basics and later we will move on to functional loops used for comprehensions, also know as List comprehensions.

In Scala, for loops work with ranges, which is another Scala data structure that represents numbers from a starting point to an end point. The range is created using the left arrow operator(<-). Scala allows you to have multiple ranges in the same for loop as long as you use the semicolon(;).

You also can use if statements in order to filter data inside for loops, and work smoothly with List structures. Scala allows you to create variables inside a for loop as well. Right now, let's see some code which illustrates the various for loop usages in Scala language.

 
Source
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Mathematics
Political science
Philosophy
Psychology
Religion
Sociology
Travel