Ms 18
Ms 18
The above code fragment contains two initialization expressions i=1 and sum =0 and two
update expressions sum+=i & ++i. These multiple expressions area executed in sequence
i.e. during initialization firstly i=1 takes place followed by sum=0. Similarly, during updation,
firstly sum +=i takes place followed by ++i
2. Optional expressions: In a for loop, initialization expressions, test expression and update
expression are optional i.e. you can skip any or all of these expressions. Say, for example,
you already have initialized the loop variable and you want to scrap off the initialization
expression then you can write for loop as follows:
for (;test-condition; update expressions (s))
loop body
See, even if you skip initialization expression of a for loop, but the ; following it must be
present.
3. Infinite loop: Although any loop statement can be used to create an infinite (endless loop)
yet for is traditionally used for this purpose. An infinite for loop can be created by omitting
the test-expression as shown below: