Java基础流程控制(条件语句 循环语句 跳转语句)

条件语句: 

 可根据不同的条件执行不同的语句,包括if条件语句与switch多分支语句。

1.if语句:

if(布尔表达式) { 将执行的语句 }

如果该表达式返回true,则执行其后的语句;若为false,则不执行if后的语句

public static void main(String[] args) {
    int a = 10;
    String name = "郑州";
    if (a == 10) {
        System.out.println("if表达式");
    }
    //String 类型比较 使用equals()方法
    if (name.equals("郑州"))//{}可以省略 省略后格式要对齐 开发中为避免出错,一般不省略
        System.out.println("地址是:"+name);
}

2.if···else语句

if(布尔表达式){ 布尔表达式的值为true将执行的语句 }

else{ 布尔表达式的值为false将执行的语句 }

如果该表达式返回true,则执行if后的语句;如果返回为false,则执行else后的语句

public static void main(String[] args) {
    int a = 11;
    String name = "河南郑州";
    if (a == 10) {
        System.out.println("if表达式");
    }else { System.out.println("if else表达式");}
    //String 类型比较 使用equals()方法
    if (name.equals("郑州")){
        System.out.println("地址是:"+name);
    }
    else {
        System.out.println("地址不是:" + name);

   }

3.if···else if多分支语句

if(布尔表达式 1){布尔表达式 1的值为true执行代码 }

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

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

...

...

else {以上布尔表达式都不为true执行代码 }

如果满足某种条件,就进行某种处理,否则,如果满足另一种条件,则进行另一种处理。

if 语句至多有 1 个 else 语句,else 语句在所有的 else if 语句之后。

if 语句可以有若干个 else if 语句,它们必须在 else 语句之前。

一旦其中一个 else if 语句检测为 true,其他的 else if 以及 else 语句都将跳过执行

public static void main(String[] args) {
    int num = 3;
    if (num == 1) {
        System.out.println("今天星期一");
    } else if (num == 2) {
        System.out.println("今天星期二");
    } else if (num == 3) {
        System.out.println("今天星期三");
    } else if (num == 4) {
        System.out.println("今天星期四");
    } else if (num == 5) {
        System.out.println("今天星期五");
    } else if (num == 6) {
        System.out.println("今天星期六");
    } else if (num == 7) {
        System.out.println("今天星期日");
    } else {
        System.out.println("这是else语句");
    }

}

4.嵌套的 if…else 语句

if(布尔表达式 1){布尔表达式 1的值为true执行代码 

if(布尔表达式 2){布尔表达式 2的值为true执行代码 }

else{ 布尔表达式 2的值为false将执行的语句 }

 }else{ 布尔表达式 1的值为false将执行的语句 }

如果满足某种条件,就进行某种处理,再处理时又满足另一种条件,则进行另一种处理。

public static void main(String[] args) {
    int a = 7;
    if (a > 5) {
        System.out.println("if 表达式");
        if (a == 7) {
            System.out.println("if if 表达式");
        } else {
            System.out.println("if else 表达式");
        }
    } else {
        System.out.println("else 表达式");
    }
}

5.switch多分支语句

switch(expression){

case value : 语句

break;

case value : 语句

 break;

....

....

default :  语句

break;  

}

switch语句是一种比较简单明了的多选一的选择,在Java语言中,可以用switch语句将动作组织起来进行多选一。

switch 语句中的变量类型可以是: byte、short、int 或者 char。从 Java SE 7 开始,switch 支持字符串 String 类型了,同时 case 标签必须为字符串常量或字面量。

switch 语句可以拥有多个 case 语句。每个 case 后面跟一个要比较的值和冒号。

case 语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。

当变量的值与 case 语句的值相等时,那么 case 语句之后的语句开始执行,直到 break 语句出现才会跳出 switch 语句。

当遇到 break 语句时,switch 语句终止。程序跳转到 switch 语句后面的语句执行。case 语句不必须要包含 break 语句。如果没有 break 语句出现,程序会继续执行下一条 case 语句,直到出现 break 语句。

switch 语句可以包含一个 default 分支,该分支一般是 switch 语句的最后一个分支(可以在任何位置,但建议在最后一个)。default 在没有 case 语句的值和变量值相等的时候执行。default 分支不需要 break 语句。

public static void main(String[] args) {
    int num = 3;
    switch (num) {
        case 1:
            System.out.println("今天星期一");
            break;//跳出不再执行
        case 2:
            System.out.println("今天星期二");
            break;
        case 3:
            System.out.println("今天星期三");
            //break; 省略后会向后执行
        case 4:
            System.out.println("今天星期四");
            break;
        case 5:
            System.out.println("今天星期五");
            break;
        case 6:
            System.out.println("今天星期六");
            break;
        case 7:
            System.out.println("今天星期日");
            break;
        default://所有 case条件都不符合
            System.out.println("这是switch语句");

break;//default在最后执行 break可以省略
 
    }

}

循环语句: 

就是在满足一定条件的情况下反复执行某一个操作,包括while循环语句、do···while循环语句和for循环语句。

 1.while循环语句

while(布尔表达式) { 循环内容 }

只要布尔表达式为 true,循环就会一直执行下去。

public static void main(String[] args) {
    int num = 3;
    while (num < 10) {
        num++;//改变条件变量进行 否则进入无限(死)循环
        System.out.println(num);
    }
}

2.do···while循环语句

do{ 循环内容 }while(布尔表达式)

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

while循环语句先判断条件是否成立再执行循环体,而do···while循环语句则先执行一次循环后,再判断条件是否成立。也即do···while至少执行一次。

public static void main(String[] args) {
    int num = 3;
    do {
        num++;//改变条件变量进行 否则进入无限(死)循环
        System.out.println(num);
    } while (num == 3);
}

3.for循环语句

for(初始化; 布尔表达式; 更新) { 代码语句 }

for循环可以用来重复执行某条语句,直到某个条件得到满足

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

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

执行一次循环后,更新循环控制变量。

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

public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
        System.out.println("第" + i + "次循环");
    }
}

