zoukankan      html  css  js  c++  java
  • 3.17Java循环结构

     

    3.17Java循环结构

    本章内容

    • while循环

    • do...while循环

    • for循环


    循环语句的作用:

    重复执行语句

    while循环

    特点:

    最基本的循环

    语句:

    while(布尔表达式){
       //循环内容
    };
    /*
    只要布尔表达式为true,循环就会一直执行下去
    */

    实例:

    public class Test{
       
       public static void main(String arguments[]){
           int x = 10;//循环初始值
           while(x < 20){
               System.out.println("Value of x:" + x );
               x++;
               System.out.println("\n");
          }
      }
    }
    /*
    x < 20;是循环控制器
    x++;是循环方法
    while循环三大构件:
    循环初始值
    循环控制器---布尔表达式
    循环方法
    */

    do...while循环

    特点:

    • while语句,如果不满足条件,不能进入循环。

    • 即使不满足条件,也至少执行一次

    • 布尔表达式在循环体的后面,所以语句块在检测布尔表达式之前已经执行了。 如果布尔表达式的值为 true,则语句块一直执行,直到布尔表达式的值为 false

    语句:

    do{
       //代码语句
    }while(布尔表达式);

    实例:

    public class TestNo3{
       
       public static void main(String arguments[]){
           int x = 10;
           
           do{
               System.out.println("value of x:" + x);
               x++;
               System.out.println("\n");
          }while( x < 20 );
      }
    }
    /*
    循环初始值
    循环方法
    循环控制器---布尔表达式
    */

    while循环和do...while循环对比

    while循环

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

    • 其次是布尔表达式

    • 最后是循环内容

    • 只要出现一个False就不会继续运行,不会得到结果

    do...while循环

    do{
       //循环内容
    }while(布尔表达式);
    • 现有do

    • 在有循环内容

    • 最后是while+布尔表达式

    • 即使出现False也会执行一遍程序得到一个结果


    for循环

    特点:

    • 循环执行的次数是在执行前就确定的

    语法:

    for(初始化;布尔表达式;更新){
       //代码语句
    }
    /*
    声明语句:
    声明新的局部变量,该变量的类型必须和数组元素的类型匹配,其作用域限定在循环语句块,其值与此时数组元素的值相等
    表达式:
    要访问的数组名
    或者是返回值为数组的方法
    */

    实例:

    public class Circulation_Practice_code {

       public static void main(String arguments[]){
           int [] numbers = {10,20,30,40,50};

           for (int x : numbers){
               System.out.print(x);
               System.out.print(",");
          }

           System.out.print("\n");

           String [] names = {"James","Harden","Lebron","Paul"};
           for (String name : names){
               System.out.print(name);
               System.out.print(",");
          }
      }
    }
    • 声明语句要和循环内容相匹配

    • 表达式也是


    break关键字

    特点:

    • break主要用在循环语句或者switch语句中,用来跳出整个语句块

    • break跳出最里层的循环,并且继续执行该循环下面的语句

    public class circulation_Practice_for_code {
    
        public static void main(String arguments[]){
            int [] numbers = {10,20,30,40,50};
    
            for( int x : numbers ){
                if( x == 30 ){
                    break;
                }
                System.out.print(x);
                System.out.print(",");
            }
        }
    }

    continue关键字

    特点:

    • continue适用于任何循环结构中,作用是让程序立即跳转到下一次循环的迭代

    • for循环中,continue语句使程序立即跳转到更新语句

    • while或者do...while循环中,程序立即跳转到布尔表达式的判断语句

    语法:

    continue

    实例:

    public class Circulation_Practice_continue_code {
        public static void main(String arguments[]) {
            int[] numbers = {10, 20, 30, 40, 50};
    
            for (int x : numbers){
                if (x == 30){
                    continue;
                }
                System.out.print(x);
                System.out.print(",");
            }
        }
    }
    It's a lonely road!!!
  • 相关阅读:
    Spring@Profile注解
    day 32 子进程的开启 及其用法
    day 31 udp 协议SOCK_DGRAM
    day 30 客户端获取cmd 命令的步骤
    day 29 socket 理论
    day 29 socket 初级版
    有关 组合 继承
    day 27 多态 接口 类方法 静态方法 hashlib 摘要算法模块
    新式类和经典类的区别
    day 28 hasattr getattr serattr delattr 和带__内置__ 类的内置方法
  • 原文地址:https://www.cnblogs.com/JunkingBoy/p/14564742.html
Copyright © 2011-2022 走看看