1 / 19

MIS 3200 – Unit 5.1

MIS 3200 – Unit 5.1. Iteration (looping) while loops for loops Working with List Items. 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). While loop.

hosea
Download Presentation

MIS 3200 – Unit 5.1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MIS 3200 – Unit 5.1 Iteration (looping) while loops for loops Working with List Items

  2. 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)

  3. 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?

  4. While loop using input from TextBox • This example sums all the integers between zero and the number entered in a TextBox

  5. 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();

  6. 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 this for(initialization; condition; increment) { // body of loop }

  7. Comparing WHILE and FOR loops Initialize counter Increment counter Test condition Test condition

  8. 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

  9. 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

  10. 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.

  11. Time to try it out – L1 • While and For loops • Create a new web page in Unit5 • Name the web page lastnameU5L1.aspx • Create a heading in the content area called UNIT 5 L1 WORKING WITH LOOPSand make it an h2 heading • 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.

  12. L1 #2 (what to copy)

  13. L1 #3 (within the U4L2.2) • 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

  14. L1 #4 (back to the U5L1) • 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

  15. L1 #5 • 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 true • 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 true • Double-click on the both buttons to create their click event methods • Add appropriate comments to the methods (this method is going to demonstrate using a while/for loop to process items in a checkboxlist)

  16. L1 #6 (while button) • In your whilebutton method, enter this code:

  17. L1 #7 (for button) • In your FOR button method, enter this code:

  18. L1 #8 • 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.

  19. 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?

More Related