java基础之 控制语句

java基础之控制语句

在java中控制语句包括:

  1. if-else语句
  2. swicth-case 语句
  3. while循环
  4. do-while循环
  5. for循环
  6. break语句
  7. continue语句

其中:if-else、swicth-case 属于条件结构,while、do-while、for属于循环结构,break和continue可用在swicth-case和循环结构中。

一、if-else语句

if-else主要用于判断条件,根据不同的条件执行不同的代码。

在java中有各种类型的if-else语句,大致分为:

  • if 语句
  • if-else 语句
  • 嵌套 if 语句
  • if-else-if 语句

语法

if (boolean-expression) {
    // boolean-expression 为 true 时执行
} else {
    // boolean-expression 为 false 时执行
}

举个栗子

package test;
public class IfElse {
    public static void main(String[] args) {
        IfElse ifElse = new IfElse();
        ifElse.isShow(true);
    }
    public void isShow(Boolean bool) {
        if (bool) {
            System.out.println("斯国一斯国一");
        } else {
            System.out.println("笨蛋笨蛋");
        }
    }
}
// 结果:斯国一斯国一

扩展

if 语句、嵌套 if 语句、if-else-if 语句,这些语句都是 if-else 语句 的变种,和它的使用方法一致,

举个栗子:

package test;

public class IfElse {
    public static void main(String[] args) {
        IfElse ifElse = new IfElse();
        ifElse.ifelse(1000);
    }
    public void ifelse(int num) {
        if (num < 0) {
            System.out.print("这个是代把的数字");
        } else if (num == 0) {
            System.out.print("这个是圈");
        } else {
            System.out.print("这个数字好大啊");
            if (num > 999) {
                System.out.print("是我一个的存款【做梦ing】");
            }
        }
    }
}
// 结果:这个数字好大啊是我一个的存款【做梦ing】

二、swicth-case 语句

根据表达式的值,swicth语句从一系列case中选出一个去执行。

语法

switch (expression) {
    case value1 : statement1; break;
    case value2 : statement2; break;
    case value3 : statement3; break;
    default: statement;
}

规则:

  1. expression是选择因子,类型可以为: byte、short、int 、char 、String(1.7+)。
  2. case 紧跟的value是值,其值不能是变量。
  3. 当expression和value匹配时,进入对应的statement语句块。
  4. break是非必须的,如果不写,将会执行下一个case,直到遇到break为止。
  5. switch中可以包含一个default语句,如果expression不符合任何的case条件,将会执行default语句后面的语句块。

举个栗子

package test;
public class SwitchCase {
    public static void main(String[] args) {
        SwitchCase switchCase = new SwitchCase();
        switchCase.nameToRgba("red");
    }
    public void nameToRgba(String colorName) {
        switch (colorName) {
            case "blue":
                System.out.println("胖次蓝");
                break;
            case "red":
                System.out.println("姨妈红");
                break;
            case "white":
                System.out.println("纯白");
                break;
            default:
                System.out.println("未知颜色");
        }
    }
}
// 结果:姨妈红

扩展

如果 case 代码块中没有 break 语句时,并不会顺序执行每一个 case 对应的代码块,而是继续匹配,匹配不成功则返回默认 case。匹配成功将执行代码块,如果匹配的case没有break语句将会顺序执行后面的case代码块,直到遇到break语句为止。

举个栗子

package test;
public class SwitchCase {
    public static void main(String[] args) {
        SwitchCase switchCase = new SwitchCase();
        switchCase.nameToRgba("red");
    }
    public void nameToRgba(String colorName) {
        switch (colorName) {
            case "blue":
                System.out.print("胖次蓝 ");
            case "red":
                System.out.print("姨妈红 ");
            case "white":
                System.out.print("纯白 ");
                break;
            case "black":
                System.out.print("黑夜 ");
            default:
                System.out.print("未知颜色");
        }
    }
}
// 结果:姨妈红 纯白 

三、while和do-while

while、do-while和for都是用来控制循环的。用这些语句的代码块会重复执行,至到起控制作用的布尔表达式为“fasle”为止。

其中while循环的格式如下:

while (expression) {
    statement
}

规则

在循环开始时会先计算一遍expression表达式的值,成立才会进入statement循环体;在下一次循环开始前会再一次计算,直到计算出的值为“假”为止。

举个栗子

package test;
public class While {
    public static void main(String[] args) {
        While w = new While();
        w.whileTest();
    }
    public void whileTest() {
        int i = 1;
        while (i <= 100) {
            System.out.println(i++);
        }
    }
}
// 结果:输出 1 到 100

do-while 循环会先执行一次循环体的内容,然后第二次才会进行条件判断,do…while 循环和 while 循环相似,不同的是,do…while 循环至少会执行一次。语法规则如下:

do{
    statement
} while (expression);

举个栗子

package test;
public class While {
    public static void main(String[] args) {
        While w = new While();
        w.doWhileTest();
    }
    public void doWhileTest() {
        int i = 0;
        do {
            System.out.println(++i);
        } while (i < 100);
    }
}
// 结果:输出 1 到 100

四、for循环

for循环可能是最常用的迭代数据的形式,在第一次迭代时进行初始化,然后进行条件测试,在每次条件结束时进行某种形式的“进步”。for循环的格式如下:

for (init; expression; setp) {
    statement;
}

规则

for循环常用执行“计数”任务,在执行前就确定执行次数。

init、expression、setp都可以为空,每次迭代都会判断expression是否为false,如果是则执行statement代码,在执行完成时会执行一次setp,如果判断expression是否为true时会结束迭代。

