选择结构_标准if-else语句与选择结构_扩展if-else语句

判断语句2--if...else
if语句第二种格式: if...else
if(关系表达式) {
     语句体1; 
}else { 
    语句体2;
 }
执行流程
首先判断关系表达式看其结果是true还是false
如果是true就执行语句体1
如果是false就执行语句体2

 

 

public class demo09 {
    public static void main(String[] args){ 
        // 判断给定的数据是奇数还是偶数 // 定义变量 
        int a = 1; 
        if(a % 2 == 0) {
            System.out.println("a是偶数"); 
        }else{
            System.out.println("a是奇数"); 
        }
        System.out.println("结束");
    }
}

判断语句3--if..else if...else
if语句第三种格式: if...else if ...else
if (判断条件1) {
 执行语句1;
 } else if 
(判断条件2) {
 执行语句2; 
}...
 }else if 
(判断条件n) 
{ 执行语句n;
 } else 
{
 执行语句n+1;
 }
执行流程
首先判断关系表达式1看其结果是true还是false
如果是true就执行语句体1
如果是false就继续判断关系表达式2看其结果是true还是false
如果是true就执行语句体2
如果是false就继续判断关系表达式…看其结果是true还是false
如果没有任何关系表达式为true,就执行语句体n+1。
public class demo010 {
    public static void main(String[] args) {
        int x = 2;
        int y;
        if (x>=3){
            y=2*x+1;
        }else if (-1<x&&x<3){
            y=2*x;
        }else {
            y=2*x-1;
        }
        System.out.println("结果是:"+y);
    }
}

 

 

 

 

public class demo010 {
public static void main(String[] args) {
int x = 2;
int y;
if (x>=3){
y=2*x+1;
}else if (-1<x&&x<3){
y=2*x;
}else {
y=2*x-1;
}
System.out.println("结果是:"+y);
}
}
posted @ 2022-06-28 22:00  zj勇敢飞,xx永相随  阅读(72)  评论(0编辑  收藏  举报