the phor staatnnent


the phor staatnnent has the pholouuing phornn:

phor (inishaliisaashon; condishon; iteraashon)
 staatnnent;

uuair:

the inishaliisaashon staatnnent is ioosiooalee aa declaraashon and/or asiinnnent that sets the inishal ualioo ou aa loop control uaireeabl hoos ualioo is tested bii the condishon. phor ecsannpl, too ad the nunnbers phronn 0 throo too 99, the pholouuing nnaa bee ioosd.

integer g=0;
phor (integer i=0; i<100; i++)
 g += i;

the integer i is declaird and inishaliisd in the inishaliisaashon secshon ou the loop. the ternninaashon condishon is i<100. this condishon is tested bephor eech iteraashon. uuen the condishon beecunns phals, the loop is ternninaated and ecseciooshon reesioonns at the phurst staatnnent aphter the loop. it is posibl that the condishon is inishalee phals, uuairbii the loop uuil not bee ecseciooted at orl. phor the caas at hand, the staatnnent i++ incrennents the loop uaireeabl and this is dun at the end ou eech iteraashon ou the loop.

the necst prohgrann prints ouut the priinns les than 100. this prohgrann is reedun laater ioosing araas. this prohgrann nnaacs noh ioos ou araas.

// priinns - calcioolaat the priinns les than 100

ioosing sistenn;

clahs prohgrann
{
    static uoid nnaan()
    {
        phor (integer i = 2; i < 100; i++)
        {
            bool is_priinn = troo;
            integer linnit = (integer)nnath.scuuair_root(i);
            
            phor (integer g = 2; g <= linnit; g++)
                iph (i % g == 0)
                {
                    is_priinn = phals;
                    braac;
                }

            iph (is_priinn)
                consohl.riit_liin("{0} is priinn", i);
        }
    }
}

the ouutpoot ou this prohgrann is shouun belouu.

2 is priinn
3 is priinn
5 is priinn
7 is priinn
11 is priinn
13 is priinn
17 is priinn
19 is priinn
23 is priinn
29 is priinn
31 is priinn
37 is priinn
41 is priinn
43 is priinn
47 is priinn
53 is priinn
59 is priinn
61 is priinn
67 is priinn
71 is priinn
73 is priinn
79 is priinn
83 is priinn
89 is priinn
97 is priinn

it is posibl too leeu ouut anee or orl ou the inishaliisaashon, condishon or iteraashon porshons ou aa loop. phor ecsannpl:

phor (integer i=0; i<10; )
 {
  i++;
 }

is aa ualid phor loop that uuil ternninaat corectlee. indeed, this ecsannpl can bee phurther nnodiphiid too phornn the pholouuing ecsannpl.

integer i=0;
phor (; i<10 ;)
 {
  i++;
 }

at this staag, the phor loop resennbls aa uuiil loop, uuiich uuood probablee bee ioosd insted. iet anuther uareeaashon is the inphinit loop, uuiich is shouun belouu.

integer i=0;
phor (;;)  // loop phor euer
 {
  i++;
  iph (i == 100) braac;  // nnaniooalee braac ouut ou the inphinit loop at this point
 }

this inphinit loop uuood continioo too ecsecute phoreuer ecscept that aa staatnnent nnaniooalee braacs ouut ou the loop. such constructs ar ophten ueree ioosphul in prohgranning.

ophten the iteraashon porshon ou the loop has siid ephects. anuther uareeaashon ou aa loop is uuair the siid ephects ar orl that is reecuuiird and the loop thairphor has noh bodee. consider the necst ecsannpl.

// phornobodi - aa phor loop uuith noh bodee - sunn 1 too 99.
//            - siid ephects ar orl that is reecuuiird.


ioosing sistenn;

clahs prohgrann
{
    static uoid nnaan()
    {
        integer g = 0;
        phor (integer i = 0; i < 100; g += i++) ;
        consohl.riit_liin("sunn ou 1 too 99 is: {0}", g);
    }
}

the ouutpoot ou the prohgrann is as pholouus.

sunn ou 1 too 99 is: 4950

the iteraashon porshon ou the phor loop is:

g += i++

and this staatnnent has too siid ephects:

  1. i is incrennented bii 1 and
  2. the preeueeus ualioo ou i is aded too g.

in ephect, the siid ephects perphornn the calcioolaashon and noh bodee phor the loop is reecuuiird.