Java流程控制—循环语法

一、顺序结构

  • java的基本结构就是顺序结构
  • 语句与语句之间,框与框之间是按从上到下的顺序进行的,它是任何一个算法都离不开的一种基本结构

二、选择结构

  • if单选择结构

  • if双选择结构

  • if多选择结构

  • 嵌套的if语句

  • switch多选择结构

if单选择结构

if(布尔表达式){
  //如果布尔表达式为true执行语句   
}
package com.cheng.struct;

import java.util.Scanner;

public class ifDemo01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入内容:");
        String s = scanner.nextLine();

        //equals:判断字符是否相等
        if(s.equals("Hello")){
            System.out.println(s);
        }

        System.out.println("END");

        scanner.close();
    }
}

if双选择结构

if(布尔表达式){
    //如果布尔表达式的值为true执行代码  
}else{
    //如果布尔表达式的值为false执行代码
}
package com.cheng.struct;

import java.util.Scanner;

public class ifDemo02 {
    public static void main(String[] args) {
        //考试分数大于60就是及格,小于60就是不及格
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入分数");

        Double score = scanner.nextDouble();

        if(score>=60){
            System.out.println("及格");
        }else{
            System.out.println("不及格");
        }

        scanner.close();
    }
}

if多选择结构

if(布尔表达式1){
    //如果布尔表达式的值为true执行代码  
}else if(布尔表达式2){
    //如果布尔表达式的值为true执行代码
}else if(布尔表达式3){
    //如果布尔表达式的值为true执行代码
}else {
    //如果布尔表达式的值为true执行代码
}

注意

  • if语句至多有一个else语句,else语句在所有的else if之后。
  • if语句可以有若干个else if语句,它们必须在else语句之前。
  • 一旦其中一个else if 语句检测为true,其他的else if 以及 else 语句都将跳过执行。
package com.cheng.struct;

import java.util.Scanner;

public class ifDemo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        
        System.out.println("请输入成绩:");

        Double score = scanner.nextDouble();

        if(score == 100){
            System.out.println("恭喜满分!");
        }else if (score<100 && score >=90){
            System.out.println("A级");
        }else if (score<90 && score >=80){
            System.out.println("B级");
        }else if (score<80 && score >=70){
            System.out.println("C级");
        }else if (score<70 && score >=60){
            System.out.println("D级");
        }else if (score<60 && score >=0){
            System.out.println("不及格");
        }else {
            System.out.println("成绩不合法");
        }
        scanner.close();
    }
}

if嵌套结构

if(布尔表达式1){
    //如果布尔表达式1为true执行语句
    if(布尔表达式2){     
        //如果布尔表达式2为true执行语句   
    }
}

switch多选择结构

switch case语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。

switch语句中的变量类型可以是byte,short,int,char或String。

case标签必须为字符串常量或字面量。

switch(expression){
    case value:
        //语句
        breake; //可选
    case value:
        //语句
        breake; //可选
    //可以有任意数量的case语句
    dafault://可选
        //语句
}
package com.cheng.struct;

public class SwitchDemo01 {
    public static void main(String[] args) {
        //case穿透现象,没有break会一直执行
        //switch匹配一个具体的值
        
        char grade = 'C';

        switch (grade){
            case 'A':
                System.out.println("优秀");
                break;
            case 'B':
                System.out.println("良好");
                break;
            case 'C':
                System.out.println("及格");
                break;
            case 'D':
                System.out.println("再接再厉");
                break;
            case 'E':
                System.out.println("挂科");
                break;
            default:
                System.out.println("未知等级");

        }

    }
}
package com.cheng.struct;

public class SwitchDemo02 {
    public static void main(String[] args) {
        String name = "橙";
        //JDK的新特性,表达式结果可以是字符串!!!
        
        switch(name){
            case "朴海":
                System.out.println("朴海");
                break;
            case "橙":
                System.out.println("橙");
                break;
            default:
                System.out.println("弄啥嘞");
        }
    }
}

三、循环结构

  • while循环
  • do...while循环
  • for循环

while循环

  • while循环是最基本的循环,它的结构为
