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

Java While Loop

The while loop executes a block of code as long as a condition is true. It checks the condition before executing the code block. The do/while loop is similar but executes the code block once before checking the condition, then repeats as long as the condition remains true. Both can be used to repeatedly execute code, such as printing multiples of a number by incrementing a counter variable in the code block.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Java While Loop

The while loop executes a block of code as long as a condition is true. It checks the condition before executing the code block. The do/while loop is similar but executes the code block once before checking the condition, then repeats as long as the condition remains true. Both can be used to repeatedly execute code, such as printing multiples of a number by incrementing a counter variable in the code block.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Java 

While Loop

• The while loop loops through a block of code as long as a specified


condition is true: .
• A loop statement allows us to execute a statement or group of
statements multiple times and following is the general form of a loop
statement in most of the programming languages
Syntax
while (condition) {
// code block to be executed
}
Example
int i = 0; Output:
while (i < 5) { 0
System.out.println(i); 1
i++; 2
} 3
4
The Do/While Loop
• The do/while loop 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
Output:
int i = 0;
do {
0
1
System.out.println(i);
2
i++;
3
}
4
while (i < 5);
Let’s try this
• Print a multiples of 5 using java
Leaning task 3.1
• Write a program in Java that allows user to input a positive
integer and display the multiples of a given integer.

You might also like