Cycle control
There may be a situation when we need to execute blocks of code several times, which is usually called a loop.
Java has a very flexible three-loop mechanism. One of the following three loops can be used:
As of Java 5, an enhanced for loop is introduced. This is mainly used for arrays.
while loop
While loop is a control structure that can repeat a specific number of tasks.
grammar
The syntax of a while loop is:
while(Boolean_expression){ //Statements}On execution, if the result of the Boolean expression is true, the actions in the loop will be executed. As long as the result of the expression is true, execution will continue.
Here, the key point of the while loop is that the loop may not run forever. When the expression is tested, the result is false, the loop body will be skipped and the first statement after the while loop will be executed.
Example
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"); } }}This will produce the following results:
value of x : 10value of x : 11value of x : 12value of x : 13value of x : 14value of x : 15value of x : 16value of x : 17value of x : 18value of x : 19
do...while loop
do ... while loop is similar to a while loop, except that a do ... while loop is guaranteed to be executed at least once.
grammar
The syntax of a do...while loop is:
do{ //Statements} while (Boolean_expression);Note that the Boolean expression appears at the end of the loop, so the statement in the loop performs the previous Boolean test.
If the Boolean expression is true, the control flow jumps back and the statement in the loop is executed again. This process is repeated until the Boolean expression is false.
Example
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 ); }}This will produce the following results:
value of x : 10value of x : 11value of x : 12value of x : 13value of x : 14value of x : 15value of x : 16value of x : 17value of x : 18value of x : 19
for loop
The for loop is a loop control structure that can effectively write a loop of a specific number of times that needs to be executed.
When you know how many times a task needs to be repeated, a for loop is beneficial.
grammar
The syntax of a for loop is:
for(initialization; Boolean_expression; update){ //Statements}The following is the control process of a for loop:
The initialization step is performed first and only once. This step declares and initializes any loop control variables. There is no need to put a declaration here, just a semicolon needs to appear.
Next, the Boolean expression evaluates. If true, the loop body is executed. If it is false, the loop body will not be executed, and the process control will jump to the next statement passing through the for loop.
After that, when the loop body is executed for loop, the control process jumps to backup to the update statement. This statement allows updates to any loop control variables. This statement can be left blank, as long as a semicolon appears after a Boolean expression.
The Boolean expression now evaluates the calculation again. If true, loop execute and repeat the process (loop body, then update the steps, then boolean expression). After that, the boolean expression is false, the loop terminates.
Example
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"); } }}This will produce the following results:
value of x : 10value of x : 11value of x : 12value of x : 13value of x : 14value of x : 15value of x : 16value of x : 17value of x : 18value of x : 19
New features for loops in Java
As of Java 5, an enhanced for loop is introduced. This is mainly used for arrays.
grammar
The syntax for enhanced for loop is:
for(declaration : expression){ //Statements}Declaration: New declares a block variable, which is a variable that is compatible with the elements in the array you are accessing. The variable can be utilized within the for block and its value as the current array element will be the same.
Expression: This calculation result requires a loop array to be completed. An expression can be an array variable or a method call that returns an array.
Example
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(","); } }}This will produce the following results:
10, 20, 30, 40, 50, James, Larry, Tom, Lacy,
break keywords
The keyword break is used to stop the entire loop. The break keyword must be used in any loop or in a switch statement.
The keyword break will stop the execution of the innermost loop and start executing the next line of code after the block.
grammar
Break syntax is a separate statement in any loop:
Copy the code as follows: break
Example
public class Test { public static void main(String args[]) { int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ) { if( x == 30 ) { break; } System.out.print(x ); System.out.print("/n"); } }}This will produce the following results:
1020
continue keywords
The continue keyword can be used in the control structure of any link. It causes the loop to jump immediately to the next iteration of the loop.
In a for loop, the continue keyword causes the control flow to jump to the update statement immediately.
In a while loop or do/while loop, the control flow immediately jumps to the boolean expression.
grammar
The continue syntax is a separate statement in any loop:
Copy the code as follows:continue
Example
public static void main(String args[]) { int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ) { if( x == 30 ) { continue; } System.out.print(x ); System.out.print("/n"); } }}This will produce the following results:
10204050
Conditional judgment
There are two types of conditional judgment statements in Java, which are:
if statement:
An if statement consists of a Boolean expression followed by one or more statements.
grammar
The syntax of an if statement is:
if(Boolean_expression){ //Statements will execute if the Boolean expression is true}If the value of the Boolean expression is true, the block if statement in the code will be executed. If not true, the first set of code after the end of the if statement (after braces) will be executed.
Example
public class Test { public static void main(String args[]){ int x = 10; if( x < 20 ){ System.out.print("This is if statement"); } }}This will produce the following results:
This is if statement
if...else statement
Any if statement can be followed by an optional else statement. When the Boolean expression is false, the statement is executed.
grammar
The syntax of if...else is:
if(Boolean_expression){ //Executes when the Boolean expression is true}else{ //Executes when the Boolean expression is false}Example
public class Test { public static void main(String args[]){ int x = 30; if( x < 20 ){ System.out.print("This is if statement"); }else{ System.out.print("This is else statement"); } }}This will produce the following results:
This is else statement
if...else if...else statement
If can be followed by an optional else if...else statement. It is very useful to test a single if statement and an else if statement under different conditions.
There are a few points to keep in mind when using if , else if , else statements.
grammar
The syntax of if...else is:
if(Boolean_expression 1){ //Executes when the Boolean expression 1 is true}else if(Boolean_expression 2){ //Executes when the Boolean expression 2 is true}else if(Boolean_expression 3){ //Executes when the Boolean expression 3 is true}else { //Executes when the none of the above condition is true.}Example
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 statement"); } }}This will produce the following results:
Value of X is 30
Nested if...else statement
It is always a legal nested if-else statement, which means you can use an if or else if statement in another if or else if statement.
grammar
The syntax of nested if...else is as follows:
if(Boolean_expression 1){ //Executes when the Boolean expression 1 is true if(Boolean_expression 2){ //Executes when the Boolean expression 2 is true }}Because we have nested if statements, we can nest else if...else in a similar way.
Example
public class Test { public static void main(String args[]){ int x = 30; int y = 10; if( x == 30 ){ if( y == 10 ){ System.out.print("X = 30 and Y = 10"); } } }}This will produce the following results:
X = 30 and Y = 10
switch statement
The switch statement allows a variable to test a series of equality of worth. Each value is called a case, and the variables being started are checked for each case.
grammar
The syntax for enhanced for loops is:
switch(expression){ case value ://Statements break; //optional case value ://Statements break; //optional //You can have any number of case statements. default ://Optional //Statements}The following rules apply to switch statements:
Example
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("Well done"); break; case 'D' : System.out.println("You passed"); case 'F' : System.out.println("Better try again"); break; default : System.out.println("Invalid grade"); } System.out.println("Your grade is " + grade); }}Compile and run the above program that uses various command line parameters. This will produce the following results:
$ java TestWell done Your grade is a C