JAVA基础(5)-循环语句

循环结构

            当需要重复执行相同的代码或者是相似的代码时,就要用到循环结构了。

循环三要素:
              (1)循环变量的声明:用于控制循环次数的循环因子
              (2)循环条件:用于判断是否执行相同或相似内容(循环体)的条件
              (3)循环变量的改变方向:向着循环结束的方向改变。、

(1)for循环:

          语法:
                 for(变量的声明和初始化;循环条件;变量的改变方向){
                       循环体;
                  }

            执行逻辑:
                   程序遇到for时,一定执行变量的声明和初始化,然后执行循环条件进行判断,如果为false,会跳过循环结构,执行后续代码。如果为true,执行循环体,然后再执行变量的改变,再执行循环条件的判断,.......

            循环体:是要重复执行的相同或相似逻辑


            break关键字:用在循环结构中时,表示结束/打断循环
            continue关键字:用在循环结构中,表示结束当次循环体,继续下一次循环

            双层for循环:

                     for循环中有嵌套了一个for循环,外层变量执行一次,内层变量执行一遍或外层变量控制行数,内层变量控制列数

练习代码:

public static void main(String[] args) {
                //打印一个空心等腰三角形
                int n=6;
                for(int i=0;i<n;i++){
                    for(int j=i;j<n;j++){
                        System.out.print(" ");
                    }
                    System.out.print("*");
                    if(i!=n-1){
                        for(int x=i*2-1;x>0;x--){
                        System.out.print(" ");
                        }    
                        if(i!=0){
                            System.out.print("*");
                        }
                    }else{
                        for(int k=0;k<(n*2-2);k++){
                            System.out.print("*");
                        }
                    }
                    
                    System.out.println();
                }
    }

(2)while循环

               语法:

                       while(循环条件){
                                循环体
                        }

              执行逻辑:
                     当程序遇到while时,一定执行循环条件,如果判断结果为false,就结束循环结构,执行后续代码,如果判断结果为true,就执行循环体,然后再判断循环条件......

练习代码:

public static void main(String[] args){
        /*打印十次 hello world*/
        int count = 1;
        while(count<=10){
            System.out.println("hello world");
            count++;
        }
        /* 第二种常用写法 */
        int num = 1;
        while(true){
            System.out.println("hello world");
            num++;
            if(num>10){
                break;
            }
        }
    }

(3)do-while循环

               语法:
                      do{
                           循环体
                      }while(循环条件);


               执行逻辑:当程序遇到do关键字时,一定先执行一次循环体,然后再判断循环条件,如果条件为false,结束循环结构,执行后续代码,如果条件为true,再执行一次循环体,然后再判断条件的成立与否........

    
               while/do-while/for的区别:

                        while/do-while:适合不知道循环次数的逻辑
                        for:适合知道循环次数的逻辑

                        while/for一般先判断条件,再执行循环体
                        do-while:一定先执行一次循环体,再判断条件

   使用技巧:什么时候使用while,什么时候使用do-while某些逻辑可以翻译成如下:

                    当......就执行.....:适合使用while
                    做.....直到.......:适合使用do-while

练习代码:

    public static void main(String[] args){
        /*银行卡密码为 888888 
          使用do-while模拟ATM输入密码操作
        */
        Scanner sc = new Scanner(System.in);
        int input = -1;
        do{
            System.out.println("请输入密码:");
            input = sc.nextInt();
        }while(input!=888888);
        System.out.println("密码验证成功");
 
    }

猜你喜欢

转载自www.cnblogs.com/yuchen656/p/9386049.html