What is Scala Control Structure?

The Control Structure is the programming statements that are used to control the flow of program execution. It represents the order in which each statement should run. Scala provides control flow statements such as if-else, for loop, while loop, and so on.

Let us see various control structures supported by Scala.


Conditional Statements


1. if Statement

If statements work on a boolean condition. If the specific condition is true then the block of statements will execute otherwise the execution flow will exist from if statement.

The syntax of the if-statement is as below.

If (boolean_condition) // this part of statement will execute if Boolean condition will meet.

Let us understand if statement with below example. Save the program with ifexample.scala name and run it from the terminal by using scala ifexample.scala command(refer screenshot).

object ifexample { def main(args: Array[String]) { var data = 100; if(data < 200 ){ println("If condition is true"); } } }



Click Here To Download Code File.

2. if-else Statement

If-else statement will have an additional else statement which will execute when the Boolean condition gets false.

The syntax of the If-else statement is as below.

If (boolean_condition){ // this part of statement will execute } else { // this part of statement will execute }

Let us understand the if-else statement with the below example. Save the program with ifelseexample.scala name and run it from the terminal by using scala ifelseexample.scala command(refer screenshot).

object ifexample { def main(args: Array[String]) { var data = 100; if(data < 200 ){ println("If condition is true"); } else { println("If condition is false"); } } }



Click Here To Download Code File.

3. If-else-if-else Statement

If-else-if-else Statement will have an additional 'else if...else' statement.

The syntax of the If-else-if-else statement is as below.

if(Boolean_condition 1){ // this part of statement will execute } else if(Boolean_condition 2){ // this part of statement will execute } else if(Boolean_condition 3){ // this part of statement will execute } else { // this part of statement will execute when none of the above condition is true. }

Let us understand the If-else-if-else statement with the below example. Save the program with ifelseifexample.scala name and run it from the terminal by using the scala ifelseifexample.scala command(refer screenshot).

object ifelseifexample { def main(args: Array[String]) { var data = 100; if(data == 200 ){ println("If condition is true with data value 200"); } else if(data == 100) { println("If condition is true with data value 100"); } else if(data == 50) { println("If condition is true with data value 50"); }else{ println("This condition will be true if all above conditions are false"); } } }



Click Here To Download Code File.


Control flow Statement

A Loop statement executes the sequence of statements many times until the condition becomes false. It has two parts, a loop body, and a control statement. The control statement executes until the specified condition becomes false.

1. for loop with ranges

The For-Loop executes a program code till the condition is true. It is basically used for iteration and it has two parts. The first part is the iteration and the second part is the body that executes based on iteration.

The syntax of for loop is as below.

for( variable <- range of value){ // execute the code }

Let us understand for loop with the below example. Save the program with forloopexample.scala name and run it from the terminal by using the scala forloopexample.scala command(refer screenshot).

object forloopexample { def main(args: Array[String]) { var data = 0; for( data <- 1 to 20){ println( "Print Data: " + data); } } }



Click Here To Download Code File.

2. for loop with until

The program will print value until the condition is true.

object forloopexample { def main(args: Array[String]) { var data = 0; for( data <- 1 until 20{ println( "Print Data: " + data); } } }

Let us understand for loop (until option) with the below example. Save the program with forloopexample1.scala name and run it from the terminal by using the scala forloopexample1.scala command(refer screenshot).




Click Here To Download Code File.

3. for Loop with Collections

We will create a list variable and for loop will run for each variable and return on value at a time.

The syntax of for loop with the list is mentioned below.

for( variable <- list){ // execute the code }

Let us see the example of for loop using the list, we have created a list of collection using the list() function. Save the program with listexample.scala name and run it from the terminal by using scala listexample.scala command(refer screenshot).

object listexample { def main(args: Array[String]) { var data = 0; val listvalue = List(1,2,3,4,5,6,7,8,9,10); // for loop execution with data collection for( data <- listvalue ){ println( "result of data: " + data ); } } }



Click Here To Download Code File.

4. for loop with Filters

To filter out some elements from for loop, we can use the if statement as a filter condition.

The syntax of for loop with filter (if statement) is mentioned below.

for(variable <- list if statement1; if statement1... ){ // execute the code);

Let us see the example of for loop using the filter, Save the program with filterexample.scala name and run it from the terminal by using scala filterexample.scala command(refer screenshot).

object filterexample { def main(args: Array[String]) { var data = 0; val listvalue = List(1,2,3,4,5,6,7,8,9,10); // for loop execution with data collection for( data <- listvalue if data !=5; if data > 8 ){ println( "result of data: " + data ); } } }



Click Here To Download Code File.

5. for loop with yield

By using yield, you can store return values from a "for" loop in a variable. In this, a curly bracket would be used in the place of a round bracket to keep variables and conditions.

The syntax of for loop with yield is mentioned below.

var result = for{ variable <- list if statement1; if statement1... }

Let us see the example of for loop using yield, Save the program with yieldexample.scala name and run it from the terminal by using scala yieldexample.scala command(refer screenshot).

object yieldexample { def main(args: Array[String]) { var data = 0; val listvalue = List(1,2,3,4,5,6,7,8,9,10); var result = for{ data <- listvalue if data !=5; if data > 8 } yield data for(data <- result){ println( "result of data: " + data ); } } }



Click Here To Download Code File.

6. while Loop

While loop works on the boolean value condition, if the condition is true then the program code will execute and control will test the condition again.

The syntax of the while loop is mentioned below.

while(condition){ //execution the statement; }

Let us see the example of while loop, Save the program with whileloopexample.scala name and run it from the terminal by using scala whileloopexample.scala command(refer screenshot).

object whileloopexample{ def main(args: Array[String]) { var data = 50; while(data < 70 ){ println( "Result of data: " + data); data = data + 1; } } }



Click Here To Download Code File.

6. do-while Loop

The Do While is also a kind of control statement that executes code at least once and after that the execute goes based on the condition defined in the block of code.

The syntax of the do-while loop is mentioned below.

do { //execution the statement; } while( condition );

Let us see the example of the do-while loop, Save the program with dowhileexample.scala name and run it from the terminal by using scala dowhileexample.scala command(refer screenshot).

object dowhileexample { def main(args: Array[String]) { var data = 50; do { println( "Value of data: " + data ); data = data + 1; } while( data < 70 ) } }



Click Here To Download Code File.