Java循环结构




for循环:

package for循环.fortest; /* * for循环语句格式: * for(初始化语句;判断条件语句;控制条件语句;){ * 循环体语句; * } * * * 执行流程: * 1.执行初始化值 * 2.执行判断条件语句,看结果是true还是false * 如果是true,就继续执行 * 若果是false,就不继续执行 * 3.执行循环体语句 * 4.执行控制条件语句 * 5.回到第二步继续 * */ public class fortest { public static void main(String[] args) { // // 在控制台输出10次“hello world~~” // for(int i = 1;i<=10;i++){ // System.out.println("HelloWorld~~"); // } //条件一定要在一个区间内 for(int a=10;a<20;a++){ System.out.println("helloworld"); } } }

for循环练习

 1 package for循环.fortest练习;
 2 
 3 /*
 4     练习:
 5 *   1.获取数据1-10,和10-1
 6 *
 7 *
 8 *
 9 * */
10 
11 
12 public class forDemo {
13     public static void main(String[] args) {
14 
15     //1.获取1-10和10-1之间的数据
16     for(int a= 1; a<=10;a++){
17         System.out.println(a);
18     }
19     System.out.println("----------");
20 
21     for(int i = 10;i>0;i--){
22         System.out.println(i);
23     }
24 
25     
26     
27     
28     }
29 }

while循环

package while循环;


/*
*   while循环结构:
*          while(判断条件语句){
*           循环体语句
*   }
*
* */



public class while_xuexi {

    public static void main(String[] args) {

        int a = 1;
        while (a<=10){
            System.out.println("helloworld");

            //加条件判断,不然一直循环
            a++;
        }

        System.out.println("---------");




        //用while循环求出1-100的和

        int sum = 0;
        int i = 1;
        while(i<=100){
            sum=i+sum;
            i++;
        }
        System.out.println(sum);
    }




}

  do...while循环练习

package do_while循环;


/*
*
*   do...while结构:
*       初始化语句--这个不写也不会报错
*       do{
*           循环体语句;
*   }while(条件判断语句);
*
*
*
*   执行流程:
*       1.执行初始化语句
*       2.执行循环语句
*       3.执行控制条件语句
*       4.执行条件判断语句,看是true还是false
* */

public class do_while_test {
    public static void main(String[] args) {

        //定义初始化值
        int a =1;

        do {
            System.out.println("ikun~~");//执行要循环的语句
            a++;
        }while (a<=15);//执行次数
    }
}