Looping statement:

The Java for loop is a control flow statement that iterates a part of the programs multiple times. The Java while loop is a control flow statement that executes a part of the programs repeatedly on the basis of given boolean condition.

While Loop:

The Java while loop is used to iterate a part of the program repeatedly until the specified Boolean condition is true. As soon as the Boolean condition becomes false, the loop automatically stops.

Program:

class Notes{
public static void main(String args[]){
int n=1;

while (n<=9){
System.out.println(n);
n=n+2;
}
}
}

output:

sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac Notes.java
sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java Notes
1
3
5
7
9

if else if:

class Multiple{
public static void main(String args[]){
int n=1;
while(n<=50){
if(n%3==0)
{
System.out.println(“yes”);
}
else if(n%5==0){
System.out.println(“yes”);
}
else{
System.out.println(“no”);
}
n=n+1;
}
}
}

output:

sobhika@sobhika-HP-Laptop-15-db1xxx:~$ javac Multiple.java
sobhika@sobhika-HP-Laptop-15-db1xxx:~$ java Multiple
no
no
yes
no
yes
yes
no
no
yes
yes
no
yes
no
no
yes
no
no
yes
no
yes
yes
no
no
yes
yes
no
yes
no
no
yes
no
no
yes
no
yes
yes
no
no
yes
yes
no
yes
no
no
yes
no
no
yes
no
yes


Leave a comment

Design a site like this with WordPress.com
Get started