# Differences between while and for loop?

Page 1 of 1

## 11 Replies - 46627 Views - Last Post: 17 January 2010 - 06:52 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=150349&amp;s=a07ce737859b0261f8f97dd530c9a720&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 tejesh12341234

Reputation: 0
• 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

• Professor Snuggly Pants

Reputation: 6611
• Posts: 30,823
• 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.

### #3 zombie_chan51

• D.I.C Regular

Reputation: 7
• 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);
}

```

### #4 TriggaMike

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

Reputation: 85
• 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.

### #5 Simple_Condolences

Reputation: 11
• Posts: 180
• 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

### #6 pbl

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

Reputation: 8378
• Posts: 31,956
• 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

### #7 Qball522

Reputation: 6
• 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.

### #8 erik.price

• D.I.C Lover

Reputation: 486
• 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

### #9 pbl

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

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Differences between while and for loop?

Posted 17 January 2010 - 04:41 PM

erik.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;
}

```

### #10 Qball522

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

## Re: Differences between while and for loop?

Posted 17 January 2010 - 04:59 PM

pbl, on 17 Jan, 2010 - 03:41 PM, said:

erik.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?

### #11 erik.price

• D.I.C Lover

Reputation: 486
• 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.

### #12 pbl

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

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Differences between while and for loop?

Posted 17 January 2010 - 06:52 PM

Qball522, 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