For loop technical difference

gauravg

New member
Joined
Dec 13, 2006
Messages
3
Programming Experience
1-3
hi

i have worked on both langauage ..vb.net and c#
i have worked on For loop also.

but somebody was saying here is some sort of technical difference in working of them..if naybody can clear...

thanks:)
 
for loops in c# are much more powerful and useful than in VB. In VB they are only capable of incrementing or decrementing a numeric variable until it is beyond a hard coded value. VB typically uses the other loops (while/do) to achieve the additional iterative needs that for loops cannot meet. In c# a for loop can do everything, and the while and do loops exist merely for code readability and self documentation:

VB.NET:
For NUMVAR As TYPE = LOWER To UPPER Step INCREMENT [COLOR=orange]*see note[/COLOR]
Next NUMVAR
 
While BOOL_CONDITION
End While
 
Do
While BOOLEAN_CONDITION

VB.NET:
//as in vb
for(TYPE NUMVAR = LOWER; NUMVAR < UPPER; NUMVAR += INCREMENT)
 
//for loop
for(PRE_LOOP; BOOL_CONDITION; POST_LOOP)
 
executes twice as: pre, bool_cond, body, post, bool_cond, body, post, bool_cond, exit
 
//while loop
for(; BOOL_CONDITION; )
 
//do loop
for(bool doLoop = true; doLoop; doLoop = BOOL_CONDITION)

If you make the Step of a for loop negative, you must ensure that the UPPER is the low value and the LOWER is the high value:
For i As Integer = 10 to 0 Step -1
 
Back
Top