3.3 循环while语句

一、学习要求

书籍参考章节: 第3.8章

知识点:

  • while循环的用法

二、参考知识

循环结构使用条件表达式来控制一个(一组)动作的重复执行。Java语言有三种形式的的循环语句。包括:while循环、do-while循环、for循环。本节我们介绍while循环。

while是最基本的循环,它的结构为:

while( 布尔表达式 ) {
  //循环内容
}

只要布尔表达式为 true,循环体会一直执行下去。
实例:

public class Test {
   public static void main(String args[]) {
      int x = 10;
      while( x < 20 ) {
         System.out.print("value of x : " + x );
         x++;
         System.out.print("\n");
      }
   }
}

以上实例编译运行结果如下:

value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19

你可能感兴趣的:(3.3 循环while语句)