YouTip LogoYouTip

Java Loop

# Java Loop Structures - for, while, and do...while Statements in a sequential structure can only be executed once. If you want the same operation to be performed multiple times, you need to use a loop structure. There are three main loop structures in Java: * **while** loop * **do…while** loop * **for** loop An enhanced for loop, primarily used for arrays, was introduced in Java 5. * * * ## while Loop The while loop is the most basic loop. Its structure is: while( boolean expression ){//loop body} As long as the boolean expression evaluates to true, the loop will keep executing. ### Example ## Test.java File Code: 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"); }}} The compilation and execution result of the above example is as follows: value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19 * * * ## do…while Loop For the while statement, if the condition is not met, the loop cannot be entered. But sometimes we need to execute the loop body at least once, even if the condition is not met. The do…while loop is similar to the while loop, with the difference that the do…while loop will execute at least once. do { //code statements}while(boolean expression); **Note:** The boolean expression is after the loop body, so the statement block has already been executed before the boolean expression is checked. If the boolean expression evaluates to true, the statement block will keep executing until the boolean expression evaluates to false. ### Example ## Test.java File Code: 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); }} The compilation and execution result of the above example is as follows: value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19 * * * ## for Loop Although all loop structures can be represented using while or do...while, Java provides another statement β€” the for loop, which makes some loop structures simpler. The number of times a for loop executes is determined before execution. The syntax is as follows: for(initialization; boolean expression; update){//code statements} Here are some points about the for loop: * The initialization step is executed first. You can declare a type, but you can initialize one or more loop control variables, or it can be an empty statement. * Then, the boolean expression is evaluated. If it is true, the loop body is executed. If it is false, the loop terminates, and execution continues with the statements after the loop body. * After one iteration of the loop, the loop control variables are updated. * The boolean expression is evaluated again. The loop repeats the above process. ### Example ## Test.java File Code: 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"); }}} The compilation and execution result of the above example is as follows: value of x : 10 value of x : 11 value of x : 12 value of x : 13 value of x : 14 value of x : 15 value of x : 16 value of x : 17 value of x : 18 value of x : 19 * * * ## Java Enhanced for Loop Java 5 introduced an enhanced for loop, primarily used for arrays. The syntax for the Java enhanced for loop is as follows: for(declaration : expression){//code statements} **Declaration:** Declares a new local variable, whose type must match the type of the array elements. Its scope is limited to the loop statement block, and its value is equal to the value of the current array element. **Expression:** The expression is the name of the array to be accessed, or a method that returns an array. ### Example ## Test.java File Code: 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(","); }}} The compilation and execution result of the above example is as follows: 10,20,30,40,50,James,Larry,Tom,Lacy, * * * ## break Keyword The break keyword is mainly used in loop statements or switch statements to exit the entire statement block. break exits the innermost loop and continues executing the statements below that loop. ### Syntax The usage of break is simple; it is a statement within a loop structure: break; ### Example ## Test.java File Code: public class Test{public static void main(String[]args){int[]numbers = {10, 20, 30, 40, 50}; for(int x : numbers){// Exit the loop when x equals 30 if(x == 30){break; }System.out.print(x); System.out.print("n"); }}} The compilation and execution result of the above example is as follows: 1020 * * * ## continue Keyword The continue keyword is applicable to any loop control structure. Its function is to make the program immediately jump to the next iteration of the loop. In a for loop, the continue statement makes the program immediately jump to the update statement. In a while or do…while loop, the program immediately jumps to the boolean expression evaluation statement. ### Syntax continue is a simple statement within the loop body: continue; ### Example ## Test.java File Code: public class Test{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"); }}} The compilation and execution result of the above example is as follows: 10204050
← Java If Else SwitchJava Operators β†’