Compound statement
Like C language or other languages, the compound statement of Java language is a statement with the whole block as the unit, so it is also called block statement. A compound statement begins with an open bracket '{' and ends with a closed bracket '}'. In the previous study, I have been exposed to this compound statement. For example, when defining a class or method, the class body is marked with "{}" as the start and end, and the method body is also marked with "{}". Each statement in a compound statement is executed from top to bottom. The compound statement takes the whole block as the unit, which can be used where any single statement can be used, and the compound statement can also be nested in the compound statement.
Conditional statement
1. if conditional statement
If conditional statement is an important programming statement. It is used to tell the program to execute a program when a condition is true, and execute another statement in another case. Using the if conditional statement, you can choose whether to execute the statement immediately after the condition. The keyword if is followed by a Boolean expression as a condition. If the result returned by the expression is true, the subsequent statements will be executed; If false, the statement after the if condition is not executed. If conditional statements can be divided into simple if conditional statements, if... else statements and if... else if multi branch statements.
if use
public class Test { public static void main(String args[]){ int x = 10; if( x < 20 ){ System.out.print("This is if sentence"); } } }
Use of if and else
public class Test { public static void main(String args[]){ int x = 30; if( x == 10 ){ System.out.print("Value of X is 10"); }else if( x == 20 ){ System.out.print("Value of X is 20"); }else if( x == 30 ){ System.out.print("Value of X is 30"); }else{ System.out.print("This is else sentence"); } } }
2. switch multi branch statement
The value of the expression in the switch statement must be integer or character, and the constant value 1 ~ constant value n must also be integer or character. Switch statement first calculates the value of the expression. If the value of the expression is the same as the variable value after a case, several statements after the case statement will be executed until the break statement is encountered. At this time, if there is no break statement in the case statement, several statements in the following case will continue to be executed until the break statement is encountered. If no constant value is the same as the value of the expression, the statement after default is executed. The default statement is optional. If it does not exist and the value of the expression in the switch statement is not the same as the constant value of any case, switch will not do any processing.
Format:
switch(expression){ case value : //sentence break; //Optional case value : //sentence break; //Optional //You can have any number of case statements default : //Optional //sentence }
use:
public class Test { public static void main(String args[]){ //char grade = args[0].charAt(0); char grade = 'C'; switch(grade) { case 'A' : System.out.println("excellent"); break; case 'B' : case 'C' : System.out.println("good"); break; case 'D' : System.out.println("pass"); break; case 'F' : System.out.println("You need to work harder"); break; default : System.out.println("Unknown level"); } System.out.println("What is your level " + grade); } }
Circular statement
1. while loop statement
The while statement is also called a conditional judgment statement. Its loop mode is to use a condition to control whether to continue to execute the statement repeatedly. The syntax is as follows:
While (conditional expression) {
Execute statement
}
When the return value of the conditional expression is true, execute the statement in {}. After executing the statement in {}, re judge the return value of the conditional expression until the result returned by the expression is false, and exit the loop.
public class Test { public static void main(String args[]) { int x = 10; while( x < 20 ) { System.out.print("value of x : " + x ); x++; System.out.print("\n"); } } }
2. do... while loop statement
The do... While loop statement is similar to the while loop statement. The difference between them is that the while statement judges whether the condition is true before executing the loop body, while the do... While loop statement executes the loop once before judging whether the condition is true. That is, the program segment in braces in the do... While loop statement must be executed at least once. The syntax is as follows:
do {
Execute statement
}
While (conditional expression);
An obvious difference from the while statement is that the do... While statement has a semicolon (;) at the end.
public class Test { public static void main(String args[]){ int x = 10; do{ System.out.print("value of x : " + x ); x++; System.out.print("\n"); }while( x < 20 ); } }
3. for loop statement
For loop is one of the most useful loop statements in Java programming. A for loop can be used to repeatedly execute a statement until a condition is met. foreach syntax is added after Java 5. This section will introduce these two forms of for loop in detail.
First kind
The syntax is as follows:
For (expression 1; expression 2; expression 3) {
Statement sequence
}
Expression 1: initialization expression, which is responsible for completing the initialization of variables.
Expression 2: loop condition expression. It is an expression with boolean value and specifies the loop condition.
Expression 3: operation expression after loop, responsible for trimming variables and changing loop conditions.
public class Test { public static void main(String args[]) { for(int x = 10; x < 20; x = x+1) { System.out.print("value of x : " + x ); System.out.print("\n"); } } }
Second
Foreach statement is a special simplified version of for statement, but foreach statement can not completely replace for statement. However, any foreach statement can be rewritten as a version of for statement. Foreach is not a keyword. It is customary to call this special format of for statement foreach statement. Foreach statement provides great convenience for programmers in traversing arrays and so on (this book will introduce arrays in detail in Chapter 6). The syntax is as follows:
For (element variable x: traversing object obj) {
java statement referencing x;
}
public class Test { public static void main(String args[]){ int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ){ System.out.print( x ); System.out.print(","); } System.out.print("\n"); String [] names ={"James", "Larry", "Tom", "Lacy"}; for( String name : names ) { System.out.print( name ); System.out.print(","); } } }