mis 3200 – unit 5.1 iteration (looping) – while loops – for loops working with list items
DESCRIPTION
While loop Sample loop to add up all the numbers between 1 and 10 – Create a variable to hold the sum – Create a counter – While the counter isTRANSCRIPT
![Page 1: MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items](https://reader036.vdocuments.mx/reader036/viewer/2022082510/5a4d1afb7f8b9ab059983a50/html5/thumbnails/1.jpg)
MIS 3200 – Unit 5.1
• Iteration (looping)– while loops– for loops
• Working with List Items
![Page 2: MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items](https://reader036.vdocuments.mx/reader036/viewer/2022082510/5a4d1afb7f8b9ab059983a50/html5/thumbnails/2.jpg)
Loop – Walk though
Walkthrough• Start the loop (A)• Check a condition (B)– If the condition is true• do block (C) and then• return to (B)
– If the condition is false• Move on to step (D)
![Page 3: MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items](https://reader036.vdocuments.mx/reader036/viewer/2022082510/5a4d1afb7f8b9ab059983a50/html5/thumbnails/3.jpg)
While loop
Sample loop to add up all the numbers between 1 and 10– Create a variable to hold the sum– Create a counter– While the counter is <= 10
• Add the counter to the sum• Add one to the counter
This can be any logical condition, including compound conditions using && and ||Note: You can use
shortcut operators and the increment operator to shorten these statements. What would that look like?
![Page 4: MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items](https://reader036.vdocuments.mx/reader036/viewer/2022082510/5a4d1afb7f8b9ab059983a50/html5/thumbnails/4.jpg)
While loop using input from TextBox
• This example sums all the integers between zero and the number entered in a TextBox
![Page 5: MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items](https://reader036.vdocuments.mx/reader036/viewer/2022082510/5a4d1afb7f8b9ab059983a50/html5/thumbnails/5.jpg)
While loop using two inputs
• This example sums all the integers between two numbers entered by the user
lblOutput2.Text = "The sum of numbers between " + intStart.ToString();lblOutput2.Text += "and " + intEnd.ToString();lblOutput2.Text += "is " + intSum.ToString();
![Page 6: MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items](https://reader036.vdocuments.mx/reader036/viewer/2022082510/5a4d1afb7f8b9ab059983a50/html5/thumbnails/6.jpg)
For loop
• Loops with the three characteristics mentioned on the last slide are so common that C# has a special control structure called a for loop just for them.
• In a very generic form the loop looks like thisfor(initialization; condition; increment){// body of loop
}
![Page 7: MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items](https://reader036.vdocuments.mx/reader036/viewer/2022082510/5a4d1afb7f8b9ab059983a50/html5/thumbnails/7.jpg)
Comparing WHILE and FOR loops
Initialize counter
Test condition
Increment counter
Test condition
![Page 8: MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items](https://reader036.vdocuments.mx/reader036/viewer/2022082510/5a4d1afb7f8b9ab059983a50/html5/thumbnails/8.jpg)
Working with ListItems
• Assume a user made these selections from a CheckBoxList called cblMenu
– We can determine the total number of items in the list using the Count property of the CheckBoxList’s Items collection
![Page 9: MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items](https://reader036.vdocuments.mx/reader036/viewer/2022082510/5a4d1afb7f8b9ab059983a50/html5/thumbnails/9.jpg)
Working with ListItems #2– Using that count we could loop through the items in the
list and see which ones are selected– When we find a selected item we can get its Text or Value
![Page 10: MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items](https://reader036.vdocuments.mx/reader036/viewer/2022082510/5a4d1afb7f8b9ab059983a50/html5/thumbnails/10.jpg)
Working with ListItems #3
• You can access selected items from a ListBox in exactly the same way
NOTE: There is a bug in the way .NET handles values in ListItem collections (CheckBoxLists, ListBoxes, and later DropDownLists) – they don’t work correctly if two items have exactly the same value! One way to get around this is to make the second item has a slightly different value such as 2.5000001 instead of 2.5, or just have completely different values.
![Page 11: MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items](https://reader036.vdocuments.mx/reader036/viewer/2022082510/5a4d1afb7f8b9ab059983a50/html5/thumbnails/11.jpg)
Time to try it out – L1
• While and For loops1. Create a new web page in Unit52. Name the web page lastnameU5L1.aspx3. Create a heading in the content area called UNIT 5 L1
WORKING WITH LOOPS and make it an h2 heading4. Open your Unit4 lastnameU422.aspx file and copy
starting with the CHECKBOXLIST heading down through the output label for the checkboxlist (basically, all of the U4L2.2 content)
See the next slide for the example of what to copy.
![Page 12: MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items](https://reader036.vdocuments.mx/reader036/viewer/2022082510/5a4d1afb7f8b9ab059983a50/html5/thumbnails/12.jpg)
L1 #2 (what to copy)
![Page 13: MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items](https://reader036.vdocuments.mx/reader036/viewer/2022082510/5a4d1afb7f8b9ab059983a50/html5/thumbnails/13.jpg)
L1 #3 (within the U4L2.2)5. Back in your U4L2.2 file, double-click on the Calculate
sales tax and total button and copy the comments and code from within the method (everything between the start and closing squiggly)
Everything between the squiggles needs to be copied
![Page 14: MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items](https://reader036.vdocuments.mx/reader036/viewer/2022082510/5a4d1afb7f8b9ab059983a50/html5/thumbnails/14.jpg)
L1 #4 (back to the U5L1)6. Back in your U5L1 file, double-click on the Calculate sales
tax and total button and paste the copied comments and code from within the method (everything between the start and closing squiggly)
Once you paste your comments and code into the method, try running your U5L1 and make sure everything works just like the U42.2 at this point
![Page 15: MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items](https://reader036.vdocuments.mx/reader036/viewer/2022082510/5a4d1afb7f8b9ab059983a50/html5/thumbnails/15.jpg)
L1 #57. Press enter after the Calculate sales tax and total button and
add a new button• Change the Text to Demonstrate a WHILE loop• Give it an appropriate name and set the appropriate output label’s
visible property to true8. Press enter after this new button and add a one more new
button• Change the Text to Demonstrate a FOR loop• Give it an appropriate name and set the appropriate output label’s
visible property to true9. Double-click on the both buttons to create their click event
methods10. Add appropriate comments to the methods (this method is
going to demonstrate using a while/for loop to process items in a checkboxlist)
![Page 16: MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items](https://reader036.vdocuments.mx/reader036/viewer/2022082510/5a4d1afb7f8b9ab059983a50/html5/thumbnails/16.jpg)
L1 #6 (while button)11. In your while button method, enter this code:
![Page 17: MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items](https://reader036.vdocuments.mx/reader036/viewer/2022082510/5a4d1afb7f8b9ab059983a50/html5/thumbnails/17.jpg)
L1 #7 (for button)12. In your FOR button method, enter this code:
![Page 18: MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items](https://reader036.vdocuments.mx/reader036/viewer/2022082510/5a4d1afb7f8b9ab059983a50/html5/thumbnails/18.jpg)
L1 #8
7. Create a link to your U5L1 page from your MIS3200 page and copy everything to ASPNET and submit your MIS Portfolio URL to the drop box.
![Page 19: MIS 3200 – Unit 5.1 Iteration (looping) – while loops – for loops Working with List Items](https://reader036.vdocuments.mx/reader036/viewer/2022082510/5a4d1afb7f8b9ab059983a50/html5/thumbnails/19.jpg)
Think About It!
• How is a loop more efficient than using the other conditional testing methods we looked at?
• Is there any time a while loop might have a strategic advantage over a for loop?