while ( 布尔表达式 ){
    //循环内容
}
  • 只要布尔表达式为true,循环就会一直执行下去。
  • 大多数情况下是会让循环停止下来的,我们需要一个让表达式失效的方式来结束循环。
  • 少部分情况需要循环一直执行,比如服务器请求响应监听等。
  • 循环条件一直为true就会造成无限循环【死循环】,我们正常的业务编程中应尽量避免死循环,否则会影响程序性能或者造成程序卡死崩溃。
package com.cheng.struct;

public class WhileDemo01 {
    public static void main(String[] args) {
        //输出1~100

        int i = 0;

        while(i < 100){
            i++;
            System.out.println(i);
        }
    }
}

死循环:

package com.cheng.struct;

public class WhileDemo02 {
    public static void main(String[] args) {
        //死循环
        while(true){
            //等待客户连接
            //定时检查
            //。。。。。
        }
    }
}

计算1+2+。。。100的和

package com.cheng.struct;

public class WhileDemo03 {
    public static void main(String[] args) {
        //计算1+2+。。。+100

        int i = 0;
        int sum = 0;

        while( i < 100){
            i++;
            sum +=i;
        }
        System.out.println("1到100的和为:"+sum);

    }
}

do...while循环

  • 对于while语言而言,如果不满足条件,则不能进入循环。但有时我们需要及时不满足条件,也要执行一次。

  • do...while循环和while循环相似,不同的是,do...while循环至少会执行一次

    do{
        //代码语句
    }while(布尔表达式);
    
  • while和do...while的区别

    • while先判断后执行。do...while先执行后判断。
    • do...while总是保证循环至少会被执行一次。
package com.cheng.struct;

public class DoWhileDemo01 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;

        do{
            sum = sum + i;
            i++;
        }while(i<=100);

        System.out.println(sum);
    }
}
package com.cheng.struct;

public class DoWhile02 {
    public static void main(String[] args) {
        int a = 0;

        while(a<0){
            System.out.println(a);
            a++;
        }

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

        do{
            System.out.println(a);
            a++;
        }while(a<0);
    }
}

For循环

  • for循环是支持迭代的一种通用结构,是最有效、最灵活的循环结构。

  • for循环执行的次数是在执行前就确定的。语法格式如下:

    for(初始化;布尔表达值;更新){
        //代码语句
    }
    
package com.cheng.struct;

public class ForDemo01 {
    public static void main(String[] args) {
        int a = 1;  //初始化条件

        while (a<=100){//条件判断
            System.out.println(a); // 循环体
            a += 2;//迭代
        }
        System.out.println("while循环结束!");


        //(初始化,条件判断,迭代)
        for(int i=1;i<=100;i++){
            System.out.println(i);
        }
        System.out.println("for循环结束!");
        
    }
}

注意

关于for循环有以下几点说明:

1、 最先执行初始化步骤。可以声明一种类型,可初始化一个或多个循环控制变量,也可以是空语句。

2、然后,检测布尔表达式的值。如果为true,循环体被执行。如果为false,循环终止,开始执行循环体后面的语句。

3、执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。

4、再次检测布尔表达式,循环执行上面的过程。

小扩展

在IDEA里直接输入数字.for回车,出现for循环。

for (int i = 0; i < 100; i++) {
    
}

练习1:计算0到100之间的奇数和与偶数和

package com.cheng.struct;

public class ForDemo02 {
    public static void main(String[] args) {
        //练习1:计算0到100之间的奇数和与偶数和

        int oddsum = 0;
        int evensum = 0;

        for (int i = 0; i <= 100; i++) {
            if(i%2!=0){
                oddsum +=i;
            }else{
                evensum +=i;
            }
        }
        System.out.println("奇数的和:"+oddsum);
        System.out.println("偶数的和:"+evensum);
    }

}

练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个

package com.cheng.struct;

public class ForDemo03 {
    public static void main(String[] args) {
        //用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个

        for (int i = 1; i <= 1000; i++) {
            if(i%5 == 0){
                System.out.print(i+"\t");//使用转义字符\t打一个Tab键
            }
            if(i%(5*3) == 0){   //换行
                System.out.println();
                //System.out.print("\n");//使用转义字符\n换行
            }
        }
        
        //println  输出完会换行
        //print    输出完不会换行
    }
}
package com.cheng.struct;

