Day3 java operator and process control, day3 Operator
Value assignment
Assignment operator: =, + =,-=, × =,/=, % =
Class fuzhiyunsuan {public static void main (String [] args) {int i1 = 10; i1 + = 3; // i1 = i1 + 3, however, there is a difference between the two. // For example, the following short s = 10; // s = s + 3; Compilation fails, causing loss of accuracy. // s = (short) (s + 1); // force-Modify the precision. s + = 1 is not recommended; // This operation can be performed without changing the Data Type of s. out. println (s);/* value assignment operator: =, + =,-=, × =,/=, % = * // remember that = is a value assignment operation in java. Boolean b1 = false; if (b1 = true) System. out. println ("the result is true"); else System. out. println ("the result is false ");}}
Root @ debian:/home/jeff/java_coding/day003 # java fuzhiyunsuan 11 returns true
Because I = I + 1 may not be compiled, it is better to use the + = method. In java, = is a value, and = is used to determine equality.
Logical operators
class logiccal{ public static void main(String[] args){ boolean a = true; boolean b = false; System.out.println(a & b); System.out.println(a && b); System.out.println(a | b); System.out.println(a || b); System.out.println(!a & b); System.out.println(a ^ b); }}
root@debian:/home/jeff/java_coding/day003# java logiccal falsefalsetruetruefalsetrue
& Differences
& Is the sum of short-circuit operations, that is, when a & B is set to false, whether B is true or false, the result is false. Therefore, B is no longer running and jumps out of the calculation.
class logicdiff{ public static void main(String[] args){ boolean b = false; int i1 = 10; if(b & (i1++) > 0){ System.out.println("good"); }else{ System.out.println("bad"); } System.out.println(i1); int i2 = 10; if(b && (i2++) > 0){ System.out.println("good"); }else{ System.out.println("bad"); } System.out.println(i2);}}
root@debian:/home/jeff/java_coding/day003# java logicdiff bad11bad10
In this example, the value on the left is false, and the I ++ on the right is still calculated. The result is + 1, and the value on the right is not + 1. We recommend that you use &&.
class logicdiff{ public static void main(String[] args){ boolean b = false; int i1 = 10; if(b & (10 / 0 == 0){ System.out.println("good"); }else{ System.out.println("bad"); } System.out.println(i1); int i2 = 10; if(b && 10/0 == 0){ System.out.println("good"); }else{ System.out.println("bad"); } System.out.println(i2);}}
Here & an error is reported during compilation, but & is compiled.
| Same as |.
Bitwise operation
class bitcal{ public static void main(String[] args){ int i1 = 31; System.out.println(i1<<3); System.out.println(i1<<28); System.out.println(i1>>2); System.out.println(i1>>>2); int i2 = -31; System.out.println(i2>>2); System.out.println(i2>>>2); }}
root@debian:/home/jeff/java_coding/day003# java bitcal 248-26843545677-81073741816
Displacement may cause loss of precision. The number of non-Signed bits changes significantly when the negative number is reached.
Transposition of two variables
Class exchange {public static void main (String [] args) {int m = 111; int n = 233; System. out. println ("m:" + m + "n:" + n); // 1. provides the intermediate variable int temp = m; m = n; n = temp; System. out. println ("m:" + m + "n:" + n); // 2.m, a large number of n may cause loss of precision m = m + n; n = m-n; m = m-n; System. out. println ("m:" + m + "n:" + n); // 3. bitwise m = m ^ n; n = m ^ n; m = m ^ n; System. out. println ("m:" + m + "n:" + n );}}
Running result
root@debian:/home/jeff/java_coding/day003# java exchange m:111n:233m:233n:111m:111n:233m:233n:111
Ternary expression
Format: (conditional expression )? Expression 1: expression 2;
If the conditional expression is true, the operation result is expression 1. If the conditional expression is false, the operation result is expression 2.
Expressions 1 and 2 should be of the same type.
The difference between a ternary expression and an if clause is that a ternary expression simplifies the if statement and must return a result. The code after an if clause can have multiple statements.
Class sanyuan {public static void main (String [] args) {int I = 10; int j = 20; int max = (I> j )? I: j; System. out. println ("larger than limit is" + max); String str = (I> j )? "I big" :( I = j )? "Equal": "j large"; System. out. println (str );}}
Calculation Result
Root @ debian:/home/jeff/java_coding/day003 # java sanyuan is larger than java sanyuan
Process Control
Sequential Structure
Programs run from top to bottom and line by line.
Branch Structure
Select a branch to run according to the condition
Loop Structure: execute a code segment repeatedly according to the loop conditions.
If statement format
If (true) {Execution code block ;}
If (conditional expression) {Execution code block;} else {Execution code block ;}
If (condition expression) {Execution code block;} else if (condition expression) {Execution code block ;}... Else {Execution code block ;}
Import java. util. role; class score {public static void main (String [] args) {// create a role object, which is used to type the number Scanner uscore = new role (System. in); System. out. println ("Enter Exam Score"); // call the corresponding method of this object to obtain the value entered by the keyboard // next (): gets a string from the keyboard, int indicates that an integer value is entered to compare int str = uscore. nextInt (); System. out. println ("your score is" + str); if (str> 100 | str <0) {System. out. println ("Incorrect score");} else {if (str = 100) {System. out. println ("First Place");} else if (str> 80 & str <= 99) {System. out. println ("excellent");} else if (str >=60 & str <= 80) {System. out. println ("general");} else {System. out. println ("coming soon ");}}}}
Root @ debian:/home/jeff/java_coding/day003 # For java score, enter test score 13. your score is 13. Come on. root @ debian: /home/jeff/java_coding/day003 # For java score, enter the test score 100. your score is 100 first. root @ debian: /home/jeff/java_coding/day003 # java score. Enter the test score 76. your score is 76.
Switch statement
Switch (variable) {case value 1: case value 2:... default :}
Class switch1 {public static void main (String [] args) {int I = 1; switch (I) {case 0: System. out. println ("zero"); case 1: System. out. println ("1"); case 2: System. out. println ("2"); default: System. out. println ("other ");}}}
Running result
Root @ debian:/home/jeff/java_coding/day003 # java switch1 one or two others
If the loop body does not exit with the break, the case statement under it will continue to be executed after it matches to the branch.
Default is optional and its position is flexible.
Class switch2 {public static void main (String [] args) {int I = 5; switch (I) {default: System. out. println ("other"); case 0: System. out. println ("zero"); case 1: System. out. println ("1"); case 2: System. out. println ("2 ");}}}
Running result
Root @ debian:/home/jeff/java_coding/day003 # java switch2 other 0, 1, 2
Class switch3 {public static void main (String [] args) {int I = 5; switch (I) {default: System. out. println ("other"); break; case 0: System. out. println ("zero"); break; case 1: System. out. println ("1"); break; case 2: System. out. println ("2"); break ;}}}
Root @ debian:/home/jeff/java_coding/day003 # other java switch3
The variable types in the switch can be char, byte, short, int, enumeration, String (jdk1.7)
The condition of case can only be that the value cannot be the value range.
This code is hosted on my github: day003.