There are several ways to create loops in C#:
for
loop:main.cs58 chars5 lines
The above code will generate a loop that runs 10 times.
while
loop:main.cs64 chars8 lines
The above code will generate a loop that runs until i is less than 10.
do-while
loop:main.cs71 chars9 lines
The above code will generate a loop that runs until i is less than 10, but executes at least once even if the condition is false.
foreach
loop:main.cs93 chars7 lines
The above code will generate a loop that iterates through each element of the array numbers
.
goto
loop:main.cs74 chars7 lines
The above code will generate a loop that runs until i is less than 10, but uses a goto
statement to jump back to the top of the loop.
It is recommended to use the for
, while
, do-while
, and foreach
loops, while goto
loops should be avoided as it makes code less readable and maintainable.
gistlibby LogSnag