public class test {
    public static void main(String[] args) {
        int i = 1;

        while(i<=1000){
            if(i%5 == 0){
                System.out.print(i+"\t");
            }
            if(i%(5*3) == 0){
                System.out.println();
                //System.out.print("\n");
            }
            i++;
        }
    }
}

练习3:打印九九乘法表

步骤一:先打印第一列。

for (int i = 1; i <= 9; i++) {
    System.out.println(1+"*"+i+"="+(1*i));
}

步骤二:把固定值的1用一个循环包起来

for (int j = 1; j <= 9; j++) {
    for (int i = 1; i <= 9; i++) {
        System.out.println(j+"*"+i+"="+(j*i));
    }
}

步骤三:去掉重复项,i<=j

for (int j = 1; j <= 9; j++) {
    for (int i = 1; i <= j; i++) {
        System.out.println(j+"*"+i+"="+(j*i));
    }
}

步骤四:调整样式:每一行之后需要加一个Tab;每跑完一行需要换行。

package com.cheng.struct;
/*
1*1=1
2*1=2  2*2=4
3*1=3  3*2=6  3*3=9
4*1=4  4*2=8  4*3=12 4*4=16
5*1=5  5*2=10 5*3=15 5*4=20 5*5=25
6*1=6  6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7  7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8  8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9  9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
 */


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

        for (int j = 1; j <= 9; j++) {
            for (int i = 1; i <= j; i++) {
                System.out.print(j+"*"+i+"="+(j*i)+"\t");
            }
            System.out.println();
        }
    }
}

增强For循环

  • Java5引用了一种主要用于数组集合的增强性for循环

  • 格式如下

    for(声明语句:表达式){
        //代码句子
    }
    
  • 声明语句:声明新的局部变量,该变量的类型必须和数组元素类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。

  • 表达式:表达式是要访问的数组名,或者是返回值为数组的方法。

package com.cheng.struct;

public class ForDemo05 {
    public static void main(String[] args) {
        int[] numbers = {10,20,30,40,50};//定义了一个数组

        for(int i=0;i < 5;i++){//数组的第一个下标为0
            System.out.println(numbers[i]);
        }

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

        //遍历数组的元素
        for (int x:numbers){
            System.out.println(x);
        }
    }
}

break

在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。(breake语句也可在switch语句中使用)

package com.cheng.struct;

public class BreakDemo {
    public static void main(String[] args) {
        int i = 0;

        while(i<100){
            i++;
            System.out.println(i);
            if(i==30){
                break;
            }
        }

        System.out.println("被终止!");
    }
}

continue

用在循环语句中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定。

package com.cheng.struct;

public class ContinueDemo {
    public static void main(String[] args) {
        int i = 0;

        while(i<100){
            i++;
            if(i%10==0){
                System.out.println();
                continue;
            }
            System.out.print(i);
        }
    }
}

goto(了解即可,Java不用)

  • goto关键字很早就在程序设计语言中出现。尽管goto仍是Java的一个保留字,但并未在语言中得到正式使用;Java没有goto。然而,在break和continue这两个关键字的身上,我们仍然能看出一些goto的影子——带标签的break和continue。
  • “标签”是指后面跟一个冒号的标识符,例如:label:
  • 对Java来说唯一用到标签的地方是在循环语句之前,而在循环之前设置标签的唯一理由是:我们希望在其中嵌套另一个循环,由于break和continue关键字通常只终端当前循环,但若随同标签使用,它们就会中断到存在标签的地方。
package com.cheng.struct;

public class LabelDemo {
    public static void main(String[] args) {
        //打印101~150之间所有的质数

        int count = 0;

        //不建议使用
        outer:for(int i=101;i<150;i++){
            for(int j=2;j<i/2;j++){
                if(i%j == 0){
                    continue outer;
                }
            }
            System.out.print(i+" ");
        }
    }
}
posted @ 2022-03-29 16:21  lauMonkey  阅读(37)  评论(0)    收藏  举报