Looping Statements in Java
Looping Statements in Java
Statements in
Java
• Identify the looping statements used in Java.
• Discuss how each looping statements worked.
Objectives: • Demonstrate how looping statements work.
• Applying looping statements in creating programs.
• A loop statement is a series of steps or
sequence of statements executed repeatedly
zero or more times satisfying the given
condition is satisfied.
What is a
• Loops can execute a block of code as long as
looping a specified condition is reached.
statement? • Loops are handy because they save time,
reduce errors, and they make code more
readable.
While Loop
Loops in Java Do/While Loop
For Loop
Java While Loop
• The loops through a block of code as long as a specified
condition is true:
SYNTAX:
while (condition) {
// code block to be executed
}
Example
In the example below, the code in the loop will run, over and over again, as
long as a variable (i) is less than 5:
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
The Do/While Loop
• The is a variant of the while loop.
• This loop will execute the code block once, before checking if the
condition is true, then it will repeat the loop as long as the condition
is true.
SYNTAX:
do {
// code block to be executed
}
while (condition);
EXAMPLE
• The example below uses a do/while loop. The loop will always be
executed at least once, even if the condition is false, because the code
block is executed before the condition is tested:
int i = 0;
do {
System.out.println(i);
i++;
}
while (i < 5);
Example 2:
• Create a program using do/while that displays the even numbers from
1 to 10.
Sample code:
int i=2;
do {
System.out.println(i);
i=i+2;
}
while (i<=10);
Your turn: Try this code
Sample Problems
1. Create a program that displays the even numbers from 1 to 10.
2. Create a program that displays numbers divisible by 5 from 1-50.
3. Create a program that displays odd numbers from 1 to 20.
4. Create a program that accepts an input value from the user and
displays the sum of the values up to 0.
Sample Input
Enter an integer: 5
Sample output
The sum is 15.
Assignment:
Java For Loop
• When you know exactly how many times you want to loop through a
block of code, use the for loop instead of a while loop:
• SYNTAX:
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
SYNTAX EXPLAINED
• Statement 1 is executed (one time) before the execution of the code
block.
• Statement 2 defines the condition for executing the code block.
• Statement 3 is executed (every time) after the code block has been
executed.
•
EXAMPLE:
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
• Statement 1 sets a variable before the loop starts (int i = 0).
• Statement 2 defines the condition for the loop to run (i must be less
than 5). If the condition is true, the loop will start over again, if it is
false, the loop will end.
• Statement 3 increases a value (i++) each time the code block in the
loop has been executed.
This example will only print even values
between 0 and 10:
for (int i = 0; i <= 10; i = i + 2) {
System.out.println(i);
}
Java Break and Continue
• Break is used to "jump out" of a switch statement.
• The break statement can also be used to jump out of a loop.
This example jumps out of the loop when i is
equal to 4:
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
System.out.println(i);
}
Expected output: 0, 1, 2, 3
Java Continue
• The continue statement breaks one iteration (in the loop), if
a specified condition occurs, and continues with the next
iteration in the loop.
Example: