Home » Scala language

Loops in Scala

Loops are the variables that are used to execute a block of code multiple times. In this article, we are going to learn about the loops is Scala with examples, codes, and syntaxes.
Submitted by Shivang Yadav, on June 23, 2019

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.

Scala defines three types of loop statements. They are,

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

1) 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

2) 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

3) 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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.