举个栗子

package test;
public class ForTest {
    public static void main(String[] args) {
        ForTest forTest = new ForTest();
        forTest.print99();
    }
    //打印 9*9 乘法表
    public void print99() {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + "x" + i + "=" + (j * i) + "\t");
            }
            System.out.println();
        }
    }
}
// 结果:
// 1x1=1    
// 1x2=2    2x2=4   
// 1x3=3    2x3=6   3x3=9   
// 1x4=4    2x4=8   3x4=12  4x4=16  
// 1x5=5    2x5=10  3x5=15  4x5=20  5x5=25  
// 1x6=6    2x6=12  3x6=18  4x6=24  5x6=30  6x6=36  
// 1x7=7    2x7=14  3x7=21  4x7=28  5x7=35  6x7=42  7x7=49  
// 1x8=8    2x8=16  3x8=24  4x8=32  5x8=40  6x8=48  7x8=56  8x8=64  
// 1x9=9    2x9=18  3x9=27  4x9=36  5x9=45  6x9=54  7x9=63  8x9=72  9x9=81

扩展

在for循环的init和setp可以使用由逗号隔开的语句,这些语句会独立执行。通过使用逗号操作符,可以在for循环内定义多个变量,但它们必须是同一种类型。

举个栗子

package test;
public class ForTest {
    public static void main(String[] args) {
        ForTest forTest = new ForTest();
        forTest.print66();
    }
    /**
     * 99乘法表向上对其
     */
    public void print66() {
        for (int i = 9; i > 0; i--) {
            for (int j = 1, k = 10 - i; j <= i; j++, k++) {
                System.out.print(j + "x" + k + "=" + (j * k) + "\t");
            }
            System.out.println();
        }
    }
}
// 结果:
// 1x1=1    2x2=4   3x3=9   4x4=16  5x5=25  6x6=36  7x7=49  8x8=64  9x9=81  
// 1x2=2    2x3=6   3x4=12  4x5=20  5x6=30  6x7=42  7x8=56  8x9=72  
// 1x3=3    2x4=8   3x5=15  4x6=24  5x7=35  6x8=48  7x9=63  
// 1x4=4    2x5=10  3x6=18  4x7=28  5x8=40  6x9=54  
// 1x5=5    2x6=12  3x7=21  4x8=32  5x9=45  
// 1x6=6    2x7=14  3x8=24  4x9=36  
// 1x7=7    2x8=16  3x9=27  
// 1x8=8    2x9=18  
// 1x9=9

增强for循环

增强for循环用于快速的迭代数组和容器,其格式如下:

for (variable : expression) {
    statement;
}

规则

variable循环内的局部变量,expression为数组、容器或者返回数组容器的表达式,在每次迭代时,可用variable这个局部变量来取出expression迭代内的项。

举个栗子

package test;
public class ForTest {
    public static void main(String[] args) throws InterruptedException {
        ForTest forTest = new ForTest();
        forTest.specialPrint();
    }
    /**
     * 每隔500毫秒打印一个字符
     */
    public void specialPrint() throws InterruptedException {
        String content = "每隔500毫秒打印一个字符。";
        for (Character str : content.toCharArray()) {
            Thread.sleep(500);
            System.out.print(str + "\t");
        }
    }
}
// 结果:每 隔   5   0   0   毫   秒   打   印   一   个   字   符   。

五、break 语句

在任何迭代语句的主体部分,都可以使用break语句来进行控制循环的流程。break语句用于强制退出循环,不执行循环中剩余的语句。语法格式如下:

break;

举个栗子

package test;
public class ForTest {
    public static void main(String[] args) throws InterruptedException {
        ForTest forTest = new ForTest();
        forTest.printFor();
    }
    /**
     * 在无穷for循环中退出
     */
    public void printFor() {
        int i = 0;
        for (; ; ) {
            i++;
            System.out.print(i + " ");
            // 循环10次后退出
            if (i >= 10) {
                break;
            }
        }
    }
}
// 结果:1 2 3 4 5 6 7 8 9 10 

六、continue语句

break语句用于强制结束本次循环,开始下一次循环。语法格式如下:

continue;

举个栗子

package test;
public class ForTest {
    public static void main(String[] args) throws InterruptedException {
        ForTest forTest = new ForTest();
        forTest.print10();
    }
    public void print10() {
        for (int i = 0; i < 10; i++) {
            // 被2整除的数不打印
            if (i % 2 == 0) {
                continue;
            }
            System.out.print(i + " ");
        }
    }
}
// 结果:1 3 5 7 9 

编码规范

根据阿里巴巴java开发手册说明:

【强制】在 if / esle / for / while / do 语句中,应该使用大括号 “{}”,避免使用单行编码。

【强制】在高并发场景中,避免使用“等于”作为中断或退出条件,防止条件被穿刺。

【强制】在使用 if else 时,为防止以后维护困难,不要超过3层。

【强制】在一个switch块内 ,每一个case要么通过break/return 等来终止,要么说明程序将继续执行到那个case为止;在一个switch块内,必须包含一个default语句并且放在最后,即使它是空的。

【强制】在循环体内避免进行定义变量、对象、获取数据库连接、进行try-catch等操作。

【推荐】不要在条件判断中执行复杂的判断语句,可将复杂的逻辑判断的结果赋予一个有意义的布尔变量,以提高可读性。

【推荐】避免使用反逻辑运算符,不利于快速理解。

转载于:https://www.cnblogs.com/lixingwu/p/10628960.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值