Rehan Rehan - 3 months ago 9
Scala Question

scala nested looop-inner loop dependent on outer loop

I am relatively new to Scala. I am started building my skill by writting small programs.
The below code wanted to print the value of

i
in innner loop for each
i
value the inner loop
j
should run
j<=i
.

object Displaying {
def main (args : Array[String]){


for( i <- 1 to 10)
{

for(j<=i )//want to give a cnditation like (j <= i , j++)
{
print("")
print(i)
}
print("\n")

}
}
}


Kindly let me know where I am going wrong.

Thank you so much in advance !!!
HAPPY CODING

Answer

You almost there. Try to change your code this way:

for( i <- 1 to 10) {
  for(j <- 1 to i ) { //want to give a cnditation like (j <= i , j++)
    print("")
    print(i)
  }
  print("\n")
}
Comments