While Loop

While loop executes group of Java statements as long as the boolean condition evaluates to true.

While loop syntax

while(){

;

}

Block of statements is any valid Java code. Boolean condition is any valid Java expression that evaluates to boolean value. Braces are options if there is only one statement to be executed.

While loop evaluates the boolean expression, and if it is true, it executes the block of statements. It continuously executes the block of statements until the boolean condition evaluates to false value. After that loop is terminated and statement immediately after the loop body will be executed.

While Loop

26 Comments

  • is there any other way to write this code
    if (quantity>=5)
    {
    System.out.println(“Congrats!! You get a discount of 5%.”);
    }
    else
    {
    System.out.println(“you can avail a discount of 5% when you make a purchase of 5 items.”);
    }
    Can Smith

    • public class loopexample
      {
      public static void main(String args[])
      {
      int[] a={ 1,2,3,4,5,6,7,8,};
      System.out.print(“using do while”);
      int i=0;
      int average1=0;
      do
      {
      average1+=a[i];
      i++;
      }
      while(i<a.length); system.out.println(“the=”” average=”” of=”” the=”” array”+average1);=”” system.out.print(“using=”” for=”” loop”);=”” int=”” average2=”0;” for(i=”0;i&lt;a.length;i++)” {=”” average2+=”a[i];” }=”” system.out.println(“the=”” average=”” of=”” the=”” array”+average2);=”” }=”” }=””>

      • please help me. i need a code.in our ACTIVITY this is the question to do..
        (triangle printing program) write an application that displays the following patterns separately, one below the other .use “for loops” to generate the patterns. all asterisks(*) should be printed by a single statements of the form System.out.println(*); w/c causes the asterisks to print side by side .A statement of the form System.out.print(‘ ‘); can be used to display a space for the last two patterns. there should be no other output statements in the program. [hint: the last two patterns requires each line begin with an appropriate number of blank spaces]

        a)

        *
        **
        ***
        ****
        *****
        ******
        *******
        ********
        *********
        **********
        b)
        **********
        *********
        ********
        *******
        ******
        *****
        ****
        ***
        **
        *

        c)
        **********
        *********
        ********
        *******
        ******
        *****
        ****
        ***
        **
        *

        d)

        *
        **
        ***
        ****
        *****
        ******
        *******
        ********
        *********
        **********

        • class Star {
          public static void main(String agrs[]) {
          System.out.println(“The Pattern is”);
          for (int i = 0; i < 5; i++) {
          for (int j = 0; j < 5; j++) {
          if (j <= i) {
          System.out.print(” *”);
          } else {
          System.out.print(” “);
          }
          }
          System.out.println();
          }
          }
          }

          output:
          *
          **
          ***
          ****
          *****

            • We can use Io.scanners input ones but i have written the code as normal while loop .Hope this helps you 🙂 If you want to change the pswd then it shows its a wrong pin.It’s a basic programming in while loop.

              public class DoWhile {

              public static void main(String[] args) {

              String name = “john”;

              String pswd = “2999”;

              do{

              System.out.println(“Enter the password :” +pswd);

              if(pswd==”2999″){

              System.out.println( “Enter the amount”);

              System.out.println(“draw the money”);

              System.out.println(“withdrawl money”);

              System.out.println(“Reciept”);

              }else

              System.out.println(“Wrong pin number”);

              break;

              }while(name==”john”);

              }

              }

  • Please help me to answer the question:

    . Write an input validation loop that asks the user to enter a number in the range of

    10 through 24.

    • public class pyramid
      {
      public static void main(String args[])
      {
      int i=0;
      while(i<=5)
      {
      int r=0;
      while(r<i) {=”” system.out.print(“*”);=”” r++;=”” }=”” system.out.println();=”” i++;=”” }=”” }=”” }=””>

    • package mypackage2;

      public class pyramid {

      public static void main(String[] args) {

      // TODO Auto-generated method stub

      int i,j;

      for(i=0;i<5;i++)

      {

      for(j=0;j<=i;j++)

      {

      System.out.print(“*”);

      }

      System.out.println();

      }

      }

      }

    • Well this kind of output only possible through for loop .This is code for do-while .Hope this helps you 🙂
      public class DoWhile {

      public static void main(String[] args) {
      short c = 0;

      do{
      System.out.println(“*”);

      System.out.println(“**”);
      System.out.println(“***”);
      System.out.println(“****”);
      System.out.println(“*****”);
      c++;
      }while(c<1);

      }

      }

    • class Subh

      {

      public static void main(String arg[])

      {

      int i,j,n=5;

      for(i=1;i<=n;i++)

      {

      for(j=1;j<=i;j++)

      {

      System.out.print(“*”);

      }

      System.out.println();

      }

      }

      }
      for more programs contact me at

  • please help me
    (Find the smallest n such that n^2 > 12,000) Use a while loop to find the smallest

    integer n such that n^2 is greater than 12,000

    • class pattern1
      {
      public void print()
      {
      for (int i=1;i<=5;i++)
      {
      for (int j=1;j<=i;j++)
      {
      System.out.print(” * “);
      }
      System.out.println();
      }
      }
      }

      • public class DoWhile {

        public static void main(String[] args) {

        short c = 0;

        do{

        System.out.println(“*”);

        System.out.println(“**”);

        System.out.println(“***”);

        System.out.println(“****”);

        System.out.println(“*****”);

        c++;

        }while(c<1);

        }

        }

    • public class While {

      public static void main(String[] args) {
      short i = 0;

      while(i<1){
      System.out.println(“*”);
      System.out.println(“**”);
      System.out.println(“***”);
      System.out.println(“****”);
      System.out.println(“*****”);
      System.out.println(“******”);

      i++;
      }

      }
      }

  • how to create pyramid that the user must input a number for example i input 5 and it will be like this

    1 2 3 4 5
    1 2 3 4
    1 2 3
    1 2
    1

    or like this

    1
    1 2
    1 2 3
    1 2 3 4
    1 2 3 4 5

    • class ssa

      {

      public static void main(String arg[])

      {

      int i,j,n=5;

      for(i=1;i<=n;i++)

      {

      for(j=1;j<=i;j++)

      {

      System.out.print(j);

      }

      System.out.println();

      }

      }

      }

      More details contact me

Sponsors

Facebook Fans

Online Shopping – Flipkart