VB.NET Cheat Sheet By: Andrew Kiceniuk
Total Page:16
File Type:pdf, Size:1020Kb
VB.NET Cheat Sheet By: Andrew Kiceniuk Unit 3: Iteration Three Basic Looping Structures: Note: In all three looping structures outlined below the code inside the loop will repeat, so long as the condition evaluates to true. As soon as the condition is false, the loop is broken and the code inside the loop will no longer execute.
1. Do is best used when you want code to repeat for an unfixed number of times always executes the code inside the loop at least once because the condition is at the end of the loop
General Structure Code Example
Do Do ‘execute the code strName = InputBox(“Enter Name”) ‘listed here strAllNames = strAllNames & strName Loop While condition = true Loop While strName <> “”
Note: strName will equal “” if no input is entered into the InputBox, or if the ‘Cancel’ button is pressed on the InputBox
2. While code inside the loop is may not execute at all, because the condition is at the beginning of the loop
General Structure Code Example
strPlay = InputBox(“Want To Play?”) Do While condition = true Do While strPlay = “YES” ‘execute the code ‘code for a game ‘listed here ‘code for a game Loop Loop
3. For is best used when you want code to repeat a fixed number of times a counter variable is necessary for this looping structure after the loop executes the code inside the loop it will by default increase the counter by 1 use the keyword ‘Step’ to count up or down by a value other than the default of 1
General Structure Code Example
For counter To number For intIndex = 1 To 10 ‘execute the code strNumbers += vbCrLf & intIndex ‘listed here Next intIndex Next counter Me.txtNumbers.Text = strNumbers VB.NET Cheat Sheet By: Andrew Kiceniuk Unit 3: Iteration General Structure For Specified Counter Values Code Example For counter To number Step number For intIndex = 1 To 10 Step 2 ‘execute the code strNumbers += vbCrLf & intIndex ‘listed here Next intIndex Next counter Me.txtNumbers.Text = strNumbers