1

I have the following java code:

    for (int i = 0, j = 0; i < 10 && j < 10 0; i++, j++)
  {
           System.out.println("i = " + i + " :: " + "j = " + j);

  }

The output is :

i = 0 :: j = 0
i = 1 :: j = 1
i = 2 :: j = 2
i = 3 :: j = 3
i = 4 :: j = 4
i = 5 :: j = 5
....

I would like to do the same thing in scala, I tried this but it does not work:

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

The output is:

i = 0 :: j = 0
i = 0 :: j = 1
i = 0 :: j = 2
i = 0 :: j = 3
i = 0 :: j = 4
i = 0 :: j = 5
i = 0 :: j = 6
i = 0 :: j = 7
i = 0 :: j = 8
i = 0 :: j = 9
i = 1 :: j = 0
i = 1 :: j = 1
i = 1 :: j = 2
i = 1 :: j = 3
....

I have not find a way to have two variables in the same level. Thank you for your answer.

Sandra
  • 85
  • 6
  • If both values will have the same value, what is even the point of having two? What exactly do you want to solve? BTW, **Scala** doesn't have for loops, it has for comprehensions, there is no mutation involved at all. – Luis Miguel Mejía Suárez Jan 30 '21 at 18:08
  • 2
    @LuisMiguelMejíaSuárez I don't think it's about literally having two identical values. I'd guess it's about the principle of iterating over two variables at once. And it can be done either with `for` and a custom structure or a `while` loop with less sexy syntax (var i, var j, while(...) {... i+=1; j+=1} – Nebril Jan 30 '21 at 18:11
  • 1
    `for` in Scala is _not_ a for-loop from Java. In some cases the behavior might be similar but one cannot be brainlessly substituted for another. – Mateusz Kubuszok Jan 30 '21 at 18:11
  • @Nebril, yes you are right. it's about the principle of iterating over two variables at once.How can I do that in scala ? – Sandra Jan 30 '21 at 18:13
  • @Nebril the point is one doesn't iterate values in functional programming, as such two values having the same value are the same thing. That is why I ask what exactly is the problem OP wants to solve instead of focus in the technique. – Luis Miguel Mejía Suárez Jan 30 '21 at 18:14
  • 1
    @LuisMiguelMejíaSuárez Ok, so I share with you the problem I want to solve. – Sandra Jan 30 '21 at 18:16
  • @LuisMiguelMejíaSuárez I have shared the problem in the following link : https://stackoverflow.com/questions/65972252/how-to-substract-two-consecutive-element-in-a-list-in-scala – Sandra Jan 30 '21 at 19:28

4 Answers4

4

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

Mateusz Kubuszok
  • 24,995
  • 4
  • 42
  • 64
2

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) }
Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
0

you can do it as below

val start = 0; val size = 10;
for ((i, j) <- (start to size) zip (start to size))
 {
  println(s"i=$i j=$j")
}
Nikunj Kakadiya
  • 2,689
  • 2
  • 20
  • 35
0

j is just a copy of i so this is one solution:

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

This pattern works in any situation where j is just a function of i

Tim
  • 26,753
  • 2
  • 16
  • 29