Example Material

Let's look at the topic of Repetition and in particular the while loop to highlight the style in which our material is written and the use of C Shark throughout the code examples and segments:

 

 

Computers have improved so much in the last few years and with the massive gains in computer processing power and memory, computers can perform repeated operations at lighting speeds.

 

In programming, repetition or iteration is a very important and key concept which allows a statement or a group of statements to be executed more than once through the use of loops.

 

 

How do we perform a loop in programming?

 

Well it depends on the type of loop needed and the purpose of the iteration so let's first look at the types of loops available.

 

In programming and C# in particular, there are four main types of loops available to perform iteration:

 

  • The while loop

 

  • The do while loop

 

  • The for loop

 

  • The foreach loop

 

We will take each loop in turn to see the features of the loop and how to implement it in C#.

 

 

The while loop

 

The first loop we will look at is the while loop which is probably the most simplest.

 

The while loop simply executes the statements within the loop's body while the condition is true and will only exit the loop once the condition gets set to false.

 

The while loop syntax is shown below:

 

Statement1 and Statement2 will be executed while the condition is true and will keep executing (looping) until the condition is set to false. Here's a handy diagram to help you visualize this.

C Shark Hint

BEWARE: If the condition never gets set to false, an infinite loop will happen which will eat up all the memory like a hungry shark and break your program.

 

Always ensure the condition is eventually set to false.

We will look at a simple example where C Shark eats continuously and only stops when he is not hungry:

So lets look at the program and explain what it does:

 

  1. First we create a Shark object named "cShark"

  2. The while loop checks if the condition is true before entering the body of the loop i.e. is the Shark hungry

  3. If the Shark is hungry (IsHungry bool variable is true), enter the body of the loop

  4. The Shark then eats by calling the Eat() method which reduces the BellyCapacity int variable by 10

  5. An if statement then checks if the Shark's belly is full i.e. is the BellyCapacity equal to 0

  6. If the BellyCapacity is not 0, the loop then checks if the condition is still true and if so, enters the body of the loop to repeat all the statement again.

  7. If the BellyCapacity equals 0, the Shark is full (IsHungry is set to false) so next time the while loop checks the condition, the condition i.e. IsHunrgy will equal false so the loop will not enter the body and loop will be exited to continue executing statements below the loop.

 

Since the condition is checked before entering the loop's body and executing the statements within, the while loop is known as a pre-test loop. Be careful as there is a chance the loop will never be entered as the condition may never be true.

 

..... More Loops