d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > For Loop In Go > How To Understand This:
12Next
Add Reply New Topic New Poll
Member
Posts: 5,491
Joined: Sep 22 2008
Gold: 0.00
Mar 18 2013 01:19pm
The Go for loop is similar to—but not the same as—C's. It unifies for and while and there is no do-while. There are three forms, only one of which has semicolons.

// Like a C for
for init; condition; post { }

// Like a C while
for condition { }

// Like a C for(;;)
for { }




1 is easy:

for i := 0; i < 10; i++ {
dosomething
}


2 is easy too:

for value = thisvalue {
dosomething
}



but how do i got to understand nr 3? o.O

Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Mar 18 2013 02:48pm
If it's like for(;;) that just means it's an infinite loop i believe. no different then a while(true) loop

for
{

}

This post was edited by SelfTaught on Mar 18 2013 02:48pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 18 2013 02:54pm
Quote (SelfTaught @ Mar 18 2013 04:48pm)
If it's like for(;;) that just means it's an infinite loop i believe. no different then a while(true) loop

for
{

}


this. leaving out the condition will cause the loop to go into a infinite loops. and since some people want to do this on purpose (for gui's, listener sockets on servers, parsing) they can just short hand it for for(;;) even though if you accedently do for(int i = 0;;i++) it will cause an infinite loop as well.
Member
Posts: 5,491
Joined: Sep 22 2008
Gold: 0.00
Mar 18 2013 04:37pm
Quote (SelfTaught @ 18 Mar 2013 21:48)
If it's like for(;;) that just means it's an infinite loop i believe. no different then a while(true) loop

for
{

}


*smashingheadontable*

Damn :D true story bro :D thanks

how could i forget that?
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Mar 18 2013 06:04pm
Quote (AbDuCt @ Mar 18 2013 12:54pm)
this. leaving out the condition will cause the loop to go into a infinite loops. and since some people want to do this on purpose (for gui's, listener sockets on servers, parsing) they can just short hand it for for(;;) even though if you accedently do for(int i = 0;;i++) it will cause an infinite loop as well.


if you declared an int and incremented it in an "infinite" for loop, would it eventually crash once it exceeded int's range being 2,147,483,647? Would take a while but still..

This post was edited by SelfTaught on Mar 18 2013 06:05pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 18 2013 10:31pm
Quote (SelfTaught @ Mar 18 2013 08:04pm)
if you declared an int and incremented it in an "infinite" for loop, would it eventually crash once it exceeded int's range being 2,147,483,647? Would take a while but still..


not sure. should try and find out lol. also it wouldnt take that long un limited loops are pretty fast.
Member
Posts: 1,358
Joined: Dec 30 2012
Gold: 0.10
Mar 19 2013 09:06am
Quote (AbDuCt @ Mar 18 2013 08:31pm)
not sure. should try and find out lol. also it wouldnt take that long un limited loops are pretty fast.


I'm trying it atm out of curiosity lol. It's gonna take at least a few hours o_o..

This post was edited by SelfTaught on Mar 19 2013 09:07am
Member
Posts: 5,491
Joined: Sep 22 2008
Gold: 0.00
Mar 19 2013 11:10am
Quote (SelfTaught @ 19 Mar 2013 16:06)
I'm trying it atm out of curiosity lol. It's gonna take at least a few hours o_o..



did you put sth in the Task-Part of the loop?

system out's and anything else slow down as hell

This post was edited by ThecoreDancer on Mar 19 2013 11:12am
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 19 2013 02:06pm
Quote (ThecoreDancer @ Mar 19 2013 01:10pm)
did you put sth in the Task-Part of the loop?

system out's and anything else slow down as hell


im pretty sure he is just doing what i said and incrementing an integer inside a for loop without a condition.

Code
int main()
{
  for(int i = 0;;i++){}
return 0;
}
Member
Posts: 5,491
Joined: Sep 22 2008
Gold: 0.00
Mar 19 2013 04:43pm
Quote (AbDuCt @ 19 Mar 2013 21:06)
im pretty sure he is just doing what i said and incrementing an integer inside a for loop without a condition.

Code
int main()
{
  for(int i = 0;;i++){}
return 0;
}



better than

Code
int main()
{
  for(int i = 0;;i++)
       {
          printf("%d",i)
        }
return 0;
}


because that'd be slow as hell

This post was edited by ThecoreDancer on Mar 19 2013 04:44pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll