In C#, there are several ways to create loops. Here are three of the most commonly used:
while
LoopThe while
loop continues iterating through a block of code as long as a specified condition remains true.
main.cs67 chars5 lines
for
LoopThe for
loop allows you to iterate through a block of code a specific number of times.
main.cs79 chars5 lines
foreach
LoopThe foreach
loop is used to iterate through collections, such as arrays or lists.
main.cs80 chars5 lines
Here is an example of each type of loop in action:
main.cs272 chars21 lines
Output:
main.cs30 chars16 lines
gistlibby LogSnag