Java BASICS (3): java Basics
1. Select structure and condition judgment 1. if statement
An if statement contains a Boolean expression and one or more statements. If the Boolean expression is true, the code block in the if statement is executed; otherwise, the code behind the if statement block is executed.
1 import static java. lang. math. round; 2 3 public class Conditional {4/** 5 * simple if Statement 6 */7 @ Test 8 public void testIf () {9 double random = Math. random (); 10 int num = (int) round (random * 10); // create a random integer 11 System. out. println (num); 12 if (num <3) {13 System. out. println ("num less than 3"); 14} else {15 System. out. println ("num greater than or equal to 3"); 16} 17} 18 19/** 20 * simple if, else if, else statement 21 */22 @ Test23 public void testIf2 () {24 double random = Math. random (); 25 int num = (int) round (random * 10); // create a random integer 26 System. out. println (num); 27 if (num <3) {28 System. out. println ("num less than 3"); 29} else if (num <6) {30 System. out. println ("num less than 6"); 31} else {32 System. out. println ("num greater than or equal to 6"); 33} 34} 35 36/** 37 * nested if statement 38 */39 @ Test40 public void testIf3 () {41 double random = Math. random (); 42 int num = (int) round (random * 10); // create a random integer 43 System. out. println (num); 44 if (num <3) {45 System. out. println ("num less than 3"); 46} else if (num <6) {47 System. out. println ("num less than 6"); 48} else {49 if (num = 6) {50 System. out. println ("num equals 6"); 51} 52 System. out. println ("num greater than 6"); 53} 54} 55}
2,
SwitchStatement
The switch statement determines whether a variable is equal to a value in a series of values. Each value is called a branch.
Syntax:
Switch (variable or expression) {Possible value of case variable 1: function statement;
Break; possible value of case variable 2: Functional statement;
Break; possible value of case variable 3: Functional statement;
Break; ...... default: function statement ;}
Switch statement rules:
The variable types in the switch statement can be byte, short, int, or char. Since Java SE 7, switch supports the string type.
The switch statement can have multiple case statements. Each case is followed by a value to be compared and a colon.
The data type of the nominal value after case must be the same as that of the variable, and can only be a constant or a literal constant.
When the value of the variable is equal to the value of the case statement, the statement after the case statement starts to run until the break statement appears.
If no break statement appears, the program continues to execute the next case statement until the break statement appears.
The values are arranged in the order of case values. The default statement is executed when the values of the case statement and the variable values are equal. The default branch does not require a break statement.
1 import static java. lang. math. round; 2 3 public class Conditional1 {4/** 5 * switch statement 6 */7 @ Test 8 public void testSwitch () {9 double random = Math. random (); 10 int num = (int) round (random * 10); 11 System. out. println (num); 12 switch (num) {13 case 0: 14 System. out. println ("num is 0"); 15 break; 16 case 1: 17 System. out. println ("num is 1"); 18 break; 19 case 2: 20 System. out. println ("num is 2"); 21 break; 22 case 3: 23 System. out. println ("num is 3"); 24 break; 25 case 4: 26 System. out. println ("num is 4"); 27 break; 28 case 5: 29 System. out. println ("num is 5"); 30 break; 31 default: 32 System. out. println ("num greater than 5"); 33} 34} 35}
Ii. Loop Structure 1. while Loop
First Judge the condition and then execute the statement
2. do-while loop
Execute the command once before determining the condition
1 public class Circulation {2/** 3 * while loop Statement 4 * first judgment, then execute 5 */6 @ Test 7 public void testWhile () {8 int num = 5; 9 while (num> 0) {10 System. out. println (num); 11 num-= 1; 12} 13} 14 15/** 16 * do-while loop 17 * execute do once first, then judge 18 */19 @ Test20 public void testDoWhile () {21 int num = 5; 22 do {23 System. out. println (num); 24 num-= 1; 25} while (num> 0); 26} 27}
3. for Loop
The number of for loop executions is determined before execution.
Syntax:
For (initialization Statement A; condition judgment B; function Statement C after loop) {// loop body D}
After executing a loop, update the loop control variable. The function of statement C. Then, check the Boolean expression again. Execute the preceding process cyclically.
1 public class Circulation {2/** 3 * simple for loop 4 */5 @ Test 6 public void testFor () {7 int num = 10; 8 for (int I = 0; I <num; I ++) {9 System. out. println (I); 10} 11} 12}
4. foreach enhanced for Loop
Java 5 introduces an enhanced for loop mainly used for arrays.
Java enhanced for loop Syntax:
For (data type value: array) {// code sentence}
1 public class Circulation1 {2/** 3 * foreach loop 4 */5 @ Test 6 public void testForeach () {7 int [] num = {10, 20, 30, 40, 50}; 8 for (int I: num) {9 System. out. println (I); 10} 11} 12}
5. nested loops
1 public class Circulation {2/** 3 * nested loop realize Bubble Sorting 4 */5 @ Test 6 public void test () {7 int [] num = {12, 2, 7, 5, 14}; 8 int t = 0; 9 for (int I = 0; I <num. length-1; I ++) {10 for (int j = 0; j <num. length-1; j ++) {11 if (num [j]> num [j + 1]) {12 t = num [j]; 13 num [j] = num [j + 1]; 14 num [j + 1] = t; 15} 16} 17} 18 System. out. println (Arrays. toString (num); 19} 20}
Iii. Loop Control Statement 1. break
Break indicates that the current cycle is terminated, that is, the current cycle is exceeded.
2. continue
Continue indicates skipping this loop and entering the next loop.
3. return
Return indicates that the current method is ended.
Note: break, continue, and return cannot be followed by any code, because they will never be executed.