11 Replies - 11023 Views - Last Post: 17 January 2010 - 06:52 PM Rate Topic: -----

#1 tejesh12341234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 06-June 09

Differences between while and for loop?

Posted 16 January 2010 - 09:53 PM

Differences between while and for loop?
Is This A Good Question/Topic? 0
  • +

Replies To: Differences between while and for loop?

#2 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5226
  • View blog
  • Posts: 27,000
  • Joined: 10-May 07

Re: Differences between while and for loop?

Posted 16 January 2010 - 09:56 PM

This is a common homework question.

The difference is simple. It's a matter of syntax & execution position.
Was This Post Helpful? 0
  • +
  • -

#3 zombie_chan51  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 327
  • Joined: 16-March 08

Re: Differences between while and for loop?

Posted 16 January 2010 - 09:56 PM

a while loop keeps going into the boolean value is set false

bool x=true
while(x)
{
   x=false;
}



while the for loop is you use a value to set how many loops you want

for(int x=0;x>5;x++)
{
  System.out.println(x);
}


Was This Post Helpful? 0
  • +
  • -

#4 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,103
  • Joined: 26-September 08

Re: Differences between while and for loop?

Posted 16 January 2010 - 10:06 PM

The differences are all syntax. As they both LOOP and both require BOOLEAN comparisons, they can be used interchangeably. They both lend themselves to certain situations, but it also depends on what you prefer as well.
Was This Post Helpful? 0
  • +
  • -

#5 Simple_Condolences  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 138
  • Joined: 10-January 10

Re: Differences between while and for loop?

Posted 16 January 2010 - 10:06 PM

A while loop continues while the condition is true. So, for example, while(1==1) will result in an infinite loop because 1 will always be equal to 1.

A for loop continues until a certain parameter is met, usually something is < something or something is greater than something. So, for(int i = 0;i<5;i++) will run five times. It starts i at 0, checks to see if it is less than 5. If so, the loop updates i and then executes the code within the loop.

Hope this helped.

- Zach
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Differences between while and for loop?

Posted 17 January 2010 - 12:54 PM

for are usually use for iterations
while are usually used when there is a condition to check
Both of them can be replaced by the other one any time
Was This Post Helpful? 0
  • +
  • -

#7 Qball522  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 86
  • Joined: 06-October 08

Re: Differences between while and for loop?

Posted 17 January 2010 - 04:17 PM

I was always to the understanding that you could write any for loop as a while loop but not always the other way around.

Suppose you have a while loop testing for a user input. How would you re-write that as a for loop?
.......
ummm....

I mean i guess you could do like
for(int i =0; i<2;i++)
{
	 input = scanner.next();
	 if(input == "yes")
	 {
		  i--;
	 }
	 else
	 {
		  i = 100;
	 }
}


But that would be just ugly if ya ask me.
Was This Post Helpful? 0
  • +
  • -

#8 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Differences between while and for loop?

Posted 17 January 2010 - 04:31 PM

for(;input.equals("yes");)
{
  input = scanner.next();
}

edit: removed ! I thought you meant to continue looping while the input was not "yes" rather than the other way around

This post has been edited by erik.price: 17 January 2010 - 04:37 PM

Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Differences between while and for loop?

Posted 17 January 2010 - 04:41 PM

View Posterik.price, on 17 Jan, 2010 - 03:31 PM, said:

for(;input.equals("yes");)
{
  input = scanner.next();
}

edit: removed ! I thought you meant to continue looping while the input was not "yes" rather than the other way around

:^: :^: :^:
Qbal522 do you have another one ? :)

Worst case
for(;;) {
  if(!condition)
	break;
}


Was This Post Helpful? 0
  • +
  • -

#10 Qball522  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 86
  • Joined: 06-October 08

Re: Differences between while and for loop?

Posted 17 January 2010 - 04:59 PM

View Postpbl, on 17 Jan, 2010 - 03:41 PM, said:

View Posterik.price, on 17 Jan, 2010 - 03:31 PM, said:

for(;input.equals("yes");)
{
  input = scanner.next();
}

edit: removed ! I thought you meant to continue looping while the input was not "yes" rather than the other way around

:^: :^: :^:
Qbal522 do you have another one ? :)

Worst case
for(;;) {
  if(!condition)
	break;
}


Would there be any advantage to doing a for loop in that way, over a while loop?
Was This Post Helpful? 0
  • +
  • -

#11 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Differences between while and for loop?

Posted 17 January 2010 - 05:02 PM

No, not really. The different loops are there as a convenience. They are all interchangeable, but allow for you to write more readable code. In this case for instance, a while loop would be better.
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Differences between while and for loop?

Posted 17 January 2010 - 06:52 PM

View PostQball522, on 17 Jan, 2010 - 03:59 PM, said:

Would there be any advantage to doing a for loop in that way, over a while loop?

They are the same
Just easier for the other human reading your code to understand
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1