Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Converting a while loop to a for loop and a for loop to a while loop.

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Converting a while loop to a for loop and a for loop to a while loop.

    Hi guys,

    I want to convert the while loop of this code to a for loop, and the for loop to a while loop but i tried and couldnt figure out, what i need to do?
    The results have to be the same.
    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++;
            }
        }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,298
    My Mood
    Hungover
    Thanks
    142
    Thanked 620 Times in 531 Posts

    Default Re: Converting a while loop to a for loop and a for loop to a while loop.

    Why do you want to do this? What did you try?

    Recommended reading: Control Flow Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Feb 2012
    Posts
    173
    Thanks
    6
    Thanked 10 Times in 10 Posts

    Default Re: Converting a while loop to a for loop and a for loop to a while loop.

    How does a for loop work? You basically are running your while loop as a for loop so why does it matter?

  4. #4
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: Converting a while loop to a for loop and a for loop to a while loop.

    basically both loops function the same. If it is code you want to change for personal reasons, or an assignment, I will give you standard loop protocol.

    for loops are used when you can more or less predict the number of iterations. traveling through an array, counting to 10, and so forth. Typically the incrementation is predictible and linear +1 every time being the most common, though it can be

    words to explain:
    do something 10 times, starting at 1 and going to 10 "for(int idx = 1; idx <=10; idx++) { //stuff }"


    a while loop is best for situations you are asking for something to happen first. while the user has not exited, while the pie is uneaten, while any Boolean expression you can think of. it can still be mathematical.

    do something until i say you are done. never ever stop until then. "while(!done) { //stuff(better change done Boolean somewhere!)"

    you could use a math expression while(numberOfCars != 7) { //stuff} this loop will run until cars = 7. hopefully its possible!

    Hope this helps,
    Jonathan

Similar Threads

  1. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 11:05 AM
  2. [SOLVED] Need help with while loop
    By anthony23415 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 6th, 2011, 02:23 PM
  3. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  4. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 04:27 PM
  5. Do While Loop
    By connex in forum Loops & Control Statements
    Replies: 6
    Last Post: December 7th, 2009, 02:54 PM