Presentation is loading. Please wait.

Presentation is loading. Please wait.

5a – While Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.

Similar presentations


Presentation on theme: "5a – While Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming."— Presentation transcript:

1 5a – While Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming

2 ListBox Revisit ListBox – Allow users to view and select multiple items in a list. Camel Notation – lstXxxx Mainly used for displaying output in our class. Add items to the ListBox: lstXxxx.Items.Add(…) Each time an item is added, it will appear in the next line. Clear a ListBox – lstXxxx.Items.Clear() A ListBox should be large enough to display all of its content or to allow scroll-bars to be used easily

3 Loops Sometimes we need to repeat a certain action many times. E.g. Add a line of “***********************” into the ListBox. lstXxxx.Items.Add(“************************”) If we want to add three lines of the above altogether, we will need to write the above statement three times. The loop structure makes it possible to execute one piece of code repeatedly so we don’t need to write the same line of code again and again.

4 Loops – Do While The piece of code will be repeated until a certain condition is met. Do While Loop – Do While (condition is true) Block of codes Loop –Keep executing the Block of codes while the condition is true. –Stop executing when the condition becomes false. –The condition must become false at certain point so the loop can stop. Otherwise an infinite loop will occur.

5 Loops – Do While –E.g. Dim intCounter As Integer = 1 Do While intCounter < 4 lstOutput.Items.Add(“****************************”) intCounter = intCounter + 1 Loop Note: The intCounter has to increase by one each time to keep track how many times the line has been added. If not, intCounter will always be 1 and the condition is always true.

6 Loops – Do Until 6 Do Until Loop – Do Until (condition becomes true) Block of codes Loop –Keep executing the Block of codes when the condition is false. –Stop executing when the condition becomes true. –The condition must become true at certain point so the loop can stop. Otherwise an infinite loop will occur.

7 Loops – Do Until 7 Do Until Loop – Do Until (condition becomes true) Block of codes Loop –Keep executing the Block of codes when the condition is false. –Stop executing when the condition becomes true. –The condition must become true at certain point so the loop can stop. Otherwise an infinite loop will occur.

8 Loops 8 It is also possible for a Do While or Do Until Loop code block never gets executed. –E.g. Dim intCounter As Integer = 0 Do While intCounter > 10 lstOutput.Items.Add(“****************************”) intCounter = intCounter + 1 Loop


Download ppt "5a – While Loops Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming."

Similar presentations


Ads by Google