0% found this document useful (0 votes)
4 views

Looping Statements in Java

The document provides an overview of looping statements in Java, including while, do/while, and for loops, explaining their syntax and functionality. It highlights the use of break and continue statements within loops and offers sample code for practical application. Additionally, it includes exercises and assignments for users to practice implementing these concepts in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Looping Statements in Java

The document provides an overview of looping statements in Java, including while, do/while, and for loops, explaining their syntax and functionality. It highlights the use of break and continue statements within loops and offers sample code for practical application. Additionally, it includes exercises and assignments for users to practice implementing these concepts in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Looping

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:

• This example skips the value of 4:


for (int i = 0; i < 10;
i++) {
if (i == 4) {
continue;
}
System.out.println(i);
}
Break and Continue in While Loop
• You can also use break and continue in while
loops:
• Break Example
int i = 0;
while (i < 10) {
System.out.println(i);
i++;
if (i == 4) {
break;
}
}
Continue Example
int i = 0;
while (i < 10) {
if (i == 4) {
i++;
continue;
}
System.out.println(i);
i++;
}
Activity:
1. Write a program that prompts the user to input an integer and then
outputs the number with the digits reversed. For example, if the input
is 8, the output should be 87654321.
2. Write a program that prompts the user to input an integer and based
on the inputted integer, it then prints the sum of the even and odd
integers.
Sample Program output:
Enter an integer: 5
Sum of numbers: 9

You might also like