The for statement is like the while statement, i.e. you use it to create loop. The for statement has following syntax: for ( init ; booleanExpression ; update ) { statement (s) } |
|
The for statement will stop only if one of the following conditions is met: |
|
It is common to declare a variable and assign a value to it in the initialization part. The variable declared will be visible to the expression and update parts as well as to the statement block.For example, the following for statement loops five times and each time prints the value of i. Note that the variable i is not visible | |
anywhere else since it is declared within the for loop. | |
| |
The initialization part of the for statement is optional. | |
| |
The update statement is optional. | |
| |
You can even omit the booleanExpression part. | |
| |
If you compare for and while, you'll see that you can always replace the while statement with for. This is to say that | |
|
| |
Hi. Hi. Hi. Hi. Hi. Hi. Hi. Hi. |
The numerical for loop
// statements
}
| |
sum = 210 |
public class Main {
public static void main(String[] args) {
for (;;) {
System.out.println("Hello");
break;
}
}
}
//Hello
http://www.java2s.com/Tutorial/Java/0080__Statement-Control/
InfiniteForloopExample.htm
No comments:
Post a Comment