Control flow textbook series (a) - Java If the conditional statement

Control flow textbook series (a) - Java's conditional statements If
more, click here to learn, sign up for a
conditional
Step 1: if
Step 2: Multi-expression with an expression
Step 3: if the course may encounter pits
step 4: if else
step 5: else if
step 6: practice -BMI
step 7: The answer -BMI

Example 1: if

if(表达式1){
  表达式2}

If the value of the expression 1 is true,
on the implementation of the expression 2

if

public class HelloWorld {
    public static void main(String[] args) {
         
        boolean b = true;
        //如果成立就打印yes
        if(b){
            System.out.println("yes");
        }
         
    }
}

Example 2: Multi-expression and expression

public class HelloWorld {
    public static void main(String[] args) {
         
        boolean b = false;
        //如果有多个表达式,必须用大括弧包括起来
        if(b){
            System.out.println("yes1");
            System.out.println("yes2");
            System.out.println("yes3");
        }
         
        //否则表达式2 3 无论b是否为true都会执行
         
        if(b)
            System.out.println("yes1");
            System.out.println("yes2");
            System.out.println("yes3");
             
        //如果只有一个表达式可以不用写括弧,看上去会简约一些
        if(b){
            System.out.println("yes1");
        }
         
        if(b)
            System.out.println("yes1");
         
    }
}

Example 3: pit if the course may be encountered
in the back row 6, if there is a semicolon; semicolon is a complete expression
if b is true, this will be the implementation of a semicolon, then print yes
if b is false, does not perform the semicolon, and then print yes
, seem anyway yes Print

public class HelloWorld {
    public static void main(String[] args) {
 
        boolean b = false;
 
        if (b);
            System.out.println("yes");
 
    }
}

Example 4: if else
case on behalf else does not hold

if else

public class HelloWorld {
    public static void main(String[] args) {
 
        boolean b = false;
 
        if (b)
            System.out.println("yes");
        else
            System.out.println("no");
 
    }
}

Example. 5: IF the else
the else condition is determined is a multi-IF

public class HelloWorld {
    public static void main(String[] args) {
 
        //如果只使用 if,会执行4次判断
        int i = 2;
        if (i==1)
            System.out.println(1);
        if (i==2)
            System.out.println(2);
        if (i==3)
            System.out.println(3);
        if (i==4)
            System.out.println(4);
         
        //如果使用else if, 一旦在18行,判断成立, 20行和22行的判断就不会执行了,节约了运算资源
        if (i==1)
            System.out.println(1);
        else if (i==2)
            System.out.println(2);
        else if (i==3)
            System.out.println(3);
        else if (i==4)
            System.out.println(4);     
         
    }
}
Published 32 original articles · won praise 182 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_44092440/article/details/102971191