Results 1 to 5 of 5
  1. #1
    supercowy is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Replacing for with while loop.

    Hey there,

    I am currently attending my first year of computer programming languages and I have my first tests in a few weeks. My teacher gave me an example of exercises we can expect on our test.
    One of the test exercises is this:


    Java Code:
       public void testWhileLoop(int limit)
        {
            int i = 1;
            while ( i <= limit ) {
                // Print a number of '*' characters
                for (int j=0; j<i; j++) {
                    System.out.print("*");
                }
                // Print a 'newline' character
                System.out.print("\n"); 
                i++;
            }
        }
    I got this piece of code. And now I have to replace the while loop with the for loop, and the for loop with the while loop. So basically turn those 2 arround.
    Basically what this code does is: It asks the user for an input int. And say for example the user inputs 4. This code outputs:
    *
    **
    ***
    ****

    Can you guys help me?

    Thanks in advance!

  2. #2
    ballantony is offline Member
    Join Date
    Oct 2011
    Posts
    5
    Rep Power
    0

    Default Re: Replacing for with while loop.

    Here's a suggestion to get you started

    Why not replace the while loop with a for loop first, and see if you can get the code to run properly. Think about what the i++ does.

    After that try and change the while loop

  3. #3
    supercowy is offline Member
    Join Date
    Oct 2011
    Posts
    2
    Rep Power
    0

    Default Re: Replacing for with while loop.

    I tried to replace the loop but I just couldn't get a working solution. I kept getting endless loops or unwanted results. Could anyone help me a bit more on this one? Shouldn't be too much of a problem for
    experienced java programmers I guess.

  4. #4
    acmohan is offline Member
    Join Date
    Jul 2011
    Posts
    26
    Rep Power
    0

    Default Re: Replacing for with while loop.

    Its simple u can try.jst check the limit condition in for and no of time you need "*" in inside while.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,051
    Blog Entries
    7
    Rep Power
    23

    Default Re: Replacing for with while loop.

    If you look at your code from a great distance, you see something like this:

    Java Code:
    int i= 1;
    while (i <= limit) {
       // here is code that uses, but doesn't change, variable i
       i++;
    }
    I doesn't take rocket science to change the code to:

    Java Code:
    for (int i= 1; i <= limit; i++) {
       // here is code that uses, but doesn't change, variable i
    }
    You can apply the same logic (but all reversed) to change your inner loop.

    kind regards,

    Jos
    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Replacing(?) applet with another
    By DarthCoffee in forum Java Applets
    Replies: 2
    Last Post: 03-10-2011, 11:31 PM
  2. Replacing a char with the next
    By hiei_yasha in forum New To Java
    Replies: 14
    Last Post: 01-15-2011, 03:12 AM
  3. Replacing Else If
    By jingly99 in forum Advanced Java
    Replies: 5
    Last Post: 12-29-2009, 12:06 AM
  4. Help Replacing String
    By 7oclock in forum New To Java
    Replies: 5
    Last Post: 02-14-2009, 08:31 AM
  5. Replacing at an index
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-29-2008, 07:33 AM

Posting Permissions

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