Parag’s Den

Dedicated to my adored friends…..

07- Loops

Posted by paragdongre on June 4, 2008


For Loop

Loops

Count the number of cups you need to fill a pitcher. This number is feasible and may be programmed on a computer as seen below.

 
        cups = 0
        ‘pitcher is now empty.’
        cups = cups + 1
        cups = cups + 1
        cups = cups + .5
        ‘pitcher is now full.’
        msgbox(cups)

In the code above, you will notice that you can actually count the cups one by one. You will know when the pitcher is empty and you will know when the pitcher is full. In some cases, counting things one by one isn’t actually practical anymore. For instance, what if you were asked to count the number of cups needed to fill a swimming pool? Does this sound fun to you? If you are really bored and have nothing to do with your life then you might as well count the number of cups one by one. If you are knowledgeable with loops then you can let the computer do the counting.

For

The For Loop is called the For Loop because it loops “for every number.” In the code below, you will see that the For Loop adds “for every number.”

        
        counter = 0
        sum = 0
        for counter = 1 to 50
          sum = sum + counter
        next counter
        msgbox(sum)

If you loop the code in your head, it would go like 1 + 2 + 3 + 4 + … … + 49 + 50 = sum. “For every” number, the sum is incremented.

The For Loop takes on the structure below.

        
        For [variable] = [1 to N]
         [command lines]
        next [variable]

 

Do Loops

Do

The Do Loop is called the Do Loop because it “does commands” under certain conditions. There are two types of the Do Loop.

Do While

The Do While Loop does commands while a certain condition holds. See the code below for an example of the Do While Loop

 
 Do while x < 100
  x = x + 1
 Loop
 Msgbox (x)

You can also put the While condition after the Loop command. See the code below for an example.

        
 Do
  x = x + 1
 Loop While x < 100
 Msgbox (x)

If the while is put at the Do line then it will check the condition before it executes the commands. If the while is put at the Loop line then it will execute the commands first before checking the condition. The program will loop based on the conditions given.

The Do While Loop takes on the basic structures below.

        
 Do While [condition}
  [command lines]
 Loop

OR

        
 Do
  [command lines]
 Loop While [condition]

Do Until

The Do Until Loop is called a Do Until Loop because it runs commands until a certain condition is satisfied. Check out the code below for an example.

        
 Do until x = 7
  x = x + 1
 Loop
 Msgbox (x)

Like the Do While Loop, you can also put the until command on the Loop line just like the code below.

        
 Do
  x = x + 2
 Loop Until x = 12
 Msgbox (x)

If the Until is put on the Do line then the computer will check the condition before it performs the commands while if the Until is put on the Loop line then the computer will execute the commands first before checking the conditions.

The Do Until Loop takes on the structures below.

        
 Do Until [condition]
  [command lines]
 Loop

OR

        
 Do
  [command lines]
 Loop Until [condition]

 

Loops and User Forms

Awhile ago, a question was posed if it was possible to count the number of cups to fill a swimming pool. Now that you are equipped with the knowledge of loops, try making a program that can calculate the number of cups needed to fill a swimming pool.

 

You can make a program that can calculate this number without using loops but use loops now for exercise purposes. You will benefit from this later on.

Your code may look something like the one below.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        

 

 ‘one liter is equivalent to .001 cubic meters OR m^3        

‘one cup is equivalent to approximately 230 milliliters OR .00023 m^3         

 

Dim Depth As Double        

Dim Length As Double        

Dim Width As Double         

Depth = TextBox1.Text        

Length = TextBox2.Text       

 Width = TextBox3.Text         

Dim Volume As Double         

Volume = Depth * Length * Width         

Dim Cups As Double        

Dim Counter As Double          

Counter = 0        

Cups = 0         

Do Until Counter > Volume           

  Cups = Cups + 1            

Counter = Counter + 0.00023         

Loop         

Counter = Counter – 0.00023        

Cups = Cups + (Volume – Counter) / 0.00023         

TextBox4.Text = Cups     

End Sub 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: