DCIT 22 – Computer Programming 1
Module 9: Loop Statements
Introduction:
In this module, students will explore the fundamental concepts of Java loop
statements, essential control structures that enable repetitive execution of code
blocks based on specified conditions. Loops provide an efficient and structured
approach to handle tasks that require repetition, reducing redundancy and
enhancing code readability. By leveraging loop statements like for, while, and do-
while, students can automate repetitive operations with precision. Additionally, loop
control statements like break and continue offer flexibility in managing program flow,
ensuring efficient execution and clarity in logic.
Objectives:
In this module the student should be able to:
1. Understand the purpose and importance of loop statements in Java
programming.
2. Differentiate between for, while, and do-while loops and identify the scenarios
where each is most suitable.
3. Write Java programs that utilize loops to solve practical problems.
4. Implement loop control statements (break, continue, and return) to manage
loop execution effectively.
5. Analyze and debug common issues in loop logic, ensuring efficient and
correct program flow.
Java Loop Statements
When Loops are Required?
There may be a situation when you need to execute a block of code several
number of times. In general, statements are executed sequentially: The first statement
in a function is executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more
complicated execution paths
In short, A loop statement allows us to execute a statement or group of
statements multiple times.
Java Loops
Java programming language provides the following types of loops to handle the
looping requirements:
Loop Description
while Repeats a statement or group of statements while a given
condition is true. It tests the condition before executing the
loop body.
for is a way to repeat a set of instructions several times. It helps
you write less code by automatically handling the starting,
stopping, and updating of a counter (called the loop variable).
Cavite State University – Naic
Information Technology Department
DCIT 22 – Computer Programming 1
Module 9: Loop Statements
do while is similar to a while loop, but with one key difference: it always
runs the loop at least once, because it checks the condition
after executing the loop body.
Loop control statements let you change the normal flow of a loop.
Instead of running the loop from start to finish every time, these
statements let you stop, skip, or jump out of the loop at specific points. For
example:
break stops the loop immediately and jumps out of it.
continue skips the rest of the current loop iteration and moves to
the next one.
return ends the loop and the entire function it's in
Java While Loop
Java while loop statement repeatedly executes a code block as long as a given
condition is true.
The while loop is an entry control loop, where conditions are checked before executing
the loop's body.
Syntax of while Loop:
Execution Process of a while loop
Here, statement(s) may be a single statement or a block of statements.
The condition may be any expression, and true is any non-zero value.
When executing, if the boolean_expression result is true, then the actions inside the
loop will be executed. This will continue as long as the expression result is true.
When the condition becomes false, program control passes to the line immediately
following the loop.
Cavite State University – Naic
Information Technology Department
DCIT 22 – Computer Programming 1
Module 9: Loop Statements
Flowchart of while loop:
Here, key point of the while loop is that the loop might not ever run. When the
expression is tested and the result is false, the loop body will be skipped and the first
statement after the while loop will be executed.
Example: Printing Numbers in a Range Using while Loop
In this example, we're showing the use of a while loop to print numbers starting from
10 to 19. Here we've initialized an int variable x with a value of 10. Then in while loop,
we're checking x as less than 20 and within while loop, we're printing the value of x
and incrementing the value of x by 1. While loop will run until x becomes 20. Once x
is 20, loop will stop execution and program exits.
Cavite State University – Naic
Information Technology Department
DCIT 22 – Computer Programming 1
Module 9: Loop Statements
Output:
Java do while loop
Java do while loop is similar to a while loop, except that a do while loop is
guaranteed to execute at least one time.
The do-while loop is an exit control loop where the condition is checked after
executing the loop's body.
Syntax of do while loop:
Execution Process of do while loop:
Notice that the Boolean expression appears at the end of the loop, so the
statements in the loop execute once before the Boolean is tested.
If the Boolean expression is true, the control jumps back up to do statement, and the
statements in the loop execute again. This process repeats until the Boolean
expression is false.
Cavite State University – Naic
Information Technology Department
DCIT 22 – Computer Programming 1
Module 9: Loop Statements
Flowchart of do while loop:
Example: Validating Positive Input Using a Do-While Loop
In this example, We begin by declaring an integer variable number. Inside the
do block, the program prompts the user to input a number, which is then stored in
the number variable using the Scanner class. After executing the code in the do
block, the condition number <= 0 is checked. If the number is zero or negative, the
loop repeats, prompting the user again. The loop continues until the user enters a
positive number, at which point the condition becomes false, and the program exits
the loop to display the valid input. The key feature of the do-while loop in this
example is that the prompt is executed at least once, ensuring the user is always
asked to input a value regardless of its validity.
Cavite State University – Naic
Information Technology Department
DCIT 22 – Computer Programming 1
Module 9: Loop Statements
Output:
Enter a positive number: -5
Enter a positive number: 0
Enter a positive number: 3
You entered: 3
Java for loop
A 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.
A for loop is useful when you know how many times a task is to be repeated. Just
like the while loop, the for loop is also an entry control loop where the given
condition executes first.
Syntax of for loop:
Statement 1 (initialization) is executed (one time) before the execution of the code
block.
Statement 2 (Boolean expression) defines the condition for executing the code
block.
Statement 3 is executed (every time) after the code block has been executed.
Parts of Java For Loop
In Java, the for loop is constructed (implemented) using three parts. The following
are the parts of a for loop in Java:
Initialization - Contains the initialization statement (s) of the loop counter.
Boolean expression - Contains the condition to be tested.
Body - Contains the statements to be iterated till the given Boolean
expression is true, also to update the loop counter.
Cavite State University – Naic
Information Technology Department
DCIT 22 – Computer Programming 1
Module 9: Loop Statements
Execution Process of a for Loop
The initialization step is executed first, and only once. This step allows you
to declare and initialize any loop control variables and this step ends with a
semi colon (;).
Next, the Boolean expression is evaluated. If it is true, the body of the loop
is executed. If it is false, the body of the loop will not be executed and control
jumps to the next statement past the for loop.
After the body of the for loop gets executed, the control jumps back up to the
update statement. This statement allows you to update any loop control
variables. This statement can be left blank with a semicolon at the end.
The Boolean expression is now evaluated again. If it is true, the loop executes
and the process repeats (body of loop, then update step, then Boolean
expression). After the Boolean expression is false, the for loop terminates.
Flowchart of for loop:
Cavite State University – Naic
Information Technology Department
DCIT 22 – Computer Programming 1
Module 9: Loop Statements
Example: program that print the numbers 0 to 4.
Output:
For loop example explanation:
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.
Nested for loop
A nested for loop is a for loop containing another for loop inside it.
Example: Print Tables from 1 to 10 Using Nested for Loop
Cavite State University – Naic
Information Technology Department
DCIT 22 – Computer Programming 1
Module 9: Loop Statements
Output:
Cavite State University – Naic
Information Technology Department