Results 1 to 4 of 4
  1. #1
    steveday77 is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default While Loop inside a While Loop

    OK so i cant quite wrap my head around this not sure if i have the correct layout for this to work, does the inside while loop finish what its doing before the g++ happens ? What i want to happen is for the inside while loop to go around 40 times so x goes from 0-39 and compares it against g[0]. Then i want g to increment to 1 and for the inside while loop to go around again 40 times so x goes from 0-39 and compares it against g[1]

    Hope this makes sense any help would be appreciated.
    Thanks code below


    moduleCode.length = 20
    choice2.length = 40


    Java Code:
    int z = 0;
    int g = 0;
    
    	while(g < moduleCode.length){
    		z = 0;
    		while(z < choice2.length) {
    			
    		if(choice2[z] == moduleCode[g]){
    			moduleNumbers[z] = moduleNumbers[z]+ 1;
    			
    		} 
    		z++;
    		} g++;
    	}

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: While Loop inside a While Loop

    To put it bluntly yes.

    what ever is inside this: while(g < moduleCode.length) As long as this evaluates to true will get executed and finish.

    If i am reading this right (im tired) it makes full sense to me, the inner loop will execute until z is greater that choice2.length then g will increment and it will happen again :)

    The big test is, have you ran it? Does it do what you want it to?

  3. #3
    steveday77 is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: While Loop inside a While Loop

    Hi thanks for your reply after looking at the code in here im after spotting "if(choice2[z] == moduleCode[g])" this was a problem i was stuck on before and someone here helped me , their strings so i needed .equals(). Woops

    Its working now thanks.

  4. #4
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    256
    Rep Power
    3

    Default Re: While Loop inside a While Loop

    no worries, i didn't check the details i only looked at the while loop situation :)

Similar Threads

  1. post increment inside a loop?
    By brendan89 in forum New To Java
    Replies: 2
    Last Post: 02-24-2012, 07:40 PM
  2. OnClick inside a loop
    By sehudson in forum New To Java
    Replies: 9
    Last Post: 05-29-2011, 07:14 AM
  3. Help with while loop inside a switch case
    By Shesaid in forum New To Java
    Replies: 2
    Last Post: 04-01-2011, 04:36 AM
  4. if else inside while loop??
    By jonytek in forum New To Java
    Replies: 3
    Last Post: 02-17-2011, 10:28 AM
  5. Loop inside a switch
    By mustachMan in forum New To Java
    Replies: 3
    Last Post: 02-26-2010, 04:25 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
  •