Results 1 to 2 of 2

Thread: do...while loop

  1. #1
    Join Date
    Apr 2006
    Posts
    429
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default do...while loop

    i forgot my Java basics...

    how do i do this again using the do...while loop without labeling? tnx.

    Code:
    public class Factorial {
      public static void main(String[] args) {
        long limit = 20;      // to calculate factorial of integers up to this value
        long factorial = 1;   // factorial will be calculated in this variable
    
        // Loop from 1 to the value of limit
        OuterLoop:
        for(int i = 1; i <= limit; i++) {
          factorial = 1;               // Initialize factorial
          for(int j = 2; j <= i; j++) {
            if(i > 10 && i % 2 == 1)
              continue OuterLoop;      // Transfer to the outer loop
            factorial *= j;
          }
          System.out.println(i + "!" + " is " + factorial);
        }
      }
    }
    Last edited by jr_yeo; 08-13-2008 at 09:06 PM.
    Please don't mind me. I am just posting a lot of nonsense.

  2. #2
    Join Date
    Aug 2009
    Location
    Egypt
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Factorial Is So Simple

    public class Factorial {
    public static void main(String[] args) {
    long limit = 20;
    long factorial = 1;
    long i=1;
    do
    {
    factorial*=i;
    System.out.println(i + "!" + " is " + factorial);
    i++;
    }while(i <= limit)
    System.out.println(limit + "!" + " is " + factorial);

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
  •