java笔记8之选择结构IF

注意1

        A比较表达式无论简单还是复杂,结果必须是boolean类型
        B:if语句控制的语句体如果是一条语句,大括号可以省略;
          如果是多条语句,就不能省略。建议永远不要省略
        C:一般来说:有左大括号就没有分号,有分号就没有左大括号

 1 class IfDemo2 {
 2     public static void main(String[] args) {
 3         int x = 10;
 4         
 5         if(x == 10) {
 6             System.out.println("x等于10");
 7         }
 8         
 9         if((x > 5) || (x == 10)) {
10             System.out.println("x大于或者等于10");
11         }
12         System.out.println("-------------------");
13         
14         int a = 100;
15         
16         /*
17         if(a == 100) {
18             System.out.println("a的值是100");
19         }
20         */
21         
22         if(a != 100) {
23             System.out.println("a的值是100");
24             System.out.println("over");
25         }
26         System.out.println("-------------------");
27         
28         int b = 100;
29         if(b != 100);  //这里其实是有语句体的,只不过是空语句体。
30         
31         //代码块
32         {
33             System.out.println("b的值是100");
34             System.out.println("over");
35         }
36     }
37 }
原文地址:https://www.cnblogs.com/lanjianhappy/p/6266712.html