Java Tutorial/Statement Control/For Loop — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 05:19, 1 июня 2010
Содержание
- 1 A loop allows you to execute a statement or block of statements repeatedly
- 2 Declare multiple variables in for loop
- 3 For statement in detail
- 4 Infinite For loop Example
- 5 initialization_expression: define two variables in for loop
- 6 Java"s "labeled for" loop
- 7 Keeping the middle element only in for loop
- 8 Multiple expressions in for loops
- 9 Nested for Loop
- 10 Print out a Diamond
- 11 The for Statement
- 12 The numerical for loop
- 13 To omit any or all of the elements in "for" loop: but you must include the semicolons
- 14 Using the Floating-Point Values as the control value in a for loop
A loop allows you to execute a statement or block of statements repeatedly
public class MainClass {
public static void main(String[] args) {
for (int i = 0; i < 8; i++) {
System.out.println("Hi.");
}
}
}
Hi. Hi. Hi. Hi. Hi. Hi. Hi. Hi.
Declare multiple variables in for loop
public class Main {
public static void main(String[] args) {
for (int i = 0, j = 1, k = 2; i < 5; i++){
System.out.println("I : " + i + ",j : " + j + ", k : " + k);
}
}
}
/*
I : 0,j : 1, k : 2
I : 1,j : 1, k : 2
I : 2,j : 1, k : 2
I : 3,j : 1, k : 2
I : 4,j : 1, k : 2
*/
For statement in detail
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.
public class MainClass {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println(i + " ");
}
}
}
Infinite For loop Example
public class Main {
public static void main(String[] args) {
for (;;) {
System.out.println("Hello");
break;
}
}
}
//Hello
initialization_expression: define two variables in for loop
public class MainClass {
public static void main(String[] arg) {
int limit = 10;
int sum = 0;
for (int i = 1, j = 0; i <= limit; i++, j++) {
sum += i * j;
}
System.out.println(sum);
}
}
330
Java"s "labeled for" loop
public class MainClass {
public static void main(String[] args) {
int i = 0;
outer: for (; true;) {
inner: for (; i < 10; i++) {
System.out.println("i = " + i);
if (i == 2) {
System.out.println("continue");
continue;
}
if (i == 3) {
System.out.println("break");
i++;
break;
}
if (i == 7) {
System.out.println("continue outer");
i++;
continue outer;
}
if (i == 8) {
System.out.println("break outer");
break outer;
}
for (int k = 0; k < 5; k++) {
if (k == 3) {
System.out.println("continue inner");
continue inner;
}
}
}
}
}
}
i = 0 continue inner i = 1 continue inner i = 2 continue i = 3 break i = 4 continue inner i = 5 continue inner i = 6 continue inner i = 7 continue outer i = 8 break outer
Keeping the middle element only in for loop
public class MainClass {
public static void main(String[] arg) {
int limit = 10;
int sum = 0;
int i = 1;
for (; i <= limit;) {
sum += i++;
}
System.out.println(sum);
}
}
55
Multiple expressions in for loops
public class Main {
public static void main(String[] args) {
for (int i = 0, j = 0; i < 5; i++, j--)
System.out.println("i = " + i + " j= " + j);
}
}
/*
i = 0 j= 0
i = 1 j= -1
i = 2 j= -2
i = 3 j= -3
i = 4 j= -4
*/
Nested for Loop
public class MainClass {
public static void main(String[] args) {
long limit = 20L;
long factorial = 1L;
for (long i = 1L; i <= limit; i++) {
factorial = 1L;
for (long factor = 2; factor <= i; factor++) {
factorial *= factor;
}
System.out.println(i + "! is " + factorial);
}
}
}
1! is 1 2! is 2 3! is 6 4! is 24 5! is 120 6! is 720 7! is 5040 8! is 40320 9! is 362880 10! is 3628800 11! is 39916800 12! is 479001600 13! is 6227020800 14! is 87178291200 15! is 1307674368000 16! is 20922789888000 17! is 355687428096000 18! is 6402373705728000 19! is 121645100408832000 20! is 2432902008176640000
Print out a Diamond
class Diamond {
public static void main(String[] args) {
for (int i = 1; i < 10; i += 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
for (int i = 7; i > 0; i -= 2) {
for (int j = 0; j < 9 - i / 2; j++)
System.out.print(" ");
for (int j = 0; j < i; j++)
System.out.print("*");
System.out.print("\n");
}
}
}
/*
*
***
*****
*******
*********
*******
*****
***
*
*/
The for Statement
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)
}
- init is an initialization that will be performed before the first iteration.
- booleanExpression is a boolean expression which will cause the execution of statement(s) if it evaluates to true.
- update is a statement that will be executed after the execution of the statement block.
- init, expression, and update are optional.
The for statement will stop only if one of the following conditions is met:
- booleanExpression evaluates to false
- A break or continue statement is executed
- A runtime error occurs.
The numerical for loop
for (initialization_expression ; loop_condition ; increment_expression) {
// statements
}
sum = 210
To omit any or all of the elements in "for" loop: but you must include the semicolons
public class MainClass {
public static void main(String[] arg) {
int limit = 10;
int sum = 0;
for (int i = 1; i <= limit;) {
sum += i++;
}
System.out.println(sum);
}
}
55
Using the Floating-Point Values as the control value in a for loop
public class MainClass {
public static void main(String[] arg) {
for (double radius = 1.0; radius <= 2.0; radius += 0.2) {
System.out.println("radius = " + radius + "area = " + Math.PI * radius * radius);
}
}
}
radius = 1.0area = 3.141592653589793 radius = 1.2area = 4.523893421169302 radius = 1.4area = 6.157521601035994 radius = 1.5999999999999999area = 8.04247719318987 radius = 1.7999999999999998area = 10.178760197630927 radius = 1.9999999999999998area = 12.566370614359169