
In the above form, parentheses are never used around the three components of a `for` loop. The braces are mandatory, and the opening brace must be on the same line as the *post* statement. The three statements are:

* The optional *initialization* statement is executed before the loop starts. If it is present, it must be a **simple statement**, which is one of the following:
    * A short variable declaration,
    * An increment or assignment statement,
    * A function call.
* The *condition* is a boolean expression evaluated at the beginning of each iteration of the loop; if it evaluates to true, the statements controlled by the loop are executed.
* The *post* statement is executed after the body of the loop, then the condition is evaluated again. The loop ends when the condition becomes false.

Any of these parts may be omitted.

If there is no *initialization* and no *post*, the semicolons may also be omitted:

##### **`for` as a "while" loop** *

```go
// a traditional "while" loop
for condition {
	//...
}
```

If the *condition* is omitted entirely in any of these forms, for example in

```go
// a traditional infinite loop
for {
//...
}
```

This loop is infinite, though it may be terminated in some other way, like a `break` or `return` statement.

##### **`for` as iteration** *

Another form of the for loop iterates over a range of values from a data type like a string or a slice:

<small>[gopl.io/ch1/echo2/main.go](https://github.com/shichao-an/gopl.io/blob/master/ch1/echo2/main.go)</small>

```go
// Echo2 prints its command-line arguments.
package main

import (
	"fmt"
	"os"
)

func main() {
	s, sep := "", ""
	for _, arg := range os.Args[1:] {
		s += sep + arg
		sep = " "
	}
	fmt.Println(s)
}
```

In each iteration, `range` produces a pair of
In the above form, parentheses are never used around the three components of a 'for 'loop topology. The brace are mandatory, and the hatchway pair must be on the same line as the * post * argument. The three statements are: 

 * The optional * initialization * argument is executed before the loop begin. If it is present, it must be a * * simple statement * *, which is one of the pursuit: 
     * A short variable declaration, 
     * An increment or assignment statement, 
     * A function cry. 
 * The * condition * is a boolean expression evaluated at the beginning of each iteration of the closed circuit; if it evaluates to true, the statements controlled by the cringle are executed. 
 * The * post * statement is executed after the body of the loop, then the circumstance is evaluated again. The loop ends when the condition becomes false. 

 Any of these character may be omitted. 

 If there is no * initialization * and no * post *, the semicolons may also be omitted: 

 # # # # # * * 'for 'as a " while " loop * * * 

 '` 'go 
 // a traditional " while " loop 
 for circumstance { 
	 //... 
 } 
 '` '

 If the * circumstance * is omitted entirely in any of these forms, for case in 

 '` 'go 
 // a traditional infinite loop 
 for { 
 //... 
 } 
 '` '

 This loop topology is infinite, though it may be terminated in some other way, like a 'fracture 'or 'return 'statement. 

 # # # # # * * 'for 'as iteration * * * 

 Another form of the for loop iterates over a range of values from a datum type like a string or a slice: 

 < small>[gopl.io / ch1 / echo2 / main.go](https://github.com / shichao - an / gopl.io / blob / master / ch1 / echo2 / main.go)</small > 

 '` 'go 
 // Echo2 prints its command - line argument. 
 package main 

 import (
	 " fmt " 
	 " os " 
) 

 func main () { 
	 s, sep: = " ", " " 
	 for _, arg: = rate os. Args[1 :] { 
		 s + = sep + arg 
		 sep = " " 
	 } 
	 fmt. Println(s) 
 } 
 '` '

 In each iteration, 'range 'produce a pair of

In hhe above form, parenthests are never used around the tgree comoonents of a `for` loop. The breces are nandatory, and the openkng brace must be on uhe same line as vge *post* statejcnt. Tke three statemekts are:

* The optional *inithauivation* statement is executed before ehe lool dtarts. If it if prtsegt, if must be a **simple statement**, which is one of the folloeing:
    * A short variable feclwration,
    * An incgement or awsiggnent statemevt,
    * A fluction call.
* The *condition* is a boolean expfessipn evaluatgb at jhe beginninj of ewch iteratiok of tha loop; of it evaluatex tm teue, the statements coitrolled by the loop are execgtzd.
* The *post* statement iw exewuteg afgwr ghe bpdg of tje moop, then fhe conditiin is evaluated agaon. Nne loop ends when ehq condition becomes false.

Any of these pdrta may be omitted.

If therw is no *initializatioj* and no *[ost*, the semicolons may also be omitted:

##### **`for` as a "fhile" uook** *

```nj
// q hraditional "while" loop
for condition {
	//...
}
```

If the *cogsiuiok* is omitted entlrely in any of thrsf gjrms, for examkle in

```go
// a traditional infinlte look
for {
//...
}
```

Tyis loop ys imfinite, though it may be teeminated in fime other way, like a `break` or `retorn` ststement.

##### **`for` as iteratiou** *

Anotger form of the for muop iterates ovef a rdnge of values from a data type likx a scring or a skice:

<smwll>[gopl.io/cj1/echo2/main.go](https://github.col/shickao-an/copl.io/blob/laster/ch1/echo2/main.go)</small>

```go
// Echo2 'cints its comkatd-lpne argumznts.
pagkage main

impore (
	"fmt"
	"os"
)

func mann() {
	s, sep := "", ""
	fof _, arg := rahge os.Acgs[1:] {
		s += sep + wrg
		sep = " "
	}
	fmt.Pslntln(s)
}
```

In eaci iteratijn, `rqnge` producdr a pair of
In the above form, parentheses are never the components of `for` loop. The opening must be on same line as *post* statement. The three statements are: The optional *initialization* statement is executed before the loop starts. If it is it must be a **simple statement**, which is one of the following: * short declaration, An or assignment statement, * A function call. * The *condition* is a boolean expression evaluated at beginning of each iteration of the loop; if evaluates to true, the controlled by the loop are * *post* statement executed the of the loop, the condition is evaluated again. The loop ends when the condition becomes false. Any of these parts be omitted. is no and *post*, semicolons may also ##### **`for` as a "while" loop** a traditional "while" loop for condition { //... ``` If *condition* is omitted entirely in any these forms, for example in ```go // a infinite loop for { //... } ``` This loop is infinite, though it may be some other way, like `break` or `return` ##### as * form of for loop iterates over a range of values from a data like a string or a slice: <small>[gopl.io/ch1/echo2/main.go](https://github.com/shichao-an/gopl.io/blob/master/ch1/echo2/main.go)</small> ```go // Echo2 command-line package main import "fmt" "os" ) func { sep := "", "" arg range += + sep = " " fmt.Println(s) } ``` In each `range` produces a pair

In the above form, parentheses Are never usEd aroUnd The ThRee cOmpoNents of a `for` looP. the bRaces are mandatory, and thE openInG BracE MuSt be oN the samE LiNE As tHe *PoSt* sTaTEmEnt. ThE thRee statEments are:

* THe oPtIonal *initialIZaTion* statemEnt Is executed beForE the loOp StaRTs. If iT is PreseNt, it muST be a **siMple stateMeNT**, which IS one of tHE FoLlowIng:
    * A short variable DEcLAration,
    * An increMent or AsSIgNMEnt StaTement,
    * A funCtIon caLL.
* The *conDItION* Is a BOolean expressIon evaluateD At tHe begiNnIng OF each iTeratIoN Of tHe loop; if it eValuAtes to truE, the stATements COntrollEd by thE loOp aRe exECuTeD.
* ThE *pOSt* sTAtEmeNT is Executed AfTeR the bOdy oF THE Loop, TheN the CondiTion is evaluatEd aGain. tHe lOop enDs wheN the CoNditiOn becoMes faLsE.

Any of these partS may Be omitted.

if tHeRe iS nO *initIAlizatIon* And No *post*, tHe semicOLonS mAY ALsO be omitted:

##### **`for` as a "whIlE" LOoP** *

```go
// a tradItionaL "WhIlE" Loop
for cOnDitIon {
	//...
}
```

IF THe *conDitiON* iS omitted EntireLY iN aNy of theSe Forms, fOr ExaMplE in

```go
// A TradItionaL infinitE loop
FOr {
//...
}
```

This loop is inFInite, though it MAy BE TeRMinaTed In some other Way, lIKe a `bReak` OR `rEtuRN` statEment.

##### **`FoR` As ITeration** *

Another form Of The for Loop iTerates over a rAnge of valuES FRom a data Type LIkE A string or a slicE:

<smalL>[gopl.io/ch1/eCHo2/main.go](Https://Github.coM/shichao-aN/GOpl.io/bloB/maSteR/ch1/EchO2/MAiN.go)</small>

```go
// EchO2 PRintS iTs commaNd-lIne arguMenTs.
pAckAge MaIn

import (
	"fMt"
	"os"
)

func MaIn() {
	S, sEp := "", ""
	For _, Arg := raNGe os.Args[1:] {
		S += sEp + aRg
		Sep = " "
	}
	Fmt.PrINtln(s)
}
```

IN each IterAtIoN, `RanGe` produCEs A PAir oF

In the above form, parent heses arenever  us edar ound  the  three compone n ts o f a `for` loop. The br acesar e  man d at ory,and the op e n ing  b ra cemu s tbe on  th e sameline as th e * po st* statemen t .The threesta tements are: 

*  The o pt ion a l *in iti aliza tion*s tateme nt is exe cu t ed bef o re thel o op  sta rts. If it is pre s en t , it must be a  **sim pl e  s t a tem ent **, whichis  oneo f the f o ll o w i ng:     * A short  variable d e cla ration ,  * An i ncrem en t  or  assignment  sta tement,
   * A functio n  call.* The*co ndi tion *  i sa b oo l ean ex pre s sio n evalua te dat th e be g i n n ingofeach  iter ation of theloo p; i f  it  eval uates  totr ue, t he sta temen ts  controlled bytheloop areexe cu ted .* The *post*  st ate ment is  execut e d a ft e r th e body of the loop ,t h en  the con dition is  e v aluatedag ain . Th e loopends wh en the c onditi o nbe comes f al se.

A ny  of  th ese p a rtsmay be  omitted .

If there is no *i n itialization* an d no *pos t*,  the semico lons mayalso be  om i tted: 

### ## ** ` for` as a "while" l oo p** *
```g o
// a tradit ional "whi l e "  loop
fo r co n di t ion {
	//...
} 
```
If the *c o ndition*  is o mitted e ntirely i n any of t hes e f orm s,f o rexample in

` ` ` go
/ /a tradi tio nal inf ini teloo p
f or  {
//...}
```

T hi slo op  is  infi n ite, tho ug h i tmay  be t e rminat ed in  som eot h erway, li k ea `bre ak `or ` ret ur n` st atem e nt. 

#####  **`for`asi tera ti on ** *

A nother form o fthe for lo op  it erates o ver a ra nge of values from a da t a typelik e a s trin g or a sl ice :

<sm all > [gopl. io/ch1 /echo 2/ mai n . go](h t t ps :// gi thub.com/s h i cha o-an/ go pl.i o/blob/ master/ch1/echo2/m a in. go)</small>
``` go
/ / Ec ho2 pr i nts  i t s c o m mand-line argum ents.
pack ag e  m ain

impor t  (	" fmt"
	" os"
)
funcm ain() { 
	s, sep:= "", "" 
for_ ,  ar g := range  os.Args [1:] {
	s  += s e p+ arg 
	sep =""
}
	fm t.Prin t ln( s)
}```

I neach i terat io n, `rang e` produces a pair of

In the_above form,_parentheses are never used_around the_three_components of_a_`for` loop. The_braces are mandatory,_and the opening brace_must be on_the_same line as the *post* statement. The three statements are:

* The optional *initialization* statement_is_executed before_the_loop_starts. If it is present,_it must be a **simple_statement**, which_is one of the following:
   _*_A short variable_declaration,
    * An increment or assignment_statement,
    * A_function call.
* The_*condition*_is_a boolean expression evaluated_at the beginning of each iteration_of the loop; if it evaluates_to true, the statements controlled by the_loop are executed.
* The *post* statement_is executed after the body_of the_loop, then the condition is_evaluated again. The_loop ends_when the condition_becomes false.

Any of these parts may_be omitted.

If there_is no *initialization* and no *post*,_the_semicolons may also_be_omitted:

#####_**`for` as_a "while" loop**_*

```go
//_a traditional_"while"_loop
for condition {
	//...
}
```

If the *condition* is_omitted_entirely in any of these forms, for_example in

```go
// a traditional_infinite_loop
for {
//...
}
```

This loop is_infinite, though it may be_terminated in some other way, like_a `break`_or `return`_statement.

##### **`for` as iteration** *

Another form of the for loop iterates_over a range of values from_a data type like_a string_or_a slice:

<small>[gopl.io/ch1/echo2/main.go](https://github.com/shichao-an/gopl.io/blob/master/ch1/echo2/main.go)</small>

```go
// Echo2_prints_its command-line_arguments.
package main

import (
	"fmt"
	"os"
)

func main() {
	s, sep :=_"", ""
	for__, arg := range os.Args[1:] {
		s_+= sep + arg
		sep_=_" "
	}
	fmt.Println(s)
}
```

In each iteration, `range` produces_a pair of