YouTip LogoYouTip

Scala For Loop

[![Image 1: Scala Loops](#) Scala Loops](#)\n\nThe for loop allows you to write a loop control structure that executes a specified number of times.\n\n* * *\n\n## Syntax\n\nThe syntax of the **for** loop in the Scala language:\n\nfor( var x <- Range ){ statement(s);}\nIn the above syntax, **Range** can be a numeric interval represented by **i to j** or **i until j**. The left arrow <- is used to assign a value to the variable x.\n\n### Example\n\nThe following is an example using the **i to j** syntax (including j):\n\n## Example\n\nobject Test {\n\ndef main(args: Array){\n\nvar a =0;\n\n// for Loops\n\nfor( a <- 1 to 10){\n\nprintln("Value of a: " + a );\n\n}\n\n}\n\n}\n\nExecuting the above code outputs the following result:\n\n$ scalac Test.scala $ scala Test value of a: 1 value of a: 2 value of a: 3 value of a: 4 value of a: 5 value of a: 6 value of a: 7 value of a: 8 value of a: 9 value of a: 10\nThe following is an example using the **i until j** syntax (excluding j):\n\n## Example\n\nobject Test {\n\ndef main(args: Array){\n\nvar a =0;\n\n// for Loops\n\nfor( a <- 1 until 10){\n\nprintln("Value of a: " + a );\n\n}\n\n}\n\n}\n\nExecuting the above code outputs the following result:\n\n$ scalac Test.scala $ scala Test value of a: 1 value of a: 2 value of a: 3 value of a: 4 value of a: 5 value of a: 6 value of a: 7 value of a: 8 value of a: 9\nIn a **for loop**, you can use a semicolon (;) to set multiple intervals, which will iterate over all possible values of the given intervals. The following example demonstrates a loop with two intervals:\n\n## Example\n\nobject Test {\n\ndef main(args: Array){\n\nvar a =0;\n\nvar b =0;\n\n// for Loops\n\nfor( a <- 1 to 3; b <- 1 to 3){\n\nprintln("Value of a: " + a );\n\nprintln("Value of b: " + b );\n\n}\n\n}\n\n}\n\nExecuting the above code outputs the following result:\n\n$ scalac Test.scala $ scala TestValue of a: 1Value of b: 1Value of a: 1Value of b: 2Value of a: 1Value of b: 3Value of a: 2Value of b: 1Value of a: 2Value of b: 2Value of a: 2Value of b: 3Value of a: 3Value of b: 1Value of a: 3Value of b: 2Value of a: 3Value of b: 3\n\n* * *\n\n## for loop over a collection\n\nThe syntax for a for loop over a collection is as follows:\n\nfor( x <- List ){ statement(s);}\nIn the above syntax, the **List** variable is a collection, and the for loop will iterate over all elements of the collection.\n\n### Example\n\nThe following example will loop over a collection of numbers. We use _List()_ to create the collection. We will introduce collections in detail in later chapters.\n\n## Example\n\nobject Test {\n\ndef main(args: Array){\n\nvar a =0;\n\nval numList = List(1,2,3,4,5,6);\n\n// for Loops\n\nfor( a <- numList ){\n\nprintln("Value of a: " + a );\n\n}\n\n}\n\n}\n\nExecuting the above code outputs the following result:\n\n$ scalac Test.scala $ scala Test value of a: 1 value of a: 2 value of a: 3 value of a: 4 value of a: 5 value of a: 6\n\n* * *\n\n## for loop filtering\n\nScala can use one or more **if** statements to filter out some elements.\n\nThe following is the syntax for using filters in a for loop.\n\nfor( var x <- List if condition1; if condition2... ){ statement(s);}\nYou can use a semicolon (;) to add one or more filtering conditions to the expression.\n\n### Example\n\nThe following is an example of filtering in a for loop:\n\n## Example\n\nobject Test {\n\ndef main(args: Array){\n\nvar a =0;\n\nval numList = List(1,2,3,4,5,6,7,8,9,10);\n\n// for Loops\n\nfor( a <- numList\n\nif a !=3;if a <8){\n\nprintln("Value of a: " + a );\n\n}\n\n}\n\n}\n\nExecuting the above code outputs the following result:\n\n$ scalac Test.scala $ scala Test value of a: 1 value of a: 2 value of a: 4 value of a: 5 value of a: 6 value of a: 7\n\n* * *\n\n## for loop with yield\n\nYou can store the return value of a for loop as a variable. The syntax format is as follows:\n\nvar retVal = for{ var x <- List if condition1; if condition2...}yield x\nNote that the curly braces are used to hold variables and conditions, _retVal_ is the variable, and yield in the loop will record the current element, save it in a collection, and return the collection after the loop ends.\n\n### Example\n\nThe following example demonstrates the use of yield in a for loop:\n\n## Example\n\nobject Test {\n\ndef main(args: Array){\n\nvar a =0;\n\nval numList = List(1,2,3,4,5,6,7,8,9,10);\n\n// for Loops\n\nvar retVal =for{ a <- numList \n\nif a !=3;if a <8\n\n}yield a\n\n// Print return value\n\nfor( a <- retVal){\n\nprintln("Value of a: " + a );\n\n}\n\n}\n\n}\n\nExecuting the above code outputs the following result:\n\n$ scalac Test.scala $ scala Test value of a: 1 value of a: 2 value of a: 4 value of a: 5 value of a: 6 value of a: 7\n[![Image 2: Scala Loops](#) Scala Loops](#)
← Scala Break StatementScala Do While Loop β†’