Java_Basic_Day03-流程控制语句

1.判断语句

1.1 if

格式:

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

1.2 if...else

格式:

if(关系表达式) {
   语句体1;
}else {
   语句体2;
}
public static void main(String[] args) {
        //定义变量
        int a = 2;
        if (a % 2 == 1){
            System.out.println("奇数");
        }else{
            System.out.println("偶数");
        }
    }

1.3 if...else if...else

格式:

if (判断条件1) {
   执行语句1;
} else if (判断条件2) {
   执行语句2;
}
   ...
}else if (判断条件n) {
   执行语句n;
} else {
   执行语句n+1;
}
public static void main(String[] args) {
        //定义变量
        int x = 2;
        int y;
        //判断语句
        if(x >= 3){
            y = 2 * x + 1;
        }else if (x > -1 && x < 3){
            y = 2 * x;
        }else {
            y = 2 * x - 1;
        }
        System.out.println(y);

    }

举例:


public static void main(String[] args) {
int score = 100;
if(score<0 || score>100){
System.out.println("你的成绩是错误的");
}else if(score>=90 && score<=100){
System.out.println("你的成绩属于优秀");
}else if(score>=80 && score<90){
System.out.println("你的成绩属于好");
}else if(score>=70 && score<80){
System.out.println("你的成绩属于良");
}else if(score>=60 && score<70){
System.out.println("你的成绩属于及格");
}else {
System.out.println("你的成绩属于不及格");
}
}

1.4 if语句和三元运算符的互换

public static void main(String[] args) {
int a = 10;
int b = 20;
//定义变量,保存a和b的较大值
int c;
if(a > b) {
c = a;
} else {
c = b;
}
//可以上述功能改写为三元运算符形式
c = a > b ? a:b;
}

2.switch语句

switch(表达式) {
   case 常量值1:
     语句体1;
     break;
   case 常量值2:
      语句体2;
      break;
      ...
   default:
      语句体n+1;
      break;
}
public static void main(String[] args) {
        //定义变量,判断是星期几
        int weekday = 6;
        //switch语句实现选择
        switch(weekday) {
            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:
                System.out.println("你输入的数字有误");
                break;
        }
    }

switch语句中,表达式的数据类型,可以是byte,short,int,char,enum(枚举),JDK7后可以接收字符串;

2.1 case穿透

在switch语句中,如果case的后面不写break,将出现穿透现象,也就是不会在判断下一个case的值,直接向后运行,直到遇到break,或者整体switch结束;

public static void main(String[] args) {
int i = 5;
switch (i){
  case 0:
    System.out.println("执行case0");
    break;
  case 5:
    System.out.println("执行case5");
  case 10:
    System.out.println("执行case10");
  default:
    System.out.println("执行default");
}
}
上述程序中,执行case5后,由于没有break语句,程序会一直向后走,不会在判断case,也不会理会break,直接运行完整体switch;
由于case存在穿透性,因此初学者在编写switch语句时,必须要写上break;


3.for语句

3.1 for

格式:

for(初始化表达式①; 布尔表达式②; 步进表达式④){
   循环体③
}
public static void main(String[] args) {
        for (int i = 0; i <= 10; i++){
            System.out.println("renmin" + " " + i);
        }
    }

练习:计算1-100之间的偶数之和

public static void main(String[] args) {
        for (int i = 0; i <= 10; i++){
            System.out.println("renmin" + " " + i);
        }
        int sum = 0;
        for (int i = 0; i <= 100; i++){
            if (i % 2 == 0){
                sum +=i;
            }
        }
        System.out.println(sum);
    }

4.while语句

格式

初始化表达式①
while(布尔表达式②){
   循环体③
   步进表达式④
}

练习:计算1-100之间的和

public static void main(String[] args) {
        //初始化
        int i = 1;
        int sum = 0;
        //循环
        while (i <= 100){
            sum += i;
            i++;
        }
        System.out.println(sum);
    }

5.do...while语句

格式:

初始化表达式①
do{
   循环体③
   步进表达式④
}while(布尔表达式②);

do...while循环的特点:无条件执行一次循环体,即使我们将循环条件直接写成false,也依然会循环一次。这样的循环具有一定的风险性,因此初学者不建议使用do...while循环;

public static void main(String[] args){
        int i = 1;
        do{
            System.out.println("一下");
            i++;
        }while(i == 10);//无条件循环一次
    }

6.注意事项

for 和 while 的小区别:
    控制条件语句所控制的那个变量,在for循环结束后,就不能再被访问到了,而while循环结束还可以继续使用,如果你想继续使用,就用while,否则推荐使用for。原因是for循环结束,该变量就从内存中消失,能够提高内存的使用效率;
    在已知循环次数的时候使用推荐使用for,循环次数未知的时推荐使用while;

7.跳出

7.1 break

使用场景:终止switch或者循环
在选择结构switch语句中
在循环语句中

离开使用场景的存在是没有意义的

7.2 continue

使用场景:结束本次循环,继续下一次的循环

7.3 死循环

也就是循环中的条件永远为true,死循环的是永不结束的循环。例如:while(true){}

7.4 嵌套循环

是指一个循环的循环体是另一个循环。比如for循环里面还有一个for循环,就是嵌套循环。总共的循环次数=外循环次数*内循环次数;

for(初始化表达式①; 循环条件②; 步进表达式⑦) {
    for(初始化表达式③; 循环条件④; 步进表达式⑥) {
        执行语句⑤;
    }
}
//5*8的矩形,打印5行*号,每行8个
    //外循环5次,内循环8次
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++){
            for (int j = 1; j <= 8; j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }












猜你喜欢

转载自blog.csdn.net/a1032818891/article/details/80764403