4.增强 for 循环

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

声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。

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

public static void main(String[] args) {
    System.out.println("<===========int===========>");
    int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    for (int x : nums) {
        System.out.println(x);
    }
    System.out.println("<===========String===========>");
    String[] names = {"郑州", "洛阳", "开封", "周口"};
    for (String name : names) {
        System.out.println(name);
    }
}

跳转语句:

用于实现循环执行过程中程序流程的跳转,Java语言提供了三种跳转语句,分别是break语句、continue语句和return语句。

1.break语句

break ;

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

public static void main(String[] args) {
    String[] names = {"郑州", "洛阳", "开封", "周口"};
    for (String name : names) {
        if (name.equals("洛阳")) {
            break;
        }
        System.out.println(name);
    }
}

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

public static void main(String[] args) {
    for (int i = 0; i < 5; i++) {    // 外层循环
     System.out.println("这是第" + i + "次循环");
      for (int j = 0; j < 5; j++) {// 内层循环
        System.out.println("这是第" + i + "次循环中第" + j + "次循环");
            if (j == 2) {
                break;
            }
        }
    }
}

break加标记可以跳出指定循环

public static void main(String[] args) {
   boolean flag = false;//标记
   for (int i = 0; i < 5; i++) {    // 外层循环
     System.out.println("这是第" + i + "次循环");
      for (int j = 0; j < 5; j++) {// 内层循环
        System.out.println("这是第" + i + "次循环中第" + j + "次循环");
            if (j == 2) {
                flag = true;
            }
        }
        if (flag) {
            break;
        }
    }
}

break语句在for、while、do···while循环语句中,用于强行退出当前循环,因为break只能跳出离它最近的那个循环的循环体,有两个循环嵌套使用,break用在内层循环下,则break只能跳出内层循环

2.continue语句

continue;

continue 适用于任何循环控制结构中。作用是让程序立刻跳转到下一次循环的迭代。

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

public static void main(String[] args) {
    int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    for (int x : nums) {
        if (x == 4) {
            continue;
        }
        System.out.println(x);
    }
}

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

public static void main(String[] args) {
    System.out.println("输出10以内的所有偶数");
    int i = 0;
    while (i < 10) {
        i++;
        if (i % 2 != 0) {    // 不能被2整除,是奇数
            continue;    // 跳过当前循环
        }
        System.out.println(i);
    }
}

3.return语句

return;

return语句可以从一个方法返回值,并把控制权交给调用它的语句。

static int sum(int x,int y){
    return x+y;//返回结果
}
public static void main(String[] args) {
  int sum =  sum(5,9);
    System.out.println(sum);
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Java之眼

创作不易,一起努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值