Review the basics of java statement to select

Conditional operator:
? Conditional Expression 1: 2 value;
conditional expression is true boolean type expression, false
if the result is true, the final result value 1
if the result is false, the final result value of 2
regardless how will eventually get a result
public static void main (String [] args) {
// the maximum of two numbers is determined
int. 5 = a;
int = B. 7;
int result = a> B a:? B;
the System. Out.println (Result);
// find whether a value is even
String str = a% 2 == 0 " even":? "odd";
System.out.println (STR);
// the exercises: seeking three the maximum value of the number of conditional operator
int. 9 = C;
int. 17 D =;
int. 6 = E;
int max = C> D C> E C:??? E: D> E D: E;
the System.out .println (max);
}
bitwise:
operands into binary, then binary operations on operands according to the logic bit different operators, to give the final
result, to decimal
&: Two binary operands, the same two-digit number is 0 if there is one result is 0, both of 1 is 1
|: two binary operands, the same number of digits If there is a number of two is 1, both 0 is 0
^: 2 hex two operands, the same two-digit number if the same is 0, 1 different
shift operators:
<< left: the first binary operand is made to move the second operand digits, spare bits 0s, the finally obtained
binary value -> to decimal
m << n ==> m * the n-^ 2
>> right: the first binary operand to the right operand second single digits, do not move out of digits, leaving only
the remaining digit values -> to decimal
m >> n ==> m / 2 ^ n
operator precedence:
() -> max
monocular bit arithmetic relationship, the logic evaluation trinocular
public static void main (String [] args) {
System.out.println ( & 2. 3); 2 //
System.out.println (. 3 | 2); // = 11- 10. 11>. 3
System.out.println (^ 2. 3); // = 01- 10. 11>. 1

the System. Out.println (2 >>. 8); 2 //
System.out.println (<< 2. 8); 32 //
System.out.println (<<. 92); // 36
System.out.println (>> 2. 9); // 2
}
select statement:
Test java flow control statements in
sequence structure: fall from left to right
selection structure: corresponding to certain conditions statement execution
cycle structure: repeatedly perform some statements member
selected structure:
the if..else
Switch ..

radio Ze | single branch:
IF (conditional expression) {
statement body;
}
execution flow:
1. Conditional expression: expression is the boolean formula true, fasle
2. If the value is true, the statement is executed in the {01}
If the value is false, the body does not execute the statement, if the skip structure
bis selected from Ze | double branch:
if (conditional expression) {
statement 1;
} else {
statement 2;
}
if 1 does not satisfy the conditional execution satisfies the conditional execution body 2
if this double selection structure, there must be a result
multiple-choice Ze | multibranched:
IF (conditional expression 1) {
statement body. 1
} the else IF (conditional expression 2) {
statement body 2
} the else IF (conditional expression 3) {
statement 3

}
....
the else {
statement n-body;
}
if only one statement body, before and after the {} may be omitted
if the entire configuration if there is not a single condition if the else {} whole structure certainly a result, it may not satisfy Article
statement body member not performed
if the conditions and the operator selects:
1. conditional operator must have a result, the structure may not result if
the plurality of conditions employed if 2 is relatively simple, requires the use of the conditional operator logical operators | conditional nested operator
simply use the conditional operator, using a complicated structure if
public static void main (String [] args) {
int a = 0;
if (to false) {
System.out.println (a + "> 0") ;
System.out.println (123); // the same scope, the statement is executed from top to bottom thereof
}
System.out.println ( "IF statement is finished body");

// bis select
IF (a> 0)
the System .out.println (A + "> 0");
the else
System.out.println (A + "<= 0");


// select multiple
IF (A> 0) {
System.out.println (A + "> 0") ;
IF the else} (A == 0) {
System.out.println (A + "= 0");
} the else {
System.out.println (A + "<0");
}
Switch statement: Analyzing setting
switch (Expression ) {
Case value 1:
statement body 1;
BREAK;
Case 2 values:
statement body 2;
BREAK;
....
default:
statement body;
BREAK;
}

expression: Usually variable | expression | value (not used)
byte, short, char, enumeration (1.5), strings (1.7)

Case: expression results need to be determined with the value of Case, if the condition statement is executed after the body is not satisfied if
the condition continues to the following case judgment
break: the end of the swtich, to prevent the penetration of case: If there is no break statement after the case body conditions are met, the following
case
will not directly determine their statements are executed body, until the next break, or switch. end before the end of
default: else if and the like, are above case are met, the implementation of the contents of default; can be written in a switch statement
of
Anywhere, generally placed at the end, on the last break may be omitted, if placed in other positions omit the break will
occur penetrate case
public static void main (String [] args) {
String In Flag = "green";
Switch (In Flag) {
case "red":
System.out.println (+ In Flag "stop");
BREAK;
case "green":
System.out.println (+ In Flag "OK");
BREAK;
case "yellow":
System.out.println (flag + "Hold");
BREAK;
default:
System.out.println ( "you stupid, right, there is no such lights !!!");
BREAK;
}

// penetrate
Switch (Flag) {
Case "red light ":
System.out.println (+ In Flag" stop ");
Case" green ":
System.out.println (+ In Flag" OK ");
Case" yellow ":
System.out.println(flag+"等一等");
break;
default:
System.out.println ( "You stupid, right, there is no such lights !!!");
}


char A = 99;
Switch (A) {
default:
System.out.println ( "other characters");
Case 'A ':
System.out.println ( "A");
BREAK;
Case' B ':
System.out.println ( "B");
BREAK;
}

Boolean to false = B;
// switch to use for a given value determination
switch (b + "") {
Case "to true":
System.out.println ( "to true");
BREAK;
Case "to false":
System.out.println ( "to false");
BREAK;
}
int I;
IF (B == to true ) {
I = 0;
} the else {
I =. 1;
}
Switch (I) {
Case. 1:
System.out.println("1");
break;
0 Case:
System.out.println ( "0");
BREAK;
}

// month season based on an output;
// penetration role
int month The = 10;
Switch (month The) {
Case. 3:
Case. 4:
Case. 5:
the System .out.println (month + "is a spring month");
BREAK;
Case 6:
Case 7:
Case 8:
System.out.println (+ month The "summer months");
BREAK;
Case 9:
Case 10:
Case 11:
System .out.println (month + "autumn month");
BREAK;
Case 12:
Case 1:
Case 2:
System.out.println (+ month The "winter months");
BREAK;
default:
System.out.println ( "month does not conform to the format ");
}

}

}
loop
Repeatedly performed using some code
default condition as long as infinite loop iteration of the loop, is determined according to the conditions, internal BREAK, end loop

for loop ***
the while loop **
a do..while cycle

for (initialization condition; Analyzing condition; conditions change ) {
loop body statement;
}
conditions initialization: declare a variable and assign int i = 0;
condition judgment: the values i condition to judge, judges whether I make a condition of your cycle, if the conditions are met, continue loop
statements , ends the loop condition is not satisfied
condition ways: i change their values, without changing the die (loop never executed) occurs

execution flow:
1. perform initialization condition
2. determination execution condition, the value of the boolean type expression
3. If the value is false, the end of the cycle, the value is true, execution loop statement
4. execution condition changes
from the second cycle starts, 2,3,4 repeat step

when only one of loop statement, before and after the { may be omitted}
public static void main (String [] args) {
// conditions: 1) the number of control execution cycle internal body 2) only recycled material used
for (int I = 1; I <= 10; I ++) {
the System .out.println ( "An-hong, I think Want sleep! - Keep Cool "+ I);
}
// print each an integer between 1 and 100
for (int I =. 1; I <= 100; I + =. 1) {
System.out.println (I);
}

//. 1 + 2 + +. 5. 4. 3 +
int SUM = 0; // store and
for (int . 1 = I; I <=. 5; I ++)
SUM = I +;

System.out.println (SUM);
//System.out.println(i); scope is only valid in the current cycle

// infinite loop
/ * for (;;) {
System.out.println ( "haha ~");
} * /
}
}

Guess you like

Origin www.cnblogs.com/DHY868168/p/11234111.html