Java uses conditional statements and loop structures to determine the control flow. java statements
Like any programming language, Java uses conditional statements and loop structures to determine the control flow. This article will briefly explain the conditions, loops, and switches.
I. Block Scope
Block, that is, compound statements. It refers to several simple Java statements enclosed by a pair of braces. The block determines the scope of the variable.
For example:
1 public class Code { 2 static 3 { 4 System.out.println("1"); 5 } 6 { 7 System.out.println("2"); 8 } 9 public Code()10 {11 System.err.println("3");12 }13 public static void main(String[]args)14 {15 new Code();16 }17 }
Note: variables with the same name cannot be declared in two nested blocks.
Ii. conditional statements
Format 1:
If (condition)
{
Statement1
Statement2
........
}
For example:
1 if(youSales>=target)2 {3 performance="Satisfactory";4 bonus=1000;5 }
Format 2:
If (condition) statement1 else statement2
For example:
1 if(youSales>=target) 2 { 3 performance=“Satisfactory”; 4 bonus=100+10*(youSales-target“); 5 } 6 else 7 { 8 performance=”Unstatisfactory“; 9 bonus=0;10 }
3. Loop
When the condition is true, the while loop is executed.
Format 1:
While (condition) statemnet
For example:
1 while (balance<goal)2 {3 balance+=payment;4 double interest=balance*interestRate/100;5 balance+=interest;6 years++;7 }
Format 2:
Do statement while (condition );
1 do 2 { 3 balance+=payment; 4 double interest=balance*interestRate/100; 5 balance+=interest; 6 7 year++; 8 9 System.out.printf("After year %d,your balance is %,.2f%,year,balance");10 11 System.out.print("Ready to retire?(Y/N)");12 input=in.next();13 }14 while(input.equals("N"));15 16 }
Iv. Definite cycle
A for Loop statement is a common structure that supports iteration. It uses counters or similar variables updated after each iteration to control the number of iterations.
The format is similar to the following:
For (int I = 0; I <x. length; I ++)
System. out. println (I );
Example 4:
1 public class ShuZu1 {2 public static void main (String [] args) {3 int [] [] x = {1, 2, 2}, {3, 3, 3, 3, 3 },{,-, 55 }}; 4 int result = qiuHe (x); 5 System. out. println ("and" + result); 6} 7 public static int qiuHe (int [] [] x) {8 int s = 0; 9 for (int I = 0; I <x. length; I ++) 10 {11 for (int j = 0; j <x [I]. length; j ++) 12 {13 s + = x [I] [j]; 14} 15} 16 return s; 17} 18}
1 public class ShuZu2 {2 public static void main (String [] args) {3 int [] [] x = new int [7] [7]; 4 // generate a random array. Note that no return value is returned. In addition, print a line of characters 5 suiJi (x); 6 System. out. println ("the generated array is:"); 7 8 // The array content is displayed. Note that the returned value is 9 showArray (x ); 10 11 // value 12 float result = getAvg (x); 13 System. out. println ("average is" + result); 14 15} 16 static float getAvg (int [] [] x) {17 float s = 0; 18 for (int I = 0; I <x. length; I ++) {19 for (int j = 0; j <x [I]. length; j ++) {20 s + = x [I] [j]; 21} 22} 23 return s/(x. length * x [0]. length); 24} 25 static void suiJi (int [] [] x) {// I encountered an error. The returned value is in the int type. It should not be. Think about it. 26 for (int I = 0; I <x. length; I ++) {27 for (int j = 0; j <x [I]. length; j ++) {28 x [I] [j] = (int) (Math. random () * 10); 29} 30} 31} 32 static void showArray (int [] [] x) {// I encountered an error. The returned value is in the int type. It should not be. Think about it. 33 for (int I = 0; I <x. length; I ++) {34 for (int j = 0; j <x [I]. length; j ++) {35 System. out. print (x [I] [j] + "\ t"); // give the data space 36} 37 System. out. println (); // print the newline 38} 39} 40}
1 import java. util. arrays; 2 public class SuZu3 {3 public static void main (String [] args) {4 int [] x = {2, 74}; 5 sort (x, 'n'); 6 for (int I = 0; I <x. length; I ++) {7 System. out. print (x [I] + "\ t "); 8} 9} 10 // sort the array selectively 11 // call the API for ascending 12 static void sort (int [] x, char Flag) {13 if ('A' = Flag) {14 Arrays. sort (x); 15} 16 else {17 for (int I = 0; I <x. length-1; I ++) {18 for (int j = 0; j <x. length-1-i; j ++) {19 int temp = 0; 20 if (x [j] <x [j + 1]) {21 temp = x [j]; 22 x [j] = x [j + 1]; 23 x [j + 1] = temp; 24} 25} 26} 27} 28} 29}
1 import java. util. optional; 2 3 public class Suzu4 {4 public static void main (String [] args) {5 System. out. println ("enter"); // This command can only be placed close to the kernel scan = new kernel (System. in. 6 scanned scan = new partition (System. in); 7 // System. out. println ("enter"); or put it in external scan = new external (System. in); the following 8 String str = scan. nextLine (); // nextLine is used to receive a row of 9 10 char [] s = str. toCharArray (); // converts a string to an array of characters 11 scan. close (); 12 int letterCount = 0; 13 int numberCount = 0; 14 int spaceCount = 0; 15 int otherCount = 0; 16 for (int I = 0; I <s. length; I ++) {17 if (s [I]> = 'A' & s [I] <= 'Z' | s [I]> = 'A' & s [I] <= 'Z ') {18 letterCount ++; 19} else if (s [I]> = '1' & s [I] <= '9') {20 numberCount ++; 21} else if (s [I] = '') {22 spaceCount ++; 23} else {24 otherCount ++; 25} 26} 27 System. out. println ("letters" + letterCount + ""); 28 System. out. println ("number:" + numberCount + ""); 29 System. out. println ("Space:" + spaceCount + ""); 30 System. out. println ("Others" + otherCount + ""); 31} 32} // ctrl + shift + f is the code format 33 // ctrl + shift + o is the import package
5. Multiple options: switch statement
The format is similar to the following:
Switch (choice)
{
Case 1:
........
Break;
Case 2:
.......
Break;
.........
// A few more cases (use break)
Default:
.......
Break;
}
Note:
The case tag can be:
* Constant expressions of the char, byte, short, or int type.
* Enumeration constant
* From Java SE 7, the case tag can also be a string literal.