while (booleanExpression) {
// statement(s) (body of loop)
}
Что если нужно вывести сообщение 100 раз?
Повторим код по выводу 100 раз!
Вместо того чтобы писать один и тот же код 100 раз, можно использовать цикл.
while
statementwhile (booleanExpression) {
// statement(s) (body of loop)
}
while (booleanExpression)
// ONE statement
Do not recommend
// declare variables
int i = 1;
int n = 5;
// while loop from 1 to 5
while (i <= n) {
System.out.println(i);
i++;
}
1
2
3
4
5
Iteration | Variable | i <= n | Action |
---|---|---|---|
1st |
|
|
|
2nd |
|
|
|
3rd |
|
|
|
4th |
|
|
|
5th |
|
|
|
6th |
|
| The loop is terminated. |
// declare variables
int i = 1;
// infinity while loop
while (true) {
System.out.println(i);
i++;
}
1
2
...
do-while
statementdo {
// statement(s) (body of loop)
} while (booleanExpression);
do
// ONE statement
while (booleanExpression);
Do not recommend
int i = 1;
int n = 5;
// do-while loop from 1 to 5
do {
System.out.println(i);
i++;
} while (i <= n);
1
2
3
4
5
int i = 1;
// infinity do-while loop
do {
System.out.println(i);
i++;
} while (true);
1
2
...
for
statementfor (initialization; termination; increment) {
// statement(s) (body of loop)
}
for (initialization; termination; increment)
// ONE statement
Do not recommend
for (initialization; termination; increment) {
// statement(s) (body of loop)
}
The initialization expression initializes the loop; it’s executed ONCE, as the loop begins.
When the termination expression evaluates to false, the loop terminates.
The increment expression is invoked after EACH iteration through the loop
int n = 5;
// for loop
for (int i = 1; i <= n; i++) {
System.out.println(i);
}
1
2
3
4
5
Iteration | Variable | i <= n | Action |
---|---|---|---|
1st |
|
|
|
2nd |
|
|
|
3rd |
|
|
|
4th |
|
|
|
5th |
|
|
|
6th |
|
| The loop is terminated. |
int sum = 0;
int n = 1000;
// for loop
for (int i = 1; i <= n; i++) {
// body inside for loop
sum += i; // sum = sum + i
}
System.out.println("Sum = " + sum);
Sum = 500500
int sum = 0;
int n = 1000;
// for loop
for (int i = n; i >= 1; i--) {
// body inside for loop
sum += i; // sum = sum + i
}
System.out.println("Sum = " + sum);
Sum = 500500
// infinity for loop
for ( ; ; ) {
...
}
for (int i = 1; i <= 10; i--) {
System.out.println("Hello");
}
Hello
...
T[] array = initArray();
// for-each loop
for (T element : array) {
// statement(s)
}
T
- data type for elements from Array/Collection
// for-each loop
for (T element : array)
// ONE statement
T
- data type for elements from Array/Collection
// create an array
int[] numbers = {3, 9, 5, -5};
// for-each loop
for (int number: numbers) {
System.out.println(number);
}
3
9
5
-5
In the first iteration, item will be 3
.
In the second iteration, item will be 9
.
In the third iteration, item will be 5
.
In the fourth iteration, item will be -5
.
for
vs for-eachchar[] vowels = {'a', 'e', 'i', 'o', 'u'};
// iterating through an array using a for loop
for (int i = 0; i < vowels.length; ++ i) {
System.out.println(vowels[i]);
}
// iterating through an array using the for-each loop
for (char item: vowels) {
System.out.println(item);
}
a
e
i
o
u
while
statement
когда количество итераций зависит от результата вычисления в теле цикла
do-while
statement
когда количество итераций зависит от результата вычисления в теле цикла, НО нужно выполнить как минимум одну итерацию
for
statement
когда количество итераций заранее известно или легко вычисляется
for-each statement
при работе с каждым элементом Collection или Array