Results 1 to 5 of 5
  1. #1
    13hartc is offline Member
    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0

    Default Converting Loops

    How do I convert my for loop below to a while and do while loop?


    for(int a=1;a<=9;a++){
    for(int b=1;b<=a;b++){
    System.out.print(a);
    }
    System.out.println();
    }//end 1-9 loop
    for(int a=9; a>=1; a--) {
    for(int b=1; b<=a; b++) {
    System.out.print(i);
    }
    System.out.println();
    }//ends 9-1 loop

  2. #2
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Re: Converting Loops

    The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Your code in [CODE] tags for clearer visibility.
    Java Code:
    for(int a=1;a<=9;a++)
    {
        for(int b=1;b<=a;b++)
        {
            System.out.print(a);
        }
        System.out.println();
    }//end 1-9 loop
    
    for(int a=9; a>=1; a--)
    {
        for(int b=1; b<=a; b++)
        {
            System.out.print(i);
        }
        System.out.println();
    }//ends 9-1 loop

  3. #3
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Re: Converting Loops

    To solve this problem just take each for loop separately. For example the outer for loop:
    Java Code:
    for (int a = 1; a <= 9; a++)
    A for loop has the syntax for (initialization; condition; setVariableTo)

    the initialization a = 1 sets a to 1
    the condition states loop while a is less than or equal to 9
    the setVariableTo says a++ at the end of the loop where
    a++ means a = a + 1

    from what i've stated you can see that:
    a counts 1, 2, 3, 4, 5, 6, 7, 8, 9. At 10 a is no longer equal to OR less than 9.

    For a while loop you have the syntax:
    Java Code:
    while (conditionTrue)
    {
        // execute what is here
    }
    Therefore for converting a for statement to a while just take the condition from the for loop and insert it in the while statement.
    Java Code:
    while (a <= 9)
    {
        // execute
    }
    But before this we need to declare a:
    Java Code:
    int a = 1
    while (a <= 9)
    {
        // execute
    }
    From here you can see though that a in this case goes nowhere since we still need the increment statement.
    Java Code:
    int a;
    while (a <= 9)
    {
        a++;
    }
    Your not done though you still need to do the inner loop. So do that part by inserting code inbetween a++ and the while statements first bracket.
    Java Code:
    int a;
    while (a <= 9)
    {
        // don't forget the initialization before the while
        // other while statement here
        a++;
    }

  4. #4
    AlexGraal is online now Señor Member
    Join Date
    Jan 2014
    Posts
    167
    Rep Power
    0

    Default Re: Converting Loops

    It's a great answer - though it looks like he needs do while loops, not a simple while loop.

    So you'd want to show him these:

    Java Code:
    int a = 0;
    do {
       //this this and this
       a++
    } while(a <= 9);

  5. #5
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Re: Converting Loops

    I was also going to do those also since he needed both but I thought he might get the gist from the first and the tutorial link. Since he didn't respond I didn't think he needed a follow up and I also thought that someone else might also put in there 2 cents.

Similar Threads

  1. Help with for loops :)
    By Oldemor in forum Eclipse
    Replies: 8
    Last Post: 09-18-2013, 09:20 PM
  2. Replies: 2
    Last Post: 12-08-2012, 04:32 AM
  3. Replies: 20
    Last Post: 03-22-2011, 04:02 AM
  4. Help with Loops
    By Spyderpig in forum New To Java
    Replies: 10
    Last Post: 02-17-2011, 08:10 AM
  5. Help with loops
    By pg5678pg in forum New To Java
    Replies: 8
    Last Post: 10-17-2010, 06:51 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •