040_for循环的两种方式

Switch先不讲

image-20210324091250173

他用的match-case

image-20210324091344192

package com.atguigu.chapter04.myfor

/*
* package com.atguigu.chapter04.myfor
* 包名不能含有关键字
* */
object ForDemo01 {
  def main(args: Array[String]): Unit = {
    //输出10句 "hello,尚硅谷!"
    val start = 1
    val end = 10
    //说明
    //1. start 从哪个数开始循环
    //2. to 是关键字
    //3. end 循环结束的值
    //4. start to end 表示前后闭合
    for (i <- start to end) {

      println("你好,尚硅谷" + i)
    }

    //说明for 这种推导式,也可以直接对集合进行遍历
    var list = List("hello", 10, 30, "tom")
    for (item <- list) {
      println("item=" + item)
    }


  }
}

image-20210324091734086

package com.atguigu.chapter04.myfor
/**
 *
 * @author victor
 * @site https://victorfengming.gitee.io/
 * @project scala
 * @package com.atguigu.chapter04.ifelse
 * @created 2021-03-24 18:59
 */
object ForUntilDemo02 {
  def main(args: Array[String]): Unit = {
    //输出10句 "hello,尚硅谷!"
    val start = 1
    val end = 11
    //循环的范围是  start --- (end-1)
    for (i <- start until end) {
      println("hello, 尚硅谷" + i)
    }
  }
}


results matching ""

    No results matching ""