Java核心知识:流程控制

判断语句

1.形式一

if (关系表达式) {
	语句体;
}

若果关系表达式为true执行语句体,否则不执行。

2.形式二

使用if-else:

if (关系表达式) {
  语句体1;
} else {
  语句体2;
}

例子:

public class IfElse1 {
    public static void main(String[] args) {
        int num = 9;
        if (num % 2 == 0) {
            System.out.println("偶数");
        } else {
            System.out.println("奇数");
        }


        // 使用if-else实现三元运算符
        int a = 5;
        int b = 6;
        int max;
        if (a > b) {
            max = a;
        } else {
            max = b;
        }
        System.out.println(max);  // 6
    }
}

3.形式3

if-elif-else

if (关系表达式) {
  语句体1;
} else if {
  语句体2;
} 
...
else if {
  语句体n;
} else {
  语句体n+1;
}

选择语句

switch语句格式:

switch (表达式) {
   case 常量值1:
    	语句体1;
    	break;
   case 常量值2:
    	语句体2;
    	break;
    ...
    default:
    	语句体n;
    	break;
}

匹配哪一个case就从哪一个位置向下执行,直到遇到break或程序执行结束。

注意:

1.多个case后面的数值不能重复

2.switch后面数值只能是下列数据类型:byte/short/char/int,String和enum

循环语句

for循环

格式:

for (初始化表达式; 布尔表达式; 步进表达式) {
  循环体;
}

例子:

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

while循环

格式:

while (条件表达式) {
  循环体;
}

// 格式2
初始化语句;
while (条件表达式) {
  循环体;
  步进语句;
}

例子:

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

do-while循环

格式:

do {
  循环体;
  步进表达式;
} while (布尔表达式)

注意:

1.do-while一定会执行一次,然后再判断

例子:

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

break和continue

break:在循环中,终止循环

continue:在循环中,跳过本次循环

例子:

public class BreakContinue {
    public static void main(String[] args) {
        // break
        int i = 1;
        while (true) {
            if (i > 5) {
                break;
            }
            System.out.println(i);
            i ++;
        }

        System.out.println("-----------");
        // continue,只打印奇数
        for (int m = 1; m < 10; m ++){
            if (m % 2 == 0) {
                continue;
            }
            System.out.println(m);
        }

    }
}

死循环

一个循环永远停不下来,比如布尔表达式永远成立。

格式:

while (true) {
  循环体;
}

注意:死循环后面不能再写语句,否则报不能到达的语句。

循环嵌套

一个循环里还有循环

格式:

for (初始化表达式1; 循环条件; 步进表达式) {
  for (初始化表达式1; 循环条件; 步进表达式) {
  	执行语句;
	}
}

例子,打印乘法表:

public class NestingLoop {
    public static void main(String[] args) {
        for (int i = 1; i < 10; i ++) {
            for (int j = 1; j <= i; j ++) {
                int res = i * j;
                if (i == j) {
                    System.out.println(String.format("%sx%s=%s", i, j, res));
                } else {
                    System.out.print(String.format("%sx%s=%s ", i, j, res));
                }
            }
        }
    }
}

结果如下:

1x1=1
2x1=2 2x2=4
3x1=3 3x2=6 3x3=9
4x1=4 4x2=8 4x3=12 4x4=16
5x1=5 5x2=10 5x3=15 5x4=20 5x5=25
6x1=6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36
7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49
8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64
9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Ethan-running

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值