Results 1 to 8 of 8
  1. #1
    danboy4 is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default Converting a while loop to a for loop

    Hey guys, super new to these forums but I was having some issues converting a while loop/if statem to a for loop so it has the same functionality. I'm not even sure it's possible My attempt is at the bottom. (I'm sure i'm doing it completely wrong but i'm a noob so please help me out)

    Java Code:
        int k = 0; 			
        int sum = 0; 		
        
        
        while (sum < n){ 	
        	k = k + 1;		
        	sum = sum + k;	
        }					
        	
        	    	
      					
        if (sum > n){		
        	k = 0;			
        }
    This is my attempt:
    Java Code:
     for (int sum=0; sum<n; sum = sum+k)
        {
        	k = k + 1;		
    	
    
    if (sum > n){		
        	k = 0;			
        }	
    
        }

    Thanks for your time!

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default Re: Converting a while loop to a for loop

    You need to get your indenting sorted out first off.

    What are you seeing with the bottom code that you aren't seeing in the original while loop code?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Zyril is offline Senior Member
    Join Date
    Oct 2011
    Location
    Sweden
    Posts
    124
    Rep Power
    0

    Default Re: Converting a while loop to a for loop

    There is code missing, I'm not sure whether you've just snipped it out or you've forgotten it.

    The integers k & sum can be declared and defined once in the top of your code, and after that your for-loop only has to be about one line of code. Study how you build a for-loop, and as Tolls said, sort out the indenting so the code is manageable and easy to read.

  4. #4
    danboy4 is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default Re: Converting a while loop to a for loop

    Thanks for the advice so far. As for the indenting, that was just a copying mistake from my code to the forum, I think all my code is correctly indented. I've now managed to get to this point now and it seems to be working. Am i able to include the if statement into the for loop to simplify this code any further?

    Java Code:
    
    
    int k = 0; 			
    int sum = 0; 		
        
        for (sum=0; sum<n; sum=sum+k)
        {
        	k = k + 1;	
        }
        
      					
        if (sum > n)
        {
        	k = 0;		
        }

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default Re: Converting a while loop to a for loop

    Why would that simplify it?
    It would be the same amount of code.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    danboy4 is offline Member
    Join Date
    Aug 2012
    Posts
    7
    Rep Power
    0

    Default Re: Converting a while loop to a for loop

    I'm wondering if there is a better way to write that segment of code that would be simplified. Sorry if i'm not coming across clear like I said, first post and very new.

    I appreciate your help but your answers are only really questions back at me and they make me feel dumb. I'm asking these questions because I don't know myself and needed a little assistance.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default Re: Converting a while loop to a for loop

    I'm only asking because I don't really understand what you are getting at.

    You've achieved what you set out to do, which is turn the while loop into a for loop.
    If you put the 'if' statement into the loop it will do the check everytime it goes round the loop and, since the loop exits when sum >= n, the 'if' statement will never be true.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default Re: Converting a while loop to a for loop

    Converting a while loop to a for loop is easy (always):

    Java Code:
    while: while (<boolean_expression>) <statement>;
    for: for (; <boolean_expression>; ) <statement>;
    The other way around is a bit trickier if you want to pay notice to the scope of variables and abnormal termination/continuation of the loops.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Converting a for loop to a do-while loop
    By awesom in forum New To Java
    Replies: 1
    Last Post: 11-23-2011, 03:02 PM
  2. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  3. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  4. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  5. Convert do while loop to for loop
    By sandeeptheviper in forum New To Java
    Replies: 3
    Last Post: 01-03-2011, 12:37 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
  •