The while loop loops through a block of code as long as a specified condition is true
while (condition) {
// code block to be executed
}
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:
ex--
int i = 0;
while (i < 5) {
System.out.println(i);
i++;
}
key examples include "if-else" for conditional checks, "for" and "while" for looping, and "switch" for multiple condition checks.
task 1
package day1;
public class While_basic {
public static void main(String[] args) {
// TODO Auto-generated method stub
int count = 1;
while(count<=5)
{
System.out.println(count);
count=count+1;
}
}
}
out put --
1
2
3
4
5
task 2
public static void main(String[] args) {
int count = 1;
while(count<=10)
{
System.out.println(count);
count= count+2;
}
out put -
1
3
5
7
9
task 3
public static void main(String[] args) {
// TODO Auto-generated method stub
int count = 0;
while(count<=10)
{
System.out.println(count);
count= count+2;
out put --
0
2
4
6
8
10
Top comments (0)