0% found this document useful (0 votes)
16 views5 pages

Lecture 12 - Repetition

The document explains different types of loops in Java: while loops, do-while loops, for loops, and nested loops. Each loop type has its syntax and use cases, such as executing code a fixed number of times or ensuring at least one execution. Examples illustrate how to implement these loops to perform tasks like printing numbers and creating a multiplication table.

Uploaded by

alfredjoso847
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)
16 views5 pages

Lecture 12 - Repetition

The document explains different types of loops in Java: while loops, do-while loops, for loops, and nested loops. Each loop type has its syntax and use cases, such as executing code a fixed number of times or ensuring at least one execution. Examples illustrate how to implement these loops to perform tasks like printing numbers and creating a multiplication table.

Uploaded by

alfredjoso847
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/ 5

While loop

● A while loop is a control flow statement that allows you to repeatedly execute
a block of code as long as a specified condition is true.
● The syntax of a while loop in Java is as follows:

while (condition) {
// code block to be executed
}

● Here, condition is a boolean expression that is evaluated before each


iteration of the loop.
● If the condition is true, the code block is executed; if the condition is false,
the loop is terminated and control is passed to the next statement after the
loop.
● The code block inside the loop can contain any valid Java statements,
including conditional statements, loops, and method calls.
● A while loop is useful when you don't know in advance how many times you
need to execute the code block.
● For example, you could use a while loop to repeatedly read input from the
user until they enter a valid input, or to process elements in a collection until
a certain condition is met.
● Consider this example of a while loop that prints the numbers from 1 to 5:

int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}

● In this example, the condition i <= 5 is true for the first five iterations of the
loop, so the code block inside the loop is executed five times.
● The loop terminates when i becomes 6, at which point the condition is false
and the loop exits.

1
Do-while loop
● A do-while loop is a control flow statement that allows you to repeatedly
execute a block of code at least once and then as long as a specified
condition is true.
● The syntax of a do-while loop in Java is as follows:

do {
// code block to be executed
} while (condition);

● Here, condition is a boolean expression that is evaluated after each iteration


of the loop.
● The code block is executed at least once, regardless of the value of
condition.
● If the condition is true after the first iteration, the code block is executed
again; if the condition is false, the loop is terminated and control is passed to
the next statement after the loop.
● The code block inside the loop can contain any valid Java statements,
including conditional statements, loops, and method calls.
● A do-while loop is useful when you want to ensure that the code block is
executed at least once, even if the condition is initially false.
● For example, you could use a do-while loop to repeatedly prompt the user
for input until they enter a valid input.
● Consider this example of a do-while loop that prints the numbers from 1 to 5:

int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);

2
For loop
● A for loop is a control flow statement that allows you to repeatedly execute a
block of code a fixed number of times.
● The syntax of a for loop in Java is as follows:

for (initialization; condition; update) {


// code block to be executed
}

● Here, initialization is an expression that is executed once before the loop


starts and is used to initialize the loop variable.
● condition is a boolean expression that is evaluated before each iteration of
the loop.
● If the condition is true, the code block is executed; if the condition is false,
the loop is terminated and control is passed to the next statement after the
loop.
● update is an expression that is executed after each iteration of the loop and
is used to update the loop variable.
● The code block inside the loop can contain any valid Java statements,
including conditional statements, loops, and method calls.
● A for loop is useful when you know in advance how many times you need to
execute the code block. For example, you could use a for loop to print the
numbers from 1 to 5:

for (int i = 1; i <= 5; i++) {


System.out.println(i);
}

● In this example, i is initialized to 1, and the condition i <= 5 is true for the first
five iterations of the loop.
● The code block inside the loop is executed five times, and i is incremented
by 1 after each iteration.

3
● The loop terminates when i becomes 6, at which point the condition is false
and the loop exits.
● You can use a for loop to iterate over the elements of an array or a collection
as well.
● In this case, the initialization statement initializes a loop variable that
represents the index of the element, and the condition checks if the index is
less than the length of the array or the size of the collection.
● The update statement increments the index by 1 after each iteration. Here's
an example that prints the elements of an array:

int[] arr = {1, 2, 3, 4, 5};


for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}

● In this example, i is initialized to 0, and the condition i < arr.length is true for
the first five iterations of the loop.
● The code block inside the loop is executed five times, and i is incremented
by 1 after each iteration. The loop terminates when i becomes 5, at which
point the condition is false and the loop exits.

Nested loop
● A nested loop is a loop inside another loop.
● This means that the code inside the outer loop will execute multiple times,
with the inner loop executing completely each time the outer loop runs.
● The inner loop can itself contain other loops and conditional statements.
● The basic syntax of a nested loop is as follows:

for (initialization1; condition1; update1) {


for (initialization2; condition2; update2) {
// code block to be executed
}

4
}

● Here, initialization1 is the initialization statement for the outer loop,


condition1 is the condition for the outer loop, and update1 is the update
statement for the outer loop.
● Similarly, initialization2, condition2, and update2 are the initialization
statement, condition, and update statement for the inner loop.
● The code block inside the inner loop can access the loop variables of both
the inner and outer loops, and can contain any valid Java statements,
including conditional statements, loops, and method calls.
● Nested loops are useful when you need to perform a repetitive task that
requires multiple iterations with different values of one or more loop
variables.
● For example, you could use a nested loop to print a multiplication table:

for (int i = 1; i <= 10; i++) {


for (int j = 1; j <= 10; j++) {
System.out.print(i * j + " ");
}
System.out.println();
}

● In this example, the outer loop iterates from 1 to 10, and the inner loop also
iterates from 1 to 10.
● The code block inside the inner loop prints the product of the loop variables i
and j, followed by a space.
● The code block inside the outer loop then prints a newline character to start
a new line.

You might also like