Decreasing for loop in Scala?

前端 未结 8 1025
一生所求
一生所求 2020-12-17 20:24

First day and first attempt at using Scala - so go easy on me! I\'m trying to rewrite some old Java code I have which is simply a function which takes two numbers and prints

8条回答
  •  不知归路
    2020-12-17 21:12

    This way you can use Decreasing for loop in Scala.

        object Example extends App {
    
    
          for(i <- 20 to 2 by -2){
    
    
            println("Value of i = "+ i)
    
          }
        }
    ------------------
    O/P
    ------------------
    Value of i = 20
    Value of i = 18
    Value of i = 16
    Value of i = 14
    Value of i = 12
    Value of i = 10
    Value of i = 8
    Value of i = 6
    Value of i = 4
    Value of i = 2
    

提交回复
热议问题