zoukankan      html  css  js  c++  java
  • java学习笔记07-循环

    java有三种主要的循环结构

    while循环

    do...while循环

    for循环

    while循环

    while(布尔表达式){

        //循环内容

    }

        public static void main(String[] args) {
            int i = 10;
            while(i<20) {
                System.out.println(i);
                i++;
            }
        }

    do...while循环

    do{

    代码语句

    }while(布尔表达式)

        public static void main(String[] args) {
            int i = 10;
            do {
                System.out.println(i);
                i++;
            }while(i>11);
        }

    对于while循环而言,不满足条件就不执行循环,而do...while循环,即使不满足条件,也会至少执行一次循环

    for循环

    for(初始化;布尔表达式;更新){

    代码语句

    }

        public static void main(String[] args) {
            for(int i=10;i<20;i++) {
                System.out.println(i);
            }
        }

    所有循环都可以用while或do...while。但是用for循环更方便一点

    增强for循环

    for(声明语句:表达式){

    代码语句

    }

    主要用于数组

        public static void main(String[] args) {
            int numbers[] = {1,2,3,4,5};
            for(int i :numbers) {
                System.out.println(i);
            }
        }


    break关键字

    用来终止循环

        public static void main(String[] args) {
            int numbers[] = {1,2,3,4,5};
            for(int i :numbers) {
                if(i==3){
                    break;
                }
                System.out.println(i);
            }
        }

    当i等于3时,终止循环

    continue关键字

    跳过该循环,进入下一个循环迭代

        public static void main(String[] args) {
            int numbers[] = {1,2,3,4,5};
            for(int i :numbers) {
                if(i==3){
                    continue;
                }
                System.out.println(i);
            }
        }

  • 相关阅读:
    PTP 接线方式及通讯距离
    串口通信基本概念
    Modbus RTU 通信应用案例
    Modbus 指令
    Modbus RTU新版本指令介绍
    Integer自动装箱和拆箱
    重写hashCode方法,导致内存泄漏
    Dom4j入门
    Java设计模式(9)——观察者模式
    IntelliJ IDEA版本控制——过滤提交文件
  • 原文地址:https://www.cnblogs.com/myal/p/10735846.html
Copyright © 2011-2022 走看看