### For...Loops

Today Mr. Malandrakis introduced something new to us in class called

**For Loops.**It's used when you know exactly how many times you want to do something.

Before we started working on the code, we were told to put a list box, a command button, and a menu on our form. Our menu had to be called

*For Loop,*and we had to have 7 sub menus.

First step was to declare our variables.

Dim n, x As Integer 'DUMMY COUNTERS USED AS LOOP COUNTERS

Dim userNumber As Integer

We're using "n" and "x" as the variables since it's easier to type rather than using "counter".

Next we needed to bring up the click event for the command button, which we named "cmdClear" because that would clear what was in the listbox.

Private Sub cmdClear_Click()

lstClear.Clear

End Sub

Now next we had to bring up the event for the sub menu. I named mine "mnuLoop". Under that line of code we typed in the select case statement.

Private Sub mnuL_Click(Index As Integer)

Select Case Index

Case 0...

...Case 6

End Case

Now this is where the actual looping begins. For the first Case (Case 0) we had to make a message box pop up 5 times. To do this, you have to first type in "For", then your variable which is "n". when looping you want to put the number that you want to start off with, and the number you want to end at. In this case we want the loop to start at 1 and end at 5. To end the code you'll have to type in "Next n". This is what the code looks like..

Case 0

For n = 1 To 5 'LOOP WHICH EXECUTES 5 TIMES MESSAGE BOX SHOWS UP 5 TIMES

MsgBox "Hi"

Next n 'WHEN PUTTING n IT LETS YOU INCREASE BY ONE EACH TIME.

Oh..and you have to remember to indent..I can't really do that here on blogger..

For case 1 we want to add the loop counter into the list box. for this one, we were told to make the loop go from 1 to 7.

Case 2 'NUMBERS FROM 1 TO 7

For n = 1 To 7 'LOOP EXECUTING 7 TIMES

lstNumbers.AddItem n 'ADDING THE LOOP COUNTER TO THE LISTBOX EACH TIME THROUGH THE LOOP

Next n

Case 2, we had to make the loop count from 1 to 15, but by 2's. since it's starting at one it would be counting by odd numbers. (ex. 1-3-5-7-9...15). When going up by a certain number you have to add in the word "step" in your code. When you add "step" it means to increase by that much each time in the loop.

Case 2 'ODD NUMBERS

For n = 1 To 15 Step 2

lstClear.AddItem n

Next n

Case 3: We wanted the user to enter a number of their choice in an inputbox.

Case 3 'ASK ME

userNumber = InputBox("PLEASE ENTER A NUMBER", "NUMBERS")

'FOR LOOP SHOWING ALL EVEN NUMBERS STARTING AT ZERO

'AND STOPPING AT THE NUMBER ENTERED BY THE USER.

For n = 0 To userNumber Step 2

lstClear.AddItem n

Next n

Case 4: We had to put a loop inside another loop...

Case 4 'NESTED LOOPS

'IN NESTED FOR...LOOPS THE INSIDE LOOP EXECUTES

'COMPLETELY FOR EVERY "STEP" OF THE OUTSIDE LOOP.

For n = 1 To 3 'OUTSIDE LOOP EXECUTING 3 TIMES

For x = 1 To 4 'INSIDE LOOP EXECUTING 4 TIMES

lstClear.AddItem "HI"

Next x 'END OF INSIDE LOOP

Next n 'END OF OUTSIDE LOOP

'"HI" WILL SHOW UP 12 TIMES IN THE LISTBOX.

In case 5 were to start the loop from 100 to 1 and go down by one. Also, to make it divisible by 3. When counting down by 1 you would have to add a negative sign (-) in front of the number. In this loop we had to include an If statement.

Case 5 'multiples of

For n = 100 To 1 Step -1

If n Mod 3 = 0 Then

lstClear.AddItem n

End If

Next n

Last one. In Case 6 we had to make the loop go from 16000 to 1 and go by multiples of 27. Your basically doing the same thing that you did in Case 5, except your changing some bumbers.

Case 6 'multiples of 27 between 1 and 16000

For n = 16000 To 1 Step -1

If n Mod 27 = 0 Then

lstClear.AddItem n

End If

Next n

End Select

For the remainder of the class we looked at the solution for "Motion in the Ocean" We looked at Graeme's because he completed it and got his to work out. GOOD JOB GRAEME !

And that's what we learned and did today. I hope I explained things well and didn't leave out anything important..If I did, I'm real sorry >.<

Next scribe will be Micheal. Since he really wants to to be the next scribe. =D

## 4 Comments:

wow, good post, anyone read that would really understand what we are doing.

keep up the good work lol.

Great job MaryAnn. That was a fabulous post and you really did summarize the class very well. Great job and keep up the good work.

I'm Hot and sexy

NSU - 4efer, 5210 - rulez

Post a Comment

<< Home