java基础4:Java运算、控制语句

来源:互联网 发布:多元高斯分布 知乎 编辑:程序博客网 时间:2024/05/20 21:23

关系运算符

<, <=, >, >=,==, !=

布尔运算符

&&, ||, &, |,!

位运算符(目前用的比较少,以后有时间再讲

&, |, ~, ^, >>, >>>, <<

& 按位与(AND[真真为真,真假为假]

| 按位或(OR[假假为假,其余全真]

^ 按位异[相同为假,不同为真]

~按位非(NOT[真则假,假则真]

>> 右移 

>>> 右移,左边空出的位以0填充

<< 左移 

赋值类运算符

=, +=, -=, *=, /=,%=

字符串连接运算符

+            

条件运算符

?:

其他运算符

instanceof new

 

一、Java常用运算符

1.算数运算符

public class OperatorTest01 {

      

       public static void main(String[] args) {

              int a = 1;

             

              //a++相当于a=a+1;

              int b = a++;

              System.out.println("a=" + a);

              System.out.println("b=" + b);

       }

}

以上会看到a=2,b=1,为什么会出现这种结果?

++在变量的后面,先把值赋值给b,然后a再加(也就是先赋值再自加

所以就输出了a=2,b=1

【示例代码】,将++放到变量的前面

public class OperatorTest02 {

      

       public static void main(String[] args) {

              int a = 1;

             

              int b = ++a;

              System.out.println("a=" + a);

              System.out.println("b=" + b);

       }

}

输出结果为a=2,b=2,如果++在变量的前面,是先自加在赋值

【示例代码】取余/取模

public class OperatorTest03 {

      

       public static void main(String[] args) {

             

              int a = 10/3;

              System.out.println(a);

             

              //取余

              int b = 10 % 3;

              System.out.println(b);

       }

}

 

2、关系运算符和布尔运算符

l  与:两个操作数相与,如果都为true,则为true

l  或:两个操作数相或,有一个为true,则为true

l  异或:相异为true,两个操作数不一样就为true

l  短路与和逻辑与的区别?

短路与, 从第一个操作数推断结果,只要有一个为fase,不再计算第二个操作数

n  逻辑与,两个操作数都计算

l  短路或和逻辑或的区别?

短路或,从第一个操作数推断结果,只要为true,不再计算第二个操作数

n  逻辑或,两个操作数都计算

      

短路与和逻辑与、短路或和逻辑或与虽然计算方式不一样,但结果都是一样的

 

注意:操作数必须是boolean

op1

op2

op1&&op2

op1&op2

op1||op2

op1|op2

op1^op2

!op1

true

true

true

true

false

false

true

false

false

true

true

false

false

true

false

true

true

true

false

false

false

false

false

true

【代码示例】

public class OperatorTest04 {

      

       public static void main(String[] args) {

             

              boolean op1 = (10 > 5);

             

              //输出true

              System.out.println("op1=" + op1);

             

              boolean op2 = (10 < 5);

              System.out.println("op2=" + op2);

             

              System.out.println("op1 && op1 =" + (op1 && op2));

              System.out.println("op1 & op1 =" + (op1 & op2));

             

              System.out.println("op1 || op1 =" + (op1 || op2));

              System.out.println("op1 | op1 =" + (op1 | op2));

             

              System.out.println("op1 ^ op1 =" + (op1 ^ op2));

             

              System.out.println("!op1 =" + !op1);

             

             

       }

}

 

3、赋值运算符

+=

a+=b

a=a+b

-=

a-=b

a=a-b

*=

a*=b

a=a*b

/=

a/=b

a=a/b

%=

a%=b

a=a%b

【代码示例】

public class OperatorTest05 {

      

       public static void main(String[] args) {

              int a = 1;

              int b = 2;

             

              //a=a + b

              a+=b;

              System.out.println(a);

       }    

}

 

4、条件运算符

条件运算符是java语言中的三元运算,格式如下:

op1 ? op2 : op3

如果操作数op1true则输出op2,否则输出op3

public class OperatorTest06 {

      

       public static void main(String[] args) {

              int a = 11;

             

              int b = a>0?1:-1;

             

              System.out.println(b);

             

              boolean c = a%2==0?true:false;

              System.out.println(c);

       }    

}

字符串与其他基本数字相加结果为字符串。



二、ava控制语句可以分为7种:

l  控制选择结构语句

ifif else

switch

l  控制循环结构语句

for

while

do while

l  改变控制语句顺序

break

continue

1、if语句和if else语句

条件语句只要包括if语句和if else语句

if语句格式:

if (布尔表达式) {

    一条或多条语句

}

如果语句为一条,可以省略大括号,如:

  if(布尔表示式)

    一条语句

  只有表示为true时,才会进入if语句中

if else语句格式

 

if (布尔表示式) {

       一条或多条语句

}else {

一条或多条语句

}

  如果else时,还需要条件,可以写成如下可是:

if (布尔表示式) {

       一条或多条语句

}else if(布尔表示式) {

一条或多条语句

} else {

一条或多条语句

}

【示例代码】

public class IfelseTest01 {

      

       public static void main(String[] args) {

             

              int age = 3;

             

              if (age % 2 == 0) {

                     System.out.println("是偶数");

              }

             

              //建议只有一条语句也加入大括号

              if (age % 2 == 0)

                     System.out.println("是偶数");

                    

              if ( age >0 && age <=5){

                     System.out.println("幼儿");

              }

              if (age >5 && age <=10) {

                     System.out.println("儿童"); 

              }

              if (age >10 && age<=18) {

                     System.out.println("少年"); 

              }

             

              //这种方法效率会好一些,如果年龄为5

              //以下代码只判断一次就可以,而上面的代码会判断多次

              if (age >0 && age <=5) {

                     System.out.println("幼儿");

              }else if (age >5 && age <=10){

                     System.out.println("儿童"); 

              }else if (age >10 && age<=18) {

                     System.out.println("少年"); 

              }else {

                     System.out.println("青年"); 

              }

       }    

}


2、switch语句

switch也称为多重分支,具体格式如下

switch (表达式) {

       case 1

              语句

       break;

       case 2

              语句

          break;

       default

              语句

        Break;

}

说明:

表达式的值只能为:charbyteshortint类型booleanlongfloatdouble都是非法的

break语句可以省略,但会出现switch穿透

default语句也可以省略,一般不建议省略,并且放置在最后

注意类的命名:首字母要大写,单词之间首字母大写,这种命名方式称为“驼峰标识”

【代码示例】

public class SwitchTest01 {

      

       public static void main(String[] args) {

              char c = 'd';

             

              switch(c) {

                     case 'a':

                            System.out.println("优秀");

                            break;//注意break

                     case 'b':

                            System.out.println("良好");

                            break;

                     case 'c':

                            System.out.println("一般");

                            break;

                     default:

                            System.out.println("很差");

              }

              System.out.println("switch执行结束!");

       }    

}

【代码示例】

public class SwitchTest02 {

      

       public static void main(String[] args) {

              //byte c = 1;

              //short c = 1;

              //int c = 1;

              //不能为longswitch只能为byteshortintchar

              long c = 1;

             

              switch(c) {

                     case 1:

                            System.out.println("优秀");

                            break;

                     case 2:

                            System.out.println("良好");

                            break;

                     case 3:

                            System.out.println("一般");

                            break;

                     default:

                            System.out.println("很差");

              }

              System.out.println("switch执行结束!");

       }    

}


3、for语句

for语句格式如下:

for(初始化部分表达式;条件表达式;更新表达式) {

       一条或多条语句

}

【代码示例】

public class ForTest01 {

      

       public static void main(String[] args) {

             

              for (int i=1; i<=10; i++) {

                     System.out.println(i);

              }

       }

}

初始化i=1

i<=10

结束

No

执行打印语句

i++

 

4、while语句

while语句格式

       while(布尔表达式) {

       一条或多条语句

}

【代码示例】

public class WhileTest01 {

      

       public static void main(String[] args) {

             

              int i = 1;

             

              //注意死循环问题

              while(i<=10) {

                     System.out.println(i);

                    i++;

              }

       }

}

以上程序同样完成了1~10的输出,可以看出for更加简单,可以看做forwhile语句的便利方式,采用for语句完全可以模仿while语句,如将for语句中的“初始化部分表达式”和“更新表达式”省略

for (; 条件表达式 ;) {

       语句

}

public class WhileTest02 {

      

       public static void main(String[] args) {

             

              int i = 1;

              for(;i<=10;) {

                     System.out.println(i);

                     i++;

              }

       }

}


5、do while语句

do while格式

do {

       语句

}while(布尔表达式)

注意while括号后必须写分号

do whilewhile非常相似,不同点在于do while先执行循环体,也就是说不管条件符不符合,循环体至少执行一次

【代码示例】

public class DoWhileTest01 {

      

       public static void main(String[] args) {

             

              int i = 1;

              do{

                     System.out.println(i);

                     i++;

              }while(i<=10); //注意分号

       }

}


6、break语句

break可以用在switch、循环语句和带标号的语句块中

l  在循环语句中主要是为了终止循环

public class BreakTest01 {

      

       public static void main(String[] args) {

              for (int i=1; i<=100; i++) {

                     System.out.println(i);

                     if (i == 50) {

                            break; //会跳出当前循环

                     }

              }

              //break跳到这里

       }

}

l  在循环语句中主要是为了终止循环(多重循环)

public class BreakTest02 {

      

       public static void main(String[] args) {

              for (int i=1; i<=5; i++) {     

                     System.out.println("i===================" + i);      

                     for (int j=1; j<=10; j++) {

                            System.out.println(j);

                           

                            if (j == 5) {

                                   break;    

                            }

                     }

                     //以上break会到此为止

              }

              //以上break不会跳到这里

       }

}

7、continue语句

continue只能用在循环语句中,表示在循环中执行到continue时,自动结束本次循环,然后判断条件,决定是否进行下一次循环(多个循环的跳出使用---标签:的方式)

public class ContinueTest01 {

      

       public static void main(String[] args) {

              for (int i=1; i<=100; i++) {

                     //if (i % 2 != 0) {

                     //     System.out.println(i);

                     //}

                     if (i % 2 == 0) {

                            continue; //继续下一次循环

                     }

                     System.out.println(i);

              }

       }

}

 



0 0
原创粉丝点击