scala in three ways to stop the cycle

1: Use the return key

{BreakLoop Object 
    . //. 1 using the return key 
  DEF the Add (): Unit = { 
      for (I <-. 1 to 10) { 
        IF (I ==. 7) { 
          // stop the loop 
          // Use Note: termination method and with back a return value 
          // use return -> stop the method, it is not recommended for use in the main method 
           return 
        } 
        the println (I) 
      } 
  }

2. Use a Boolean variable response is substantially while or do-while loop

main DEF (args: the Array [String]): Unit = { 
       var = In Flag to true 
       var = 0 n- 
       the while (In Flag) {// infinite loop 
         n-+ =. 1 
         the println (n-) 
         IF (n-== 10) { 
           In Flag to false = // stop the circulating 
         } 
         
       } 
      // for loop 
      var = FLAG1 to true 
      for (I <- 0 to 10 FLAG1 IF) { 
        the println (I) 
        IF (I ==. 7) { 
           FLAG1 to false = 
        } 
      }

3: break, but also you need to import a package

import scala.util.control.Breaks._

import scala.util.control.Breaks._
    //break块
    breakable{
      for(i <- 1 to 10){
         if (i == 7){
           break
         }
      }
    }

  

Guess you like

Origin www.cnblogs.com/liangyan131/p/12014377.html