循环结构
-
循环语句功能:在某些条件满足的情况下,反复执行特定代码的功能
-
循环语句的四个组成部分:
- 初始化部分
- 循环条件部分
- 循环体部分
- 迭代部分
-
循环语句分类
- for循环
例1:连续输出好多好多东西
public class practice { public static void main(String[] args){ //连续输出五次hello world // System.out.println("hello world"); // System.out.println("hello world"); // System.out.println("hello world"); // System.out.println("hello world"); // System.out.println("hello world"); //这样可以输出结果,但是要求的次数多了就做不到了 //用for循环可以解决 for(int i = 0;i < 5;i++){//初始化表表达式;布尔值测试表达式;更改表达式 System.out.println(i); // System.out.println("hello world"); } //初始化变量,咱们这里是i,判断i<5,如果是,执行代码块{ },执行更改表达式i++给i重更新赋值,值就变为1,后面同理 //判断变量i<5,当前i是5.判断结果是false,不再往下执行,结束循环 } }
例2:求水仙花数
for(int i = 100;i <=999;i++){ //水仙花数怎么表示,145,145/100=1