WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

Scala - Loop Statements


we may need to execute the same piece of code multiple times. In general, statements are executed in order: the first statement in the function is executed first, then the second statement, and so on.

The programming language provides a variety of control structures for more complex execution paths.

Loop statements allow us to execute a single statement or group of statements. The following is a flowchart of loop statements in most programming languages:

Sr.No Loop Type & Description
1

while loop

Run a series of statements, if the condition is true, it will run repeatedly until the condition becomes false.

2

do-while loop

The difference between a similar and a statement is that the loop is executed once before the loop condition is evaluated.

3

for loop

Used to repeat a series of statements until a specific condition is reached, usually by increasing the value of the counter after each loop.

Output:

1
2
3
4
5
6
7
8
9
10

Loop Control Statements

The loop control statement changes the execution order of your code, through which you can implement code jumps. Scala The following loop control statements:

Scala does not support break or continue statements, but since version 2.8 provides a way to break loops, click on the link below to see the details.

Sr.No Control Statement & Description
1

break statement

Interrupt cycle

The infinite Loop

A loop becomes an infinite loop if a condition never becomes false

example
object Demo {
   def main(args: Array[String]) {
      var a = 10;
      
      // An infinite loop.
      while( true ){
         println( "Value of a: " + a );
      }
   }
}
Value of a: 10
Value of a: 10
Value of a: 10
Value of a: 10
…………….