2

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.

7
  • 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. Commented Jan 30, 2021 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} Commented Jan 30, 2021 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. Commented Jan 30, 2021 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 ? Commented Jan 30, 2021 at 18:13
  • 1
    @LuisMiguelMejíaSuárez Ok, so I share with you the problem I want to solve. Commented Jan 30, 2021 at 18:16

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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")
}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.