Explore the English language on a new scale using AI-powered English language navigator.
Loop statements in JavaIn previous articles you learned about variables and types and now it's time to speak about control statements in Java. There are several types of statements in Java, which are useful for repeating things. Before we are going any further let's check simple example printing "Hello!" thing to the console ten times: public class Loops { public static void main(String[] args) { System.out.println("I am about to print \"Hello!\" ten times."); System.out.println("Hello!"); System.out.println("Hello!"); System.out.println("Hello!"); System.out.println("Hello!"); System.out.println("Hello!"); System.out.println("Hello!"); System.out.println("Hello!"); System.out.println("Hello!"); System.out.println("Hello!"); System.out.println("Hello!"); System.out.println("Uff... It was long. Is there a way to compact repeating things?"); } } I am about to print "Hello!" ten times. It was long, wasn't it? In a big program one may need to repeat things hundreds of times or, most often get known how many times things are to be repeated only during the program run. Let's compact the code using for loop. public class Loops { public static void main(String[] args) { System.out.println("Let's compact the code using for loop."); int i; for (i = 0; i < 10; i++) System.out.println("Hello!"); System.out.println("Now the code looks much better."); } } Let's compact the code using for loop.
Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Hello! Now the code looks much better. Learning the magic of the for loopThe for statement consists of three elements:
In the example above, counter i takes on the following values: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Hence, the loop's body is repeated ten times. Using counter you may track the current step: public class ForLoop { public static void main(String[] args) { System.out.println("Let's count to 10:"); for (int i = 1; i <= 10; i++) System.out.println(i); } } Let's count to 10:
1 2 3 4 5 6 7 8 9 10 There are slight changes in the code we would like to comment.
Note. One is allowed to change counter inside the for loop, but it is considered as a bad programming style. public class ForLoop { public static void main(String[] args) { System.out.println("Let's count to 10:"); for (int i = 1; i <= 10; i += 2) { System.out.println(i); // bad programming style i--; } } } The while and do-while statementsThe while statement executes its body, while specified condition is true. To be more illustrative, let's rewrite the counting example: public class WhileLoop { public static void main(String[] args) { System.out.println("Let's count to 10 using while:"); int i = 1; while (i <= 10) { System.out.println(i); i++; } } }
Let's count to 10 using while:
1 2 3 4 5 6 7 8 9 10 Notice that while statement checking the condition before it executes body for the first time. So, it's possible, that it wouldn't execute at all: public class WhileLoop { public static void main(String[] args) { System.out.println("Start counting..."); int i = 11; while (i <= 10) { System.out.println(i); i++; } System.out.println("It seems that no lines have been printed."); } } Start counting... The do-while statement checks the condition after it executes body for the first time. Hence, it always executed at least one time, even if the condition is always false: public class DoWhileLoop { public static void main(String[] args) { System.out.println("Start counting..."); int i = 1; do { System.out.println(i); i++; } while (false); } } Start counting... Note. The for loop checks it's condition before: public class ForLoop { public static void main(String[] args) { System.out .println("Will the following for loop repeat at least once?"); for (int i = 1; i < 1; i++) { System.out.println("Repeated!"); } System.out.println("No, it doesn't."); } } Will the following for loop repeat at least once? Congratulations! Now, you have one more tool in your programming inventory. |