×

Scala Tutorial

Scala Basics

Scala Control statements

Scala Functions

Scala Arrays

Scala Lists

Scala Strings

Scala Classes & Objects

Scala Collections

Loops in Scala

By IncludeHelp Last updated : October 10, 2024

Loops in Scala

In programming, many times a condition comes when we need to execute the same statement or block of code more than one time. It could be difficult to write the same code multiple times, so programing language developers come up with a solution, name a loop statement.

A Loop statement executes the specified block of code multiple times based on some condition.

Types of Loops

Scala defines three types of loop statements. They are,

  1. for Loop
  2. while Loop
  3. do...while Loop

for Loop

A for loop in used when you want to execute a block of code n number of times. The number n is specified explicitly. It is used when the user knows the number of times the loop needs to be executed beforehand. That is before the loop statement the number of executions is specified.

In for loop, a variable is used. This variable is a counter to the number of loops that are executed using them for a statement.

Syntax

for(loop condition){
  // code to be executed...
}

Example

There are various ways to use for loop we are discussing basic one here.

object MyClass {
      def add(x:Int, y:Int) = x + y;

      def main(args: Array[String]) {
          var i = 0;
          print("Print numbers from 5 to 10\n")
         for(  i <- 5 to 10){
             print(i + "\n")
         }
      }
   }

Output

Print numbers from 5 to 10
5
6
7
8
9
10

while Loop

A while loop is used when you want to execute a block of code some condition is TRUE. The condition can be any variable, or expression. When evaluated the all positive values are treated as TRUE and 0 is treated as FALSE.

It checks the condition before entering the loop's block. If the condition is FALSE then the block is not executed. There might be a condition when the block of code does not execute at all. This condition arises when the condition is initially FALSE. There can also be infinite loop execution if the expression never turns FALSE and no internal termination statement is available.

Syntax

while(condition){
  //code to be executed
}

Example

object MyClass {
      def main(args: Array[String]) {
          var myVar = 2; 
          println("This code prints 2's table upto 10")
          while(myVar <= 10){
              println(myVar)
              myVar += 2;
          }
      }
   }

Output

This code prints 2's table upto 10
2
4
6
8
10

do...while Loop

A do...while loop is when you want to execute a block of code until a specific condition is TRUE. The condition can be any variable or expression. This condition when evaluated is TRUE for all positive value and for 0 it is FALSE.

It checks the condition before exiting the loop's block. If the condition is FALSE then the block is executed only once. There is never a condition when the block of code does not execute at all. There can be loop execution if the expression never turns FALSE and no internal termination statement is done.

Syntax

do{
  //code to be executed
}
while(condition);

Example

object MyClass {
      def main(args: Array[String]) {
          var myVar = 2; 
          println("This code prints 2's table upto 10")
          while(myVar <= 10){
              println(myVar)
              myVar += 2;
          }
      }
   }

Output

This code prints 2's table upto 10
2
4
6
8
10

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.