Presentation is loading. Please wait.

Presentation is loading. Please wait.

For Loops (ProjFor1, ProjFor2, ProjFor3, ProjFor4, textbox, textbox1) Please use speaker notes for additional information!

Similar presentations


Presentation on theme: "For Loops (ProjFor1, ProjFor2, ProjFor3, ProjFor4, textbox, textbox1) Please use speaker notes for additional information!"— Presentation transcript:

1 For Loops (ProjFor1, ProjFor2, ProjFor3, ProjFor4, textbox, textbox1) Please use speaker notes for additional information!

2 textbox Two areas are being used to show the results of clicking the Calculate button. The area on the left is a PictureBox and the area on the right is a TextBox.

3 textbox The logic in the cmdCalc has a For loop within a For loop. The logic and the syntax will be discussed in the next slides. & means concatenate. There are two very different ways of showing the data in a text box and in a picture box. picAns.Cls will clear out the picture box. The text box is set to “” to clear it. Properties for the text box include setting MultiLine to True to allow this output.

4 For Loop For ct = 1 To 5 answer = ct + ct Next ct This slide will show a simple example using a For loop. Note that the loop starts with the word For and the bottom of the loop is marked by the word Next. ctanswer 1 2 1 2 2 4 2 4 3 6 3 6 4 8 4 8 5 10 5 10 ct is initialized at 1. When next is reached, ct is checked against the limit of 5 to determine if the loop should continue. If the loop should continue then ct is incremented by 1 for the next pass in the for loop. Initialize ct ct has reached 5 so processing ends. answer is calculate as ct + ct as seen inside the loop.

5 This slide will show a simple example using a nested For loop. Note that the loop each starts with the word For and the bottom of the loop is marked by the word Next. ct1 ct2answer 1 1 2 1 1 2 1 2 3 1 2 3 1 3 4 1 3 4 1 4 5 1 4 5 2 1 3 2 1 3 2 2 4 2 2 4 2 3 5 2 3 5 2 4 6 2 4 6 3 1 4 3 1 4 3 2 5 3 2 5 3 3 6 3 3 6 3 4 7 3 4 7 4 1 5 4 1 5 4 2 6 4 2 6 4 3 7 4 3 7 4 4 8 4 4 8 5 1 6 5 1 6 5 2 7 5 2 7 5 3 8 5 3 8 5 4 9 5 4 9 ct1 is initialized at 1. ct2 is initialized at 1. answer is calculated. Next ct2 increments ct2 and goes back to the inner loop. When ct2 reaches 4, control drops through to next ct1. ct1 is incremented by 1 and the inner loop has ct2 reset to 1. Nested For Loop For ct1 = 1 To 5 For ct2 = 1 To 4 answer = ct1 + ct2 Next ct2 Next ct1 ct2 reaches maximum of 4 so ct1 is incremented by 1 and ct2 is reset to 1. ct1 and ct2 have both reached maximum so loop ends.

6 Dim ct1 As Integer, ct2 As Integer, ans As Integer For ct1 = 1 To 5 For ct2 = 1 To 5 ans = ct1 + ct2 txtAns.Text = txtAns.Text & ct1 & "+" & ct2 & "=" & ans & Chr(13) & Chr(10) picAns.Print ct1 & "+" & ct2 & "=" & ans Next ct2 Next ct1 textbox With the text box, additions to the table must be concatenated to what is already there. That is why you see txtAns.Text = txtAns.Text & etc.. I am concatenating what is already in the field with ct1 with the + with ct2 with the = with ans and then with the carriage control and line feed. With the picture box, I used the Print statement and simply coded the data to be printed. Note that ct1 is concatenated with the + which is concatenated with ct2 which is concatenated with the = which is concatenated with ans. Each new Print will show on a separate line.

7 textbox1 Note that I have no set up a scroll bar in the textbox so that I can scroll and see all of the data in the text box.

8 textbox1 When I click on ScrollBars and the down arrow, I see the options which are: 0-None, 1-Horizontal, 2-Vertical and 3-Both. For this example I only needed the vertical scroll bar. MultiLine is set to true - a requirement when you are going to add scroll bars.

9 ProjFor1 Note that I did not put the answer in a work area or variable, I simply coded X + X. In this example, I did not use concatenation. Instead I used the semi-colon to separate the components of the formula.

10 ProjFor2 The range for X is from 1 to 3 and the range for Y is from 1 to 4. X is set to 1. Y is set to 1. The inner FOR loop is processed with X = 1 and Y varying from 1 to 4. When Y would be greater than 4, the inner loop is done and control returns to the outer loop. X is incremented by 1 to make it 2. The the code drops through to the inner loop for a new set of passes. Because control is dropping through rather than returning to the FOR from the NEXT, The inner loop is starting fresh and Y is set to 1. The loop will stop when processing has been done for both X and Y at their maximums. That is when X= 3 and Y = 4 has been processed.

11 ProjFor3 In this example I am using Step so that X and Y are not incremented by the default of 1, but rather by the number specified in the Step.

12 ForProj4 In this example, I am using a negative step. Note that I started X at 8 and want to end when X = 2. The step is -2 so each time X is changed it will be reduced by 2. Y starts at 9 and Y will be decremented by 3 until it has been processed with a value of 3.


Download ppt "For Loops (ProjFor1, ProjFor2, ProjFor3, ProjFor4, textbox, textbox1) Please use speaker notes for additional information!"

Similar presentations


Ads by Google