Scala's replacement would be

for {
  (i, j) <- (0 to 9) zip (0 to 9)
} {
  println("i = " + i + " :: " + "j = " + j) 
}

To avoid the confusion I suggest reading what for is the syntactic sugar for (as opposed to Java it is not specialized while).

Answer from Mateusz Kubuszok on Stack Overflow
๐ŸŒ
Scala Documentation
docs.scala-lang.org โ€บ overviews โ€บ scala-book โ€บ for-loops.html
for Loops | Scala Book | Scala Documentation
In its most simple use, a Scala for loop can be used to iterate over the elements in a collection.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ scala โ€บ for-loop-in-scala
For Loop in Scala - GeeksforGeeks
July 20, 2021 - In for loop, We can use until when We want to print the value from 0 to n-1. Or in other words, until with for loop it excludes the end value like as shown in the below program, it prints only from 0 to 9 not print from 0 to 10. ... // Scala program to illustrate how to // create for loop using until object Main { def main(args: Array[String]) { println("The value of w is:"); // Here, the for loop starts from 0 // and ends at 10 for( w <- 0 until 10) { println(w); } } }
Discussions

For loop with two variables in scala - Stack Overflow
I have the following java code: for (int i = 0, j = 0; i More on stackoverflow.com
๐ŸŒ stackoverflow.com
For Loops
For loops always require mutable state and in scala its best practice to solve problems with immutable values, so its better to use things like filter, map and fold functions. This way you can avoid mutable state. Also dont confuse them with for comprehensions, these are absolutely fine to use for idiomatic scala code More on reddit.com
๐ŸŒ r/scala
17
2
June 5, 2020
foreach loop in scala - Stack Overflow
Please explain what happens when the two foreach loops are executed. ... It is design flaw in scala. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Fast C-style for loops in Scala 3
This has always been my biggest core problem with Scala. We are at a point where I think the compiler should infer when inlining is possible for basic loops. What's the point of syntactic sugar if we can't abstract these easy wins? I love that the feature is there but I wish it didn't have to be explicit in use. More on reddit.com
๐ŸŒ r/scala
19
53
September 21, 2021
๐ŸŒ
Rock the JVM
rockthejvm.com โ€บ articles โ€บ things-that-dont-make-sense-scala-loops
Things That Don't Make Sense: Scala Loops | Rock the JVM
April 15, 2020 - Another (understandable) confusion about the Scala language. Now, for-structures like the one above ARE built into the language. Itโ€™s just that theyโ€™re not what they seem: ... Thatโ€™s what the for โ€œloopโ€ above compiles to. Thatโ€™s why for-structures in Scala are called comprehensions.
Top answer
1 of 4
5

Scala's replacement would be

for {
  (i, j) <- (0 to 9) zip (0 to 9)
} {
  println("i = " + i + " :: " + "j = " + j) 
}

To avoid the confusion I suggest reading what for is the syntactic sugar for (as opposed to Java it is not specialized while).

2 of 4
3

Since both variables always have the same value, you actually only need one of them. In Scala, you would generally not use a loop to solve this problem, but use higher-level collection operations instead. Something like:

(0 to 9) map { i => s"i = $i :: j = $i" } mkString "\n"

Note: this will only generate the string that you want to print, but not actually print it. It is generally considered a good thing to not mix generating data and printing data.

If you want to print this, you only need to pass it to println:

println((0 to 9) map { i => s"i = $i :: j = $i" } mkString "\n")

Or, in Scala 2.13+:

import scala.util.chaining._

(0 to 9) map { i => s"i = $i :: j = $i" } mkString "\n" pipe println

You could also write it like this:

(for (i <- 0 to 9) yield s"i = $i :: j = $i") mkString "\n"

Now, you might say, "Wait a minute, didn't you just say that we don't use loops in Scala?" Well, here's the thing: that's not a loop! That is a for comprehension. It is actually syntactic sugar for collection operations.

for (foo <- bar) yield baz(foo)

is actually just syntactic sugar for

bar map { foo => baz(foo) }

A for comprehension simply desugars into calls to map, flatMap, foreach, and withFilter. It is not a loop.

Note that Scala does have a while loop. It exists mainly for performance reasons. Unless you are writing low-level libraries that are going to be used in performance-intensive code by tens of thousands of developers, please just pretend that it doesn't exist.

Also note that if the while loop weren't built into Scala, you could easily write it yourself:

def whiley(cond: => Boolean)(body: => Unit): Unit =
  if (cond) { body; whiley(cond)(body) }
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ scala โ€บ scala_for_loop.htm
Scala - for Loop
Scala's for loop allows to filter out some elements using one or more if statement(s). Following is the syntax of for loop along with filters.
Find elsewhere
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ scala syntax โ€บ for loops in scala
For Loops in Scala | Baeldung on Scala
March 18, 2024 - While in Java weโ€™re forced to write nested loops, at the same time in Scala we can use only one for with multiple generators.
๐ŸŒ
DataFlair
data-flair.training โ€บ blogs โ€บ scala-for-loop
Scala for loop - Types, Syntax and Example of For loop - DataFlair
December 4, 2021 - Scala for loop: Learn for loops in Scala, Types of Scala for-loop, i to j, i until j, Using Multiple Ranges in Scala for loop with syntax & examples
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ iterations-and-loops-in-scala โ€บ lessons โ€บ mastering-scala-for-loops-a-comprehensive-guide
Mastering Scala For Loops: A Comprehensive Guide
// Loop over a range of numbers for (i <- 1 to 3) { val innerValue = "hello" // Declare a variable in the loop } println(innerValue) // Error! innerValue is out of the scope ยท Our innerValue is scoped inside the loop, so trying to print it outside will result in an error. ... In Scala, single-statement iterations of loops can be written in a concise form without the use of braces ({}).
๐ŸŒ
Reddit
reddit.com โ€บ r/scala โ€บ fast c-style for loops in scala 3
r/scala on Reddit: Fast C-style for loops in Scala 3
September 21, 2021 - The JVM can do inlining for hot functions but it acts in a heuristic fashion. Anonymous functions are so common in Scala (and FP in general) I'm not sure why it was never implemented. Even the Java with the addition of lambdas automatically inline. ... Would be a bit cooler if you called arr as matrix... :P Great post btw! ... Being C-style is a big drawback on itself loops are just too generic to be useful.
๐ŸŒ
Waiting for Code
waitingforcode.com โ€บ home โ€บ scala core
Scala and for loop on waitingforcode.com - Scala core blog posts
August 23, 2018 - The last section mentions recursion that can be used instead of for loops. Scala's for loop definition is very similar to a foreach loop because it clearly suggests the iteration over a collection of items.
๐ŸŒ
Scala Documentation
docs.scala-lang.org โ€บ scala3 โ€บ book โ€บ taste-control-structures.html
Control Structures | Scala 3 โ€” Book | Scala Documentation
An expression returns a result, while a statement does not. Statements are typically used for their side-effects, such as using println to print to the console. The for keyword is used to create a for loop...
๐ŸŒ
Strumenti Resistenti
strumentiresistenti.org โ€บ blog โ€บ 2015-05-17-looping_in_scala
Looping in Scala โ€ข Strumenti Resistenti
May 17, 2015 - As Iโ€™ve noted before, Scala is so functional that every instruction has a value, as it happens in math. Scala is also functional in avoiding the standard three-elements-loop construct: /* C/C++/Java classic for loop */ for (initial condition; comparative condition; incremental instruction) { ... } This was considered so wrong by Scala designers that they purposely avoided including this construct.
๐ŸŒ
Scala Documentation
docs.scala-lang.org โ€บ overviews โ€บ scala-book โ€บ for-expressions.html
for Expressions | Scala Book | Scala Documentation
This page shows how to use Scala 'for' expressions (also known as 'for-expressions'), including examples of how to use it with the 'yield' keyword.
๐ŸŒ
IncludeHelp
includehelp.com โ€บ scala โ€บ the-for-loop-in-scala.aspx
The for loop in Scala
October 10, 2024 - Scala Misc. Topics ... for loop in Scala is used to execute a block of code multiple numbers of times. It executes the block n number of times where n is specified by some integer initialized before the start of the loop.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ scala โ€บ scala-loopswhile-do-while-for-nested-loops
Scala | Loops(while, do..while, for, nested loops) - GeeksforGeeks
July 11, 2025 - There are many variations of "for loop in Scala" which we will discuss in upcoming articles. Basically, it is a repetition control structure which allows the programmer to write a loop that needs to execute a particular number of times.