| Author |
Message |
Singachea fourth grade
Joined: 19 Nov 2005 Posts: 258
|
|
| Back to top |
|
 |
guitarman fourth grade
Joined: 04 Nov 2005 Posts: 724
|
Posted: Thu May 11, 2006 1:09 pm Post subject: |
|
|
an Endless loop???
Wow, Never try the condition of Eqaul sign before ehh..
Innovative 
|
|
| Back to top |
|
 |
Singachea fourth grade
Joined: 19 Nov 2005 Posts: 258
|
Posted: Thu May 11, 2006 11:55 pm Post subject: |
|
|
Can anyone explain why?
|
|
| Back to top |
|
 |
guitarman fourth grade
Joined: 04 Nov 2005 Posts: 724
|
Posted: Fri May 12, 2006 12:22 am Post subject: |
|
|
So... my answer is right??
|
|
| Back to top |
|
 |
Singachea fourth grade
Joined: 19 Nov 2005 Posts: 258
|
Posted: Fri May 12, 2006 12:48 am Post subject: |
|
|
yes, you are right brother, but i just wonder why. Is it the flaw in C or what?
|
|
| Back to top |
|
 |
guitarman fourth grade
Joined: 04 Nov 2005 Posts: 724
|
Posted: Fri May 12, 2006 1:10 am Post subject: |
|
|
for(i = 0; i = 3; i++)
I think first i is initialized to 0, next it checks the condition i = 3 (this is always true, as i was assigned to 3), so the first print statement fires. AFterwhich, i is incremented to 1, then the condition is still true.. next... next... always true.
I think the cue point is the condition>> i = 3. Instead, it should be i==3.
|
|
| Back to top |
|
 |
Singachea fourth grade
Joined: 19 Nov 2005 Posts: 258
|
Posted: Fri May 12, 2006 11:07 am Post subject: |
|
|
I think it's not an assignment.
for(i = 0; i = 3; i++)
i = 3 is the condition
and another: for(i = 0, i = 3; ; i++)
i = 3 is an assignment where the condition is always true.
Actually we can see this:
| Code: |
for(initialization; condition; update)
{
statements;
} |
can be transformed into
| Code: |
initialization
while(condition)
{
statements;
update;
} |
Thus if the statement is written as for(i = 0; i = 3; i++), it should be the same if we write:
| Code: |
i = 0
while(i = 3)
{
statements;
i++;
} |
A mistake should be detected at while(i = 3)
We can't compile this statement for(i = 0; i = 3; i++) in java because it detects an error (incompatible type.)
If the condition is changed to i == 3, then there is nothing printed out.
|
|
| Back to top |
|
 |
guitarman fourth grade
Joined: 04 Nov 2005 Posts: 724
|
Posted: Fri May 12, 2006 2:50 pm Post subject: |
|
|
Nice explanation about transforming into while loop
It happens a lot to me when I code in PHP. I think use .NET is better .
|
|
| Back to top |
|
 |
|