AllTheTimeWorld.com

csharp loopsLoops in C#

Loops in C#

Learn C# (C sharp) in FREE step-by-step lessons.

Loops in C#

Loops: for, while, do; Endless loops; Nested loops; Arrays with for each

A loop is a block of statements that is repeated.
C# has three types of loops: while, for and do.
There is also a foreach that works with collections and arrays.

A loop has a Boolean expression to test.
If the expression is true the statements in the block are executed and then the test is performed again.
When the condition is false, the next statement after the loop is executed.
A while loop and a for loop test a Boolean expression at the beginning, a do loop tests the Boolean expression at the end.
While and for loops are executed zero or more times. Do loops are always executed at least once.

The Boolean expression is a check point:
The decision is made to execute the entire loop or not.
If the value of the Boolean expression changes during execution of the loop, the loop continues until the test is made again.
The loop does not end in the middle if the variable changes.

Each pass through the loop is called an iteration.

In order to do this you will:

Please study the material at each of the links below.

  1. References: loops References: loops: View the Microsoft information on loops in C#

  2. Prepare code for loop Prepare code for loop: Statements to add 1,2,3 to list box individually are converted to a while loop.
    /csharp/videos/csharp-loops.mp4
  3. A few notes about the while loop A few notes about the while loop: Cautions about possibility of endless loops and loops that execute 0 times.
    /csharp/videos/csharp-loops2.mp4
  4. Sorted values: If the sorted property of the list box is true, the values are sorted as strings

  5. While, cont.: A while loop generates the powers of 2: 2, 4, 8, 16, 32, 64, 128, 512, 1024

  6. Position of increment in loop: The position of the increment changes the values and also what is added to list box

  7. Introducing the for loop Introducing the for loop: The while loop to add 1, 2, 3 to the listbox is changed to a for loop.
    /csharp/videos/csharp-forloop.mp4
  8. Format of the for Loop: Explanation of the format of the for loop.

  9. Compare while and for Loops: Compares a for and a while loop that each generate the values 1, 2, 3, 4, 5.

  10. Introduction to do Loops Introduction to do Loops: A do loop always executes at least once.
    /csharp/videos/csharp-doloop.mp4
  11. do Loops: A do loop tests the condition at the end of the loop.

  12. Endless do loops: Shows a do loop that is endless because there is no increment of the variable.

  13. Compare for, while, and do Loops: Each of the loops puts the numbers from 1 to 10 in a list box.

  14. Months and Seasons Months and Seasons: The switch/case block is embedded inside the loop
    /csharp/videos/csharp-loop-seasons.mp4
  15. Nested Loops: One loop can be inside another loop, or nested

  16. Nested Loops: generate a deck of cards Nested Loops: generate a deck of cards: A loop for each suit with a nested loop for each card.
    Also uses switch.
    /csharp/videos/csharp-loop-cards.mp4
  17. For Each Loops For Each Loops: The foreach loop works on a collection or an array.
    /csharp/videos/csharp-foreach.mp4
  18. For Each Loops with a List For Each Loops with a List: Use the foreach loop to remove blank lines from a list box.
    /csharp/videos/csharp-foreachlist.mp4
  19. Self Study Questions