for loop does not seem to work

  • Thread starter Thread starter Delphi Sleuth
  • Start date Start date
D

Delphi Sleuth

what the heck am I not getting. This little snipet doesn't do what I
thought it was going to do. Can anyone tell me why? I expected
2
Hello: 102

All I get is the 2. I had been trying to iterate though a list that
was not working... please help!

int cnt = 0;
cnt = cnt + 1;
cnt = cnt + 1;
Response.Write(cnt.ToString());
for (int counter = 0; counter == 100; counter++)
{
cnt++;
}
Response.Write("<BR>Hello: " + cnt.ToString());
 
That's some weird code...

cnt = 0 (cnt equals zero)
cnt = cnt + 1 (so now cnt equals 1)
cnt = cnt + 1 (so now cnt equals 2)
starting variable "counter" at 0,
and incrementing "counter" by 1,
while the variable 'counter' equals 100
{
increment the variable "cnt" by 1 (and since cnt is already
2...)
}
write the value of the variable "cnt, which will always be 102!
 
Delphi Sleuth said:
what the heck am I not getting. This little snipet doesn't do what I
thought it was going to do. Can anyone tell me why? I expected
2
Hello: 102

All I get is the 2. I had been trying to iterate though a list that
was not working... please help!

int cnt = 0;
cnt = cnt + 1;
cnt = cnt + 1;
Response.Write(cnt.ToString());
for (int counter = 0; counter == 100; counter++)
{
cnt++;
}
Response.Write("<BR>Hello: " + cnt.ToString());

The for loop says "initialize counter to 0, then run the loop as long as
counter equals 100".

But counter does not equal 100, so the loop doesn't execute.

Try counter < 100

or something like that.
 
aaaaa!!! Sorry guys. I had a bad moment. My other language is Delphi
and I confused the two loops. In Delphi the for-loop is more like a
counter. C# as you guys know its more like a while-loop. Excuse my
brain.

- Hal
 
Back
Top