java条件控制语句、循环结构

2021/4/27 20:55:22

本文主要是介绍java条件控制语句、循环结构,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

条件控制语句

if 语句

  • if格式1:

    if(…) {
    true进入 if 语句体
    }

  • if格式2:

    if(…) {
    true进入 if 语句体
    }else {
    false进入 else 语句体
    }

  • if格式3:

    if(…) {
    当前true进入 if 语句体
    }else if (…) {
    当前true进入 if 语句体
    }else if (…) {
    当前true进入 if 语句体
    }…
    else {
    所有的if或者else if 都不满足则进入 else
    }

  • 格式3和多个if的区别

  • 格式3是分支流程,最终只会进入一个if

  • 多个if时只要满足条件就能进入if,不存在互斥

switch 语句

switch(值) {
case 值1:
语句;
break;
case 值2:
语句;
break;
case 值3:
语句;
break;
default:
语句;
break;
}

  • switch(值)值的类型:byte、short、int、char、String、枚举
  • switch遇到break和 } 结束
  • break穿透:没有遇到break,不会再次进行case匹配

if 和 switch 的区别

  • if的条件更加丰富,只要表达式结果是boolean类型就ok
  • switch只是一个值的比较,switch需要匹配的值是一个范围时,很难用
  • switch只比较值是否相等,且数量较少的时候,性能高过if
  • 95%情况下都使用if

循环结构

  • for循环

    for(初始化语句1; 条件判断语句 2; 循环体执行之后的语句4) {
    循环体 3 ;
    }

  • for循环执行流程:1 --> 2 --> 3 --> 4 --> 2 --> 3 --> 4 --> … false 结束

  • 求和思想

  • 增强for循环

    for(数据类型 变量名 : 数组或者集合名) {
    循环体
    }

  • 增强for循环好处:代码简单;弊端:只能按顺序挨个遍历,不能直接获取索引

  • for嵌套循环

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

  • 循环控制语句

  • continue; //跳出本次循环,继续下次循环

  • break; //跳出全部循环

  • while 循环

    初始化语句 1
    while(条件判断语句2) {
    循环体3;
    循环体执行之后的语句4;
    }

  • while循环执行流程:1 --> 2 --> 3 --> 4 --> 2 --> 3 --> 4 --> … false 结束

  • do…while 循环

    初始化语句 1
    do {
    循环体3;
    循环体执行之后的语句4;
    }while(条件判断语句2);

  • do while 至少会执行一次循环体

public class ifDemo {
    public static void main(String[] args) {
        //if格式1:
        int age = 20;
        if(age>=18){
            System.out.println("你成年了");
            if(age>=30){
                //if语句体
                System.out.println("而立之年");
            }
        }
        System.out.println("end......");
        //if格式2:
        String username = "admin";
        String password = "admin";
        if(username == "admin" && password == "admin"){
            System.out.println("登录成功");
        }else{
            System.out.println("登录失败");
        }
        System.out.println("end......");
        //if格式3:多分支结构
        int score = 100;
        if(score <= 100 && score >= 80){
            System.out.println("优秀");
        }else if(score < 80 && score >= 60){
            System.out.println("及格");
        }else if(score < 60 && score >= 10){
            System.out.println("学了点东西");
        }else if(score < 10 && score >= 0){
            System.out.println("勇士");
        }else{
            System.out.println("输入分数有问题");
        }
        System.out.println("end......");
        //多个if语句
        score = 20;
        if(score <= 100 && score >= 80){
            System.out.println("优秀");
        }
        if(score < 80 && score >= 60){
            System.out.println("及格");
        }
        if(score < 60 && score >= 10){
            System.out.println("学了点东西");
        }
        if(score < 10 && score >= 0){
            System.out.println("勇士");
        }else{
            System.out.println("输入分数有问题");
        }
        System.out.println("end......");
    }
}
public class SwitchDemo {
    public static void main(String[] args) {
        int month = 6;
        switch(month){
            case 1:
                System.out.println("1月");
                break;
            case 2:
                System.out.println("2月");
                break;
            case 3:
                System.out.println("3月");
                break;
            case 6:
                System.out.println("6月");
                break;
            default:
                System.out.println("输入月份有问题");
                break;
        }
        System.out.println("end.....");
    }
}
public class ForPlusDemo {
    public static void main(String[] args) {
        int[] arr = {22, 3, 4, 5, 61, 111};
        //for循环
        for(int i=0; i<arr.length; i++){
            System.out.println(arr[i]);
        }
        System.out.println("--------");
        //增强for循环
        //好处:代码简单; 弊端:只能按顺序挨个遍历,不能直接获取索引
        for(int element : arr){
            System.out.println(element);
        }
    }
}
public class ForForDemo {
    public static void main(String[] args) {
        //for嵌套循环
        for(int i=1; i<=4; i++){
            for(int j=1; j<=5; j++){
                System.out.print("*");
            }
            System.out.println();
        }
        int[][] arr = {{22, 33, 11}, {4, 5, 6}};
        for(int m=0; m<arr.length; m++){
            for(int n=0; n<arr[m].length; n++){
                System.out.print(arr[m][n] + " ");
            }
            System.out.println();
        }
    }
}
public class ForControlDemo {
    public static void main(String[] args) {
        //循环控制语句
        for(int i=1; i<=10; i++){
            if(i%3==0){
                //结束本次循环,继续下次循环
                //continue;
                //结束当前循环
                break;
            }
            System.out.println(i);
        }
    }
}
public class ForSumDemo {
    public static void main(String[] args) {
        //求和
        int sum = 0;
        for(int i=1; i<=5; i++){
            System.out.println(i);
            sum += i;
        }
        //变量作用域:包含变量的大括号
        System.out.println(sum);
    }
}


这篇关于java条件控制语句、循环结构的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程