1 / 38

Programming with Visual Basic .NET

Programming with Visual Basic .NET. While, Do and For – Next Loops Week 5 Tariq Ibn Aziz. Loop Structures. Loop structures allow you to execute one or more lines of code repetitively.

terryfrank
Download Presentation

Programming with Visual Basic .NET

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. Programming with Visual Basic .NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz Compunet Corporation

  2. Loop Structures • Loop structures allow you to execute one or more lines of code repetitively. • You can repeat the statements until a condition is true, until a condition is false, a specified number of times, or once for each object in a collection. • The loop structures supported by Visual Basic include: • While • Do...Loop • For...Next • For Each...Next Compunet Corporation

  3. The While Loop • You can use the While statement to execute a block of statements an indefinite number of times depending on the Boolean value of a condition. • The statements are repeated as long as the condition is True. Compunet Corporation

  4. Get shampoo • Lather • Rinse Shampoo Algorithm What’s the problem? Compunet Corporation

  5. While Loop Example 4 Imports System.Console Public Module WhileDemo Sub Main() CheckWhile() EndSub Sub CheckWhile() Dim Counter AsInteger = 0 Dim Number AsInteger = 10 While Number > 6 Number = Number - 1 Counter = Counter + 1 EndWhile WriteLine ("The loop ran " & Counter & " times.") EndSub EndModule Compunet Corporation

  6. While Loop Example 4 Imports System.Console Public Class WhileDemo Shared Sub Main() CheckWhile() EndSub Shared Sub CheckWhile() Dim Counter AsInteger = 0 Dim Number AsInteger = 10 While Number > 6 Number = Number - 1 Counter = Counter + 1 EndWhile WriteLine ("The loop ran " & Counter & " times.") EndSub EndClass Compunet Corporation

  7. 9 Example (THE while LOOP) • In the following example, Number is assigned a value that could cause the loop to execute more than 2 ^ 31 times. Sub ExitWhileExample() Dim Counter AsInteger = 0 Dim Number AsInteger = 8 While Number <> 10 If Number < 0 ThenExitWhile Number = Number - 1 Counter = Counter + 1 EndWhile MsgBox("The loop ran " & Counter & " times.") EndSub • Note 1: To stop an endless loop, press ESC or CTRL+BREAK. • Note 2: Need Microsoft.VisualBasic namespace for MsgBox Compunet Corporation

  8. Do...Loop Statements • There are two ways to use the While keyword to check a condition in a Do loop. • You can check the condition before you enter the loop • or you can check it after the loop has run at least once. Compunet Corporation

  9. FALSE Condition Condition TRUE Loop body Loop body FALSE TRUE Pre-test and Post-test Loops Compunet Corporation

  10. Do...Loop Statements 4 • In the following example, the CheckWhileFirst procedure tests the condition before it enters the loop. If Number had been initialized to 6 instead of 10, the statements inside the loop would never run. Sub CheckWhileFirst() Dim Counter AsInteger = 0 Dim Number AsInteger = 10 DoWhile Number > 6 Number = Number - 1 Counter = Counter + 1 Loop MsgBox("The loop ran " & Counter & " times.") EndSub Compunet Corporation

  11. Do...Loop Statements 1 • In the following example, the CheckWhileLast procedure, the statements inside the loop run once before testing the condition, which is False on the first test. Sub CheckWhileLast() Dim Counter AsInteger = 0 Dim Number AsInteger = 5 Do Number = Number - 1 Counter = Counter + 1 Loop While Number > 6 MsgBox("The loop ran " & Counter & " times.") EndSub Compunet Corporation

  12. Do...Loop Statements • There are two ways to use the Until keyword to check a condition in a Do loop. • You can check the condition before you enter the loop, • or you can check it after the loop has run at least once. • Looping continues while the condition remains False. Compunet Corporation

  13. Do...Loop Statements Example 5 • In the following example, the CheckUntilFirst procedure tests the condition before it enters the loop. If Number had been initialized to 15 instead of 20, the statements inside the loop would never run. Sub CheckUntilFirst() Dim Counter AsInteger = 0 Dim Number AsInteger = 20 Do Until Number = 15 Number = Number - 1 Counter = Counter + 1 Loop MsgBox("The loop ran " & Counter & " times.") EndSub Compunet Corporation

  14. Do...Loop Statements Example 5 • In the following example, the CheckUntilLast procedure, the statements inside the loop run once before testing the condition, which is False on the first test.. Sub CheckUntilLast() Dim Counter AsInteger = 0 Dim Number AsInteger = 20 Do Number = Number - 1 Counter = Counter + 1 Loop Until Number = 15 MsgBox("The loop ran " & Counter & " times.") EndSub Compunet Corporation

  15. Exercises • Write a program that displays the numbers 1 through 100. • Write a program that displays the numbers 100 to 1, in reverse order. Compunet Corporation

  16. Do...Loop – Exit Do Statements • You can transfer out of a Do loop by using the Exit Do statement. • One use of this is to test for a condition that could cause an endless loop, which is a loop that could run an extremely large or even infinite number of times. • If the condition is True, use Exit Do to escape. • If the condition is False, the loop continues running. Compunet Corporation

  17. 8 Do...Loop – Exit Do Example • In the following example, Number is assigned a value that could cause the loop to execute more than 2 ^ 31 times. The If statement checks for this and exits if it exists, preventing endless looping. Sub ExitDoExample() Dim Counter AsInteger = 0 Dim Number AsInteger = 8 Do Until Number = 10 If Number <= 0 ThenExitDo Number = Number - 1 Counter = Counter + 1 Loop MsgBox("The loop ran " & Counter & " times.") EndSub Compunet Corporation

  18. For...Next Statements • Do loops work well when you do not know in advance how many times you need to execute the statements in the loop • However, when you expect to execute the loop a specific number of times, a For...Next loop is a better choice. • For loop uses a variable called a counter that increases or decreases in value during each repetition of the loop. Compunet Corporation

  19. For...Next Statements • The For loop is used to repeat a statement or block of statements. • The syntax is as follows: For counter [ As datatype ] = start To end [ Step step ] ' Statement block to be executed for each value of counter. Next [ counter ] Compunet Corporation

  20. For...Next Example Public Module ForLoop Sub Main() Dim I AsInteger For I = 0 To 10 System.Console.Write (I & " ") Next I System.Console.WriteLine (" Terminating") End Sub End Module Output: 0 1 2 3 4 5 6 7 8 9 10 Terminating Compunet Corporation

  21. For...Next Example Public Class ForLoop Shared Sub Main() Dim I AsInteger For I = 0 To 10 System.Console.Write (I & " ") Next I System.Console.WriteLine (" Terminating") End Sub End Class Output: 0 1 2 3 4 5 6 7 8 9 10 Terminating Compunet Corporation

  22. Example (for Statement) Public Module ForLoop Sub Main() Dim sum AS Integer=0 Dim prod AS Integer=1 For I AS Integer= 1 To 6 sum = sum + I prod = prod * I Next I System.Console.Write ("Product & Sum:") System.Console.Write (prod) System.Console.Write (" & ") System.Console.Write (sum) End Sub End Module Output:Product & Sum:720 & 21 Compunet Corporation

  23. Example (for Statement) Public Class ForLoop Shared Sub Main() Dim sum AS Integer=0 Dim prod AS Integer=1 For I AS Integer= 1 To 6 sum = sum + I prod = prod * I Next I System.Console.Write ("Product & Sum:") System.Console.Write (prod) System.Console.Write (" & ") System.Console.Write (sum) End Sub End Class Output:Product & Sum:720 & 21 Compunet Corporation

  24. More Practice • Write programs (using loops) to do each of the following: • Sigma(j, k) that solves Si, where i goes from j to k • Product(j, k) that solves Pi, where i goes from j to k • Write a program to find the Factorial of n i.e. n! Compunet Corporation

  25. Incrementing and Decrementing the Counter Variable • One way of Incrementing and Decrementing: • Increases the value n = n + 1 or n += 1 • Decreases the value n = n - 1 or n -= 1 Compunet Corporation

  26. Increment and decrement Public Module IncrementDecrement Sub Main() Dim n AS Integer=6 n += 1 ' 7 n = n + 1 ' 8 System.Console.Write ( n ) ' 8 will be printed n -= 1 ' 7 n = n - 1 ' 6 System.Console.Write ( n ) ' 6 will be printed End Sub End Module Compunet Corporation

  27. Incrementing and Decrementing the Counter Variable • Using the Step keyword, you can increment or decrement the counter by the value you specify. • In the following example, the counter variable J is incremented by 1 each time the loop repeats Sub TwosTotal() Dim J, Total AsInteger For J = 1 To 10 Step 1 Total = Total + 1 Next J MsgBox("The total is " & Total) EndSub Compunet Corporation

  28. Incrementing and Decrementing the Counter Variable • To decrease the counter variable, use a negative Step value. Sub NewTotal() Dim N, Total AsInteger For N = 16 To 4 Step -1 Total = Total + 1 Next N MsgBox("The total is " & Total) EndSub Compunet Corporation

  29. Exiting a For...Next Loop • You can exit a For...Next loop before the counter passes its end value by using the Exit For statement. Compunet Corporation

  30. Exiting a For...Next Loop Sub ExitDoExample() Dim Counter AsInteger = 0 Dim Number AsInteger For Number = 10 To 1 Step -1 If Number <= 0 ThenExitFor Counter = Counter + 1 Loop MsgBox("The loop ran " & Counter & " times.") EndSub Compunet Corporation

  31. Faster For...Next Loops • Integer variables are slightly faster to update than either Short or Long ones. ' First case -- Integer is fastest. Dim Fastest AsInteger For Fastest = 0 to 100000 ' Statements to execute for each value of Fastest. Next Fastest Compunet Corporation

  32. Faster For...Next Loops • Integer variables are slightly faster to update than either Short or Long ones. ' Second case -- Long is not as fast. Dim NotAsFastest AsInteger For NotAsFastest = 0 To 100000 ' Statements to execute for each value of Fastest. Next NotAsFastest Compunet Corporation

  33. Faster For...Next Loops • Integer variables are slightly faster to update than either Short or Long ones. ' Third case -- Decimal is slower. Dim MuchSlower As Decimal For MuchSlower = 0 To 100000 'Statements to execute for each value of Fastest. Next MuchSlower Compunet Corporation

  34. Exercise (THE do LOOP) • Write an application that generates the first 15 numbers in the Fibonacci series (i.e., 1 1 2 3 5 8 13 21 … ) A number in this series is calculated by adding together the two numbers that precede it. Use a Do loop to control the computations. • Show how to replace this For loop with a While For I AS Integer= 1 To 10 Step 2 Next I Compunet Corporation

  35. Nested Loop • When the body of one loop contains another, the second is said to be nested inside the first. For I AS Integer= 0 To 5 Step 1 For J AS Integer= 0 To 5 Step 1 System.Console.Write (I & " ") Next J Next I Compunet Corporation

  36. Exercise (NESTED LOOP) • Write an application that counts the total number of characters in all of its command-line arguments. • Write a program that finds all the prime number between 100 and 200 • Write a program to display the following: 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 Compunet Corporation

  37. Exercise • Write an application that searches through its command-line arguments. If an arguments is found that does not begin with an uppercase letter, display an error message and terminate. Compunet Corporation

  38. Exercise • Write a program that accepts one command-line argument and displays its Spanish equivalent. For example the token "one," "two," "three," "four," and "five" are translated to "Uno," "Dos," "Tres," "Quatro," and "Cinco." • Write an application that can process a sequence of command-line arguments that describe the quantities and types of coins held by a person. This sequence of arguments might be similar to the following: 5 nickles 4 quarters 2 dimes. In this case, the program would display a message indicating that the total value of these coins is $1.45. Compunet Corporation

More Related