C# Loops - Lessons

 

Introduction to LOOPs

Computers were designed to do boring repetitive tasks. That brings us to Loops.

In this video you will see a list of the topics I’ve thrown together which belong to the Loop Structure world today. You will note that this is not as extensive as the decision topics. There is basically three loop types and a couple of operators.

There's the WHILE loop. Next is the FOR loop. Finally we have the DO-WHILE loop. This section is easy to learn. Three types of loops.

List Box Intro (review)

The ListBox is basically an object that displays more than one item. It’s like a super label in a way. In other words, a collection of items can be displayed at once. If the collection is too large to display in its entirety a scroll bar automatically appears allowing you navigate up and down through the list.

The WHILE Loop

This is a pretest loop. Meaning it checks a condition and if the condition is true, it executes a block of code. It does this repeatedly until the condition is false. When the condition is false the block of code is no longer executed, and the program continues down its normal path.

The FOR Loop

You as the programmer have total control on how you use this loop.  You set up where it starts.  You set up how long it should run.  You set up how it should move forward or backward.  And it’s easy to use.

You use and integer variable as your counter.  You use this counter to step though your loop.  This counter is what you check against your Boolean statement to determine if it should keep running or now.  The counter is key here.

The DO WHILE Loop

This loop is not to be confused with the WHILE loop.  In fact this is a Post test loop.  It checks a condition after a block of code has been executed.  Only after that block of code has been executed does the test actually occur.  And, of course, it gets tested every time a block of code gets executed.   It does this repeatedly until the condition is false.  When the condition is false the program continues down its normal path.

What you need to understand here is that your block of code will execute even if the value of your test is false.  It forces that first execution.