Save
COMPUTER PROGRAMMING 2
Iteration Statements
Save
Share
Learn
Content
Leaderboard
Learn
Created by
Gerry Rosario
Visit profile
Cards (15)
Iteration Statement
the
iteration statements
in Java are
for
,
while
and
dowhile.
These statements create
loops.
Iteration Statement
A loop
repeatedly
executes the same set of
instructions
until a
termination
condition is met.
For Loop
is a
repetition
control
structure
that allows you to efficiently write a loop that needs to be executed a specific number of times.
For Loop
Syntax:
for (
initialization
;
condition
;
iteration
) {
// code to be executed repeatedly
}
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
// Print numbers from
1
to
5
using a
for
loop
While Loop
repeatedly
executes
a target
statement
as long as a given condition is
true.
While Loop Syntax:
while
(
condition
) {
// code to be executed repeatedly
}
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
// Print numbers from
1
to
5
using a
while loop
Do-While Loop
checks its condition at the
bottom
of the
loop.
the
condition
is evaluated
after
the execution of the loop's body.
Do-While Loop
Syntax:
do
{
// code to be executed repeatedly
}
while
(
condition
);
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
// Print numbers from
1
to
5
using a
do-while
loop
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
System.out.println("Sum: " + sum);
55
int i = 10;
while (i >= 1) {
System.out.println(i);
i--;
10
-
1
int i = 2;
while (i <= 10) {
System.out.println(i);
i += 2;
2
-
10
(
even numbers
)
String str = "Hello";
int i = str.length() - 1;
do {
System.out.println(str.charAt(i));
i--;
} while (i >= 0);
o
l
l
e
H