Java复习笔记(四)流程控制语句

流程控制语句

1. 顺序语句

语句: 使用分号分隔的代码就是一个语句。
顺序语句: 按照代码顺序从上往下执行所有的代码就是顺序语句。

class Demo1 {
    public static void main(String[] args) 
    {
        System.out.println("A");
        System.out.println("B");
        System.out.println("C");
        System.out.println("D");
    }
}

语句自上而下顺序执行是一般语言的基本规则

2. if 判断语句

if判断语句的格式:

格式1:适用于一种情况使用。

if(判断的条件){
    符合条件执行的代码;      
}

格式2 : 适用于两种情况下去使用的。

if(判断条件){
    符合条件执行的代码
}
else{
    不符合条件执行的代码;
}

与 if else 结构相似的三元运算符
三元运算符的格式:布尔表达式?值1:值2;

public class Main{
    public static void main(String[] args){
        String answer;
        String str1 = "A";
        String str2 = "B";
        answer = str1==str2 ?"same":"different";
        System.out.print(answer);
    }
}

三元运算符的优点: 结构比较简洁。
三元运算符的缺点: 符合条件必须返回一个结果,不能执行语句。

格式3 : 适用于两种以上的情况下去使用的。

if(判断条件1){
    符合条件1执行的代码
}
else if(判断条件2){
    符合条件2执行的代码;
}
...
else if(判断条件n){
    符合条件n执行的代码;
}
else{
    不符合条件执行的代码;
}

if 语句要注意的细节:

  1. 如果符合条件后只有一个语句需要执行,那么可以省略大括号。但是建议不要省略,因为结构不清晰。
  2. if语句的判断条件后不能添加分号,否则待执行代码就不会执行。
public class Main{
    public static void main(String[] args){
        int day = (int)(Math.random()*7)+1;
        if(day==0){
            System.out.println("星期天");
        }else if(day==1){
            System.out.println("星期一");
        }else if(day==2){
            System.out.println("星期二");
        }else if(day==3){
            System.out.println("星期三");
        }else if(day==4){
            System.out.println("星期四");
        }else if(day==5){
            System.out.println("星期五");
        }else if(day==6){
            System.out.println("星期六");
        }else{
            System.out.println("没有对应的星期");
        }
    }
}

3. switch 判断语句

switch 判断语句的格式:

switch(表达式)
{
case 取值1:
    执行语句;
    break;
case 取值2:
    执行语句;
    break;
......
default:
    执行语句;
    break;
}

switch的使用规则:

(1)switch语句选择的类型有四种:byte,short,int , char;JDK5.0增加了枚举类型,JDK7.0增加了字符串类型的数据。
(2)case之间与default没有顺序。先判断所有的case,没有匹配的case执行default。
(3)switch语句停止的条件是遇到了break关键字或者结束switch语句的大括号。
(4)如果匹配的case或者default没有对应的break,那么程序会继续向下执行,运行可以执行的语句,直到遇到break或者switch结尾结束。
(5)switch case中的值必须要与switch表达式的值具有相同的数据类型。而且case后跟的值必须是常量,不能跟变量。

//标准使用
public class Mian {
    public static void main(String[] args){
        String str1 = "chenjipayne";
        switch(str1){
            case "zhanchangyuan":
                System.out.println("大佬战场原");
                break;
            case "luwenjun":
                System.out.println("壮汉陆文俊凌");
                break;
            case"chenjipayne":
                System.out.println("弱鸡沉寂佩恩");
                break;
            default:
                System.out.println("查无此人");
                break;
        }
    }
}
//缺少break的情况
import java.util.Scanner;
public class Mian {
    public static void main(String[] args)
    {
        System.out.println("请输入一个月份:");
        Scanner scanner = new Scanner(System.in);
        int month = scanner.nextInt();
        switch(month){
            case 3:
            case 4:
            case 5:
                System.out.println("春天");
            case 6:
            case 7:
            case 8:
                System.out.println("夏天");
            case 9:
            case 10:
            case 11:
                System.out.println("秋天");
            case 12:
            case 1:
            case 2:
                System.out.println("冬天");
            default:
                System.out.println("没有对应的季节");
        }
    }
}
//运行结果
请输入一个月份:
6
夏天
秋天
冬天
没有对应的季节

4. while & do while 循环语句

while循环语句的格式:

while(循环的条件){
    循环语句;
}

while循环语句要注意的事项:
(1)while循环语句一般是通过一个变量控制其循环的次数。
(2) while循环语句的循环体代码如果只有一个语句的时候,那么可以省略大括号。但是也是不建议省略。
(3) while循环语句的判断条件后面不能跟有分号,否则待执行代码就不会执行。

public class Mian {
    public static void main(String[] args)
    {
        int count = 0;
        while(count<5){
            System.out.println("hello chenjipayne!");
            count++;
        }
    }
}
//运行结果:
hello chenjipayne!
hello chenjipayne!
hello chenjipayne!
hello chenjipayne!
hello chenjipayne!

do while循环语句格式:

do{
    循环语句;
}while(判断条件);
public class Mian {
    public static void main(String[] args)
    {
        int count = 0;
        do{
            System.out.println("hello chenjipayne!");
            count++;
        }
        while(count<5);
    }
}
//运行结果:
hello chenjipayne!
hello chenjipayne!
hello chenjipayne!
hello chenjipayne!
hello chenjipayne!

while与do-while的区别:
(1)while循环语句是先判断后执行循环语句的;
(2)do-while循环语句是先执行,后判断。不管条件是否满足至少会执行一次。

//当条件一开始就不满足时,while()语句
public class Mian {
    public static void main(String[] args)
    {
        int count = 0;
        while(count<0){
            System.out.println("hello chenjipayne!");
            count++;
        }
    }
}
//无输出

//当条件一开始就不满足时,do while()语句
public class Mian {
    public static void main(String[] args)
    {
        int count = 0;
        do{
            System.out.println("hello chenjipayne!");
            count++;
        }
        while(count<0);
    }
}
//输出:hello chenjipayne!

5. for 循环语句

for循环语句的格式:

for(初始化语句;判断语句;循环后的语句){
    循环语句;
}

for循环语句 要注意的事项:
(1) for(;;)这种写法 是一个死循环语句,相当于while(true);
(2) for循环语句的初始化语句只会执行一次,只是在第一次循环的时候执行而已。
(3) for循环语句的循环体语句只有一句的时候,可以省略大括号不写。但是不建议省略。

//求1~100的整数和
public class Mian {
    public static void main(String[] args)
    {
        int and = 0;
        for(int i = 1; i <= 100; i++)
        {
            and += i;
        }
        System.out.println(and);
    }
}

for循环的括号中的语句除判断条件外,全部可以写到外部

//求1~100的整数和
public class Mian {
    public static void main(String[] args)
    {
        int and = 0;
        int i = 1;//初始化语句
        for( ; i <= 100; )
        {
            and += i;
            i++;//循环后的语句
        }
        System.out.println(and);
    }
}

for(;;)这种写法用来制造死循环

public class Mian {
    public static void main(String[] args)
    {
        for( ; ; )
        {
            System.out.println("hello chenjipayne!");
        }
    }
}

6. break & continue 关键字

(1) break

break适用范围:只能用于switch或者是循环语句中。

break作用:
1. break用于switch语句的作用是结束一个switch语句。
2. break用于循环语句中的作用是结束当前所在的循环语句。

笔试题目:break目前位于内层的for循环,如何才能让break作用于外层 的for循环。
解:可以标记解决

public class Mian {
    public static void main(String[] args) 
    {
        //标记的命名只要符合标识符的命名规则即可。
        aaa:for(int j = 0 ; j<100 ; j++)// j=0 外层for循环
        {
            bbb:for(int i = 0 ; i< 100 ; i++)// i=0 内层for循环
            {
                System.out.println("hello world"); 
                break aaa;
            }       
        }
    }
}
(2) continue

continue适用范围: continue只能用于循环语句。

continue作用:continue的作用是跳过本次的循环体内容,继续下一次。

continue要注意的事项:
1. continue后面不能跟有其他语句,因为永远都无法执行到。
2. continue 也可以配合标记使用的。

//计算1-100的偶数总和.
public class Mian {
    public static void main(String[] args) 
    {   
        int sum = 0 ;
        for(int num = 1 ; num <= 100 ; num++){
            if(num%2!=0){
                continue;  //如果是奇数就跳过本次循环。
            }
            sum  = sum+num;
        }
        System.out.println("总和:"+ sum);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_29615991/article/details/80048532