3

I am trying to learn scala, here I am using basic for loop, but I am getting errors while compiling.

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

    for (i <- 1 to 10; i % 2 == 0)
      Console.println("Counting " + i)

  }
}

Errors while compiling :

fortest.scala:5: error: '<-' expected but ')' found.

    for (i <- 1 to 10; i % 2 == 0)
                                 ^
fortest.scala:7: error: illegal start of simple expression

  }
  ^

two errors found

I am using scala version 2.9.1

Any idea what is the problem..............?

6 Answers 6

8
for (i <- 1 to 10 if i % 2 == 0)
  println("Counting " + i)
Sign up to request clarification or add additional context in comments.

Comments

7

Scala is not Java, thus you cannot use a regular Java syntax. Instead you have to do:

for{
    i <- 1 to 10
    if(i % 2 == 0)
}{println("Counting " + i)}

or with ; delimeters, inside the (,) parentheses:

for(i <- 1 to 10;if(i % 2 == 0)){
    println("Counting " + i)
}

Also, note that Scala's for expressions, have some pretty nifty capabilities. you can use a for expression with multiple "loop iterators" and conditions.

For instance, instead of writing:

for(i <- 1 to n; if(someCondition(i)){
    for(j <- 1 to m; if(otherCondition(j)){
         //Do something
    }
}

You can simply write:

for{
    i <- 1 to n
    if(someCondition(i))
    j <- 1 to m
    if(otherCondition(j))
}{
    //Do something
}

SIDE NOTE:
When you extend App (there's a trait of that name in Predef), you don't need to define a main method. You can simply write your code between the curly braces of object:

object MyClazz extends App {
    for(i <- 1 to 10;if(i % 2 == 0)){
        println("Counting " + i)
    }
}

3 Comments

Thanks, what the basic concept behind scala, which prevents multiple conditions in a for loop?
you can invoke as many conditions as you want. just add more if lines... (i'll edit my answer to include more info on that)
you can skip semicolon between generator and if-guard: for (i <- 1 to 10 if i % 2 == 0) println(i)
6

Take a look at the "by" method of the Range class to count by 2

object App {    
  def main(args: Array[String]) {
    for (i <- 2 to 10 by 2)
      Console.println("Counting " + i)
  }
}

Or, like others have already stated you can fix your loop by doing

object App {    
  def main(args: Array[String]) {
    for {
         i <- 1 to 10
         if i % 2 == 0
        }
      Console.println("Counting " + i)
  }
}

Or another way:

object App {    
  def main(args: Array[String]) {
    val evenNumbers = for {
         i <- 1 to 10
         if i % 2 == 0
        } yield i
    Console.println(evenNumbers.mkString("\n"))
  }
}

Comments

1

The modulo 2 condition can be moved to an if clause.

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

    for (i <- 1 to 10)
      if(i % 2 == 0)
      Console.println("Counting " + i)

  }

}

Comments

0

Here is the simply example;

 for (i <- List(1, 2, 3) if i < 2) println(i)

The best way to exam your code is to use scala shell.

Comments

0

Basically, you are trying to use for-loop + iterator gaurd. Please find below syntax

for ( i <- 1 to 10 if (i%2==0) ) yield i

1 Comment

I don't think that your answer, being the 7th on the topic, adds much to this thread.

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.