Facebook

while loops

While loop is another type of loops that can be used in Java. We put the condition first and then according to the condition, the subsequent statement is executed. Let's see syntax of while loop:

while(condition)
{
/*Statement to be executed;*/
/*iteration;*/
}

Program to print multiples of 3 between range 3-100.

package practice_udemy_java;
public class WhileLoop 
{
  public static void main(String[] args) 
  {
  int num = 3;
  System.out.println("Here are multiples of 3 between 3-100: \n ");
  while(num < 100)
  {
  System.out.println(num);
  num = num + 3;
  }
 }
}

Run

 

Program to print numbers in reverse order i.e from 100 which are multiples 0f both 4 and 5.

 

package practice_udemy_java;
public class NumbersReverse 
{
 public static void main(String[] args) 
 {
 int num = 100;
 while(num > 1)
 {
 if(num % 4 == 0 && num % 5 == 0)
 {
 System.out.println(num);
 }
 num--;
 }
}
}

Run

Rate this post

Comments

So empty here ... leave a comment!

Leave a Reply

Your email address will not be published. Required fields are marked *

Sidebar