A for loop works similarly to most programing languages. An example of a for loop is
for(i=0; i<10; i++)
{
}
The code you want to be operated every loop should be between the brackets. The first part of a for loop is the declaration. The second part, is the comparison used to determine if the loop should continue, and the third part is what operation should be done after every loop.
The Basic for Loop
The for loop is especially useful for flow control when you already know how many times you need to execute the statements in the loop's block. The for loop declaration has three main parts, besides the body of the loop:
• Declaration and initialization of variables
• The boolean expression (conditional test)
• The iteration expression
The three for declaration parts are separated by semicolons. The following two examples demonstrate the for loop. The first example shows the parts of a for loop in a pseudocode form, and the second shows a typical example of a for loop.
for (/*Initialization*/ ; /*Condition*/ ; /* Iteration */) {
/* loop body */
}
Ex:
for (int i = 0; i<10; i++) { System.out.println("i is " + i); }
The Basic for Loop: Declaration and Initialization
The first part of the for statement lets you declare and initialize zero, one, or multiple variables of the same type inside the parentheses after the for keyword. If you declare more than one variable of the same type, then you'll need to separate them with commas as follows:
for (int x = 10, y = 3; y > 3; y++) { }
The declaration and initialization happens before anything else in a for loop. And whereas the other two parts-the boolean test and the iteration expression-will run with each iteration of the loop, the declaration and initialization happens just once, at the very beginning. You also must know that the scope of variables declared in the for loop ends with the for loop! The following demonstrates this:
for (int x = 1; x < 2; x++) { System.out.println(x); // Legal } System.out.println(x); // Not Legal! x is now out of scope If you try to compile this, you'll get something like this: Test.java:19: cannot resolve symbol symbol : variable x location: class Test System.out.println(x); ^
Basic for Loop: Conditional Expression
The next section that executes is the conditional expression, which (like all other conditional tests) must evaluate to a boolean value. You can have only one logical expression, but it can be very complex. Look out for code that uses logical expressions like this:
for (int x = 0; ((((x < 10) && (y-- > 2)) | x == 3)); x++) { }
The preceding code is legal, but the following is not:
for (int x = 0; (x > 5), (y < 2); x++) { } // too many //expressions The compiler will let you know the problem: TestLong.java:20: ';' expected for (int x = 0; (x > 5), (y < 2); x++) { } ^ The rule to remember is this: You can have only one test expression. In other words, you can't use multiple tests separated by commas, even though the other two parts of a for statement can have multiple parts.
Basic for Loop: Iteration Expression
After each execution of the body of the for loop, the iteration expression is executed. This is where you get to say what you want to happen with each iteration of the loop. Remember that it always happens after the loop body runs! Look at the following:
for (int x = 0; x < 1; x++) { // body code that doesn't change the value of x } The preceding loop executes just once. The first time into the loop x is set to 0, then x is tested to see if it's less than 1 (which it is), and then the body of the loop executes. After the body of the loop runs, the iteration expression runs, incrementing x by 1. Next, the conditional test is checked, and since the result is now false, execution jumps to below the for loop and continues on. Keep in mind that barring a forced exit, evaluating the iteration expression and then evaluating the conditional expression are always the last two things that happen in a for loop! Examples of forced exits include a break, a return, a System.exit(), or an exception, which will all cause a loop to terminate abruptly, without running the iteration expression. Look at the following code: static boolean doSomething() { for (int x = 0; x < 3; x++) { System.out.println("in for loop"); return true; } return true; } Running this code produces in for loop The statement only prints once, because a return causes execution to leave not just the current iteration of a loop, but the entire method. So the iteration expression never runs in that case. Basic for Loop: for Loop Issues None of the three sections of the for declaration are required! The following example is perfectly legal (although not necessarily good practice): for( ; ; ) { System.out.println("Inside an endless loop"); } In the preceding example, all the declaration parts are left out so the for loop will act like an endless loop. For the exam, it's important to know that with the absence of the initialization and increment sections, the loop will act like a while loop. The following example demonstrates how this is accomplished: int i = 0; for (;i<10;) { i++; //do some other work } The next example demonstrates a for loop with multiple variables in play. A comma separates the variables, and they must be of the same type. Remember that the variables declared in the for statement are all local to the for loop, and can't be used outside the scope of the loop. for (int i = 0,j = 0; (i<10) && (j<10); i++, j++) { System.out.println("i is " + i + " j is " +j); } The last thing to note is that all three sections of the for loop are independent of each other. The three expressions in the for statement don't need to operate on the same variables, although they typically do. But even the iterator expression, which many mistakenly call the "increment expression," doesn't need to increment or set anything; you can put in virtually any arbitrary code statements that you want to happen with each iteration of the loop. Look at the following: int b = 3; for (int a = 1; b != 1; System.out.println("hii")) { b = b - a; } The preceding code prints hii hii
You may exit a nested loop in Java using a break with a label for the outer loop.
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.
Java has three kinds of loops 1. For Loop 2. While Loop 3. Do - While Loop Both For loop and While loop would iterate through a certain lines of code within the loop's limit as long as the loop condition is satisfied. A do while loop would execute the loop once even before checking the condition. So in a do while loop, even if the loop condition is not satisfied the loop would execute once. Example Declarations: for(int i = 0; i < n; i++) { ..... } while (i < n) { ... i++; } do { ... i++; } while (i < n) ;
You use loops in Java when you want a set of actions to be repeated until a particular condition is met or for a certain number of times.The different types of loops in Java are:For LoopsDo-While LoopsWhile Loops
The control structures used in java script are if-statement, for-loop, for-in loop, while loop,do-while loop, switch-statement, with-statement. try-catch-finally statements.
You may exit a nested loop in Java using a break with a label for the outer loop.
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
1) use for loop 2) do while loop
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.
The do while loop is also called an exit condition loop in c, c++, and java.
A nested loop is a (inner) loop that appears in the loop body of another (outer) loop. The inner or outer loop can be any type: while, do while, or for. For example, the inner loop can be a while loop while an outer loop can be a for loop.
Exactly what do you mean by 'C program in Java'
Java has three kinds of loops 1. For Loop 2. While Loop 3. Do - While Loop Both For loop and While loop would iterate through a certain lines of code within the loop's limit as long as the loop condition is satisfied. A do while loop would execute the loop once even before checking the condition. So in a do while loop, even if the loop condition is not satisfied the loop would execute once. Example Declarations: for(int i = 0; i < n; i++) { ..... } while (i < n) { ... i++; } do { ... i++; } while (i < n) ;
You use loops in Java when you want a set of actions to be repeated until a particular condition is met or for a certain number of times.The different types of loops in Java are:For LoopsDo-While LoopsWhile Loops
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.