The enhanced for loop, new to Java 6, is a specialized for loop that simplifies looping through an array or a collection. In this chapter we're going to focus on using the enhanced for to loop through arrays. We'll revisit the enhanced for as we discuss collections in the future chapter.
Instead of having three components, the enhanced for has two.
for(declaration : expression)
The two pieces of the for statement are
• declaration The newly declared block variable, of a type compatible with the elements of the array you are accessing. This variable will be available within the for block, and its value will be the same as the current array element.
• expression This must evaluate to the array you want to loop through. This could be an array variable or a method call that returns an array. The array can be any type: primitives, objects, even arrays of arrays.
Using the above definitions, let's look at some legal and illegal enhanced for declarations:
String [] sNums = {"one", "two", "three"};
// legal 'for' declarations
for(String s : sNums) ; // loop thru the array of Strings
The enhanced for loop assumes that, barring an early exit from the loop, you'll always loop through every element of the array. The following discussions of break and continue apply to both the basic and enhanced for loops.
You may exit a nested loop in Java using a break with a label for the outer loop.
ComparisonThe conditions for both the 'while' and 'for' loop are exactly the same, but in each flow structure the conditions are placed in different locations. A 'for' loop places the full condition within the 'for' loop whereas the 'while' loop places the counter variable outside of the loop.The 'for' loop is used when the length of the loop is known whereas the 'while' loop is usually used when the length of the loop is unknown.'for' loop exampleWithin the parentheses the complete condition is contained. The condition has 3 parts delimited by a colon ';'. The first part sets the counter 'int i' to 0, the second part tells the loop to stop when the counter is 5. The third part tells java to increment the counter by 1 value each time the loop iterates.for ( int i = 0; i < 5; i++)'while' loop exampleWith the 'while' loop the counter is initialized outside of the 'while' loop. Inside the parentheses of the loop the condition is set to stop looping when the counter reaches a value of 5. Inside of the 'while' loop block the counter is set to increment by 1 value each time the loop iterates.int i = 0;while ( i < 5 ) {i++}
There are 4 different looping statements in Java. They are:While loopDo-While loopFor loopFor Each loop
12345 1234 123 12 1
With two nested loops. In the outer loop, a variable - we might call it "n" - would go from 5 down to 1, in the inner loop, the variable (whatever you call it) goes from 5 down to n. Write out each digit in the inner loop; in the outer loop you need to add the extra space.
You may exit a nested loop in Java using a break with a label for the outer loop.
ComparisonThe conditions for both the 'while' and 'for' loop are exactly the same, but in each flow structure the conditions are placed in different locations. A 'for' loop places the full condition within the 'for' loop whereas the 'while' loop places the counter variable outside of the loop.The 'for' loop is used when the length of the loop is known whereas the 'while' loop is usually used when the length of the loop is unknown.'for' loop exampleWithin the parentheses the complete condition is contained. The condition has 3 parts delimited by a colon ';'. The first part sets the counter 'int i' to 0, the second part tells the loop to stop when the counter is 5. The third part tells java to increment the counter by 1 value each time the loop iterates.for ( int i = 0; i < 5; i++)'while' loop exampleWith the 'while' loop the counter is initialized outside of the 'while' loop. Inside the parentheses of the loop the condition is set to stop looping when the counter reaches a value of 5. Inside of the 'while' loop block the counter is set to increment by 1 value each time the loop iterates.int i = 0;while ( i < 5 ) {i++}
There are 4 different looping statements in Java. They are:While loopDo-While loopFor loopFor Each loop
To determine the number of iterations a loop runs in Java, you can use a counter variable that increments each time the loop runs. You can also use a conditional statement to check when the loop should stop running. By keeping track of the counter variable, you can determine the total number of iterations the loop has executed.
for loop function
In Java, you can use the "break" statement within a "for" loop to exit the loop prematurely. When the "break" statement is encountered, the loop will immediately stop executing and the program will continue with the code after the loop.
in a loop
12345 1234 123 12 1
public static void main(String[] args) { for(int i=1;i<5;i++) { for(int j=1;j<5;j++) { System.out.print(i); } System.out.println(); } }
1) use for loop 2) do while loop
With two nested loops. In the outer loop, a variable - we might call it "n" - would go from 5 down to 1, in the inner loop, the variable (whatever you call it) goes from 5 down to n. Write out each digit in the inner loop; in the outer loop you need to add the extra space.
All loops available in Java (for, while, do-while) have a loop termination condition that would get executed during every iteration of the loop. Without checking the loop condition the loop cannot be terminated and hence avoiding the loop condition check during iteration is not logic and Java does not do it.