Results 1 to 6 of 6

Thread: How to use the do-while loop statement in java program?

  1. #1
    Join Date
    Aug 2009
    Posts
    51

    How to use the do-while loop statement in java program?

    Hi friends,
    I am second year Computer Science student. I recently started learning java language. In our last lecture we learn do-while loop statement. But still I don't understand how to use this in java program. Can anyone tell me how to use the do-while loop statement in java program? Please help me.
    Thank you.
    Last edited by KALIDA; 27-01-2010 at 05:43 PM.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to use the do-while loop statement in java program?

    Hey it is very easy process to use do- while loop in the Java programming language. Rather than evaluating the expression at the beginning like we do in while loop, the do-while loop evaluates its expression at the end of the loop. Because of this the loop executes at least once during the program execution even if it is wrong at first time.


    Code:
    package org.kodejava.example.lang;
    
    public class DoWhileEg {
        public static void main(String[] args) {
            int k = 0;
    
           
            do {
                
                System.out.println(i);
                k++;
            } while (k <= 6);
        }
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to use the do-while loop statement in java program?

    If you know how to use while statement then it will easy for you you to understand how to use the do-while loop statement in java program. The main difference between do-while statement and while statement is that do-while statement evaluates its expression at the bottom of the loop instead of the top. For this reason the statements within the do block are always executed at least once thorugh out the execution.


    Code:
    class DoWhileDemo {
         public static void main(String[] args){
              int counts = 1;
              do {
                   System.out.println("Count is: " + counts);
                   counts++;
              } while (counts <= 11);
         }
    }
    Output will be as follows :
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How to use the do-while loop statement in java program?

    There are some situation where you have to run some statements and processes once at least. In this case you have to use the do while loop to solve such type of problem. Do - while loop executes all the statements first at once and then check the condition.

    The Syntax for the do-while loop is as follows:

    do
    {
    statements;
    }
    while(condition);
    Code:
    public class DoWhile{
      public static void main(String[] args){
        int j = 12345;
        int k,w = 0;
        System.out.println("The original number : " + j);
        do{
          k = j % 10;
          w = w * 10 +k;
          j = j / 10;
        }while (j > 0);
        System.out.println("The reverse number : " +w);
      }
    }

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to use the do-while loop statement in java program?

    In the do-while syntax code block will always be run at least once. It is very useful when you want to execute at least first statement of particular code.
    Syntax of Do while statement is as follows.

    Code:
    do {
    code_block;
    } while (condition);
    If you don't understand how to use do-while statement then try to understand following example.

    Code:
    // This is the Hello program in Java
     
     class Hello {
     
       public static void main (String args[]) {
     
         int k = -1;
         do {
           if (k == -1) System.out.print("Hello to all "); 
           else {
             System.out.print(args[k]);  
             System.out.print(" ");
           }
           k = k + 1;
         } while (k < args.length);
         System.out.println(); 
      }
     
    }

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to use the do-while loop statement in java program?

    Do while statement is used in java program when you want to run program in loop on pre defined condition. Do while statement helps to run any java statement in block to execute on first block without checking conditions. In do while statement we have to use "do" key word in top and "while" keyword in last to make block. Try to understand following example.

    Code:
    public class JavaDoWhileStatementExample {
    
        public static void main(String[] args) {
    
            int k=0;
            
            do
            {
                System.out.println("do while loop iterator1 :"+k);
                k++;
            }
            while(k<0);
    
            int l=0;
           
            do
            {
                System.out.println("do while loop iterator2 :"+l);
                l++;
            }
            while(l<5);
        }
    }

Similar Threads

  1. How to use switch statement in java
    By Raju Chacha in forum Software Development
    Replies: 2
    Last Post: 19-01-2012, 06:40 PM
  2. Java program to use enum in switch statement.
    By MADGE25 in forum Software Development
    Replies: 5
    Last Post: 01-02-2010, 06:12 PM
  3. How to use the break statement in java program?
    By Madaleno in forum Software Development
    Replies: 4
    Last Post: 23-01-2010, 09:27 PM
  4. How to use the continue statement in java program?
    By Luz in forum Software Development
    Replies: 4
    Last Post: 23-01-2010, 08:40 PM
  5. To use goto statement or not while coding the program?
    By YatinK in forum Software Development
    Replies: 3
    Last Post: 19-02-2009, 07:19 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,975,412.22632 seconds with 17 queries