Chapter 6: Loops (Iterations/Repetition) Structures ISTN103 Topic: Programming Chapter 6: Loops (Iterations/Repetition) Structures Chapter 6 Contents Introduction to Loops (Iteration/Repetition) ................................................................ 2 For…..Next loop ............................................................................................................. 3 Do loop also known as Do….While loop ......................................................................... 4 While…..End While loop ................................................................................................ 5 References...................................................................................................................... 6 1 Chapter 6: Loops (Iterations/Repetition) Structures Introduction to Loops (Iteration/Repetition) In general, statements are executed sequentially: the first statement in a function is executed first, followed by the second, and so on. There may be a situation when you need to execute a block of code several number of times repetitively. Programming languages such as Visual Basic provide various control structures that allow for more complicated execution paths. One of the control structures that can be used for complicated execution paths is Loops also known as Iterations/Repetition. A loop statement allows the execution a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages. Loops (Iteration/Repetition) is a process that involves a procedure that runs repetitively until a certain condition is met. For example, we can design a program that adds a series of numbers until the sum exceeds a certain value or a program that asks the user to enter data repeatedly until he or she enters the word 'Finish'. Visual Basic loop structures allow you to run one or more lines of code repetitively. You can repeat the statements in a loop structure until a condition is True, until a condition is False, a specified number of times, or once for each element in a collection. The following illustrations show a loop structure that runs a set of statements until a condition becomes true: There are three major types of Loops in Visual Basic 2015, namely the: For….. Next loop Do loop While…..End While loop 2 Chapter 6: Loops (Iterations/Repetition) Structures For…..Next loop In Visual Basic, For loop is useful to execute a statement or a group of statements repeatedly until the defined condition returns true. Generally, the For loop is useful in Visual Basic applications to iterate and execute a certain block of statements repeatedly until the specified number of times. You use the For...Next structure when you want to repeat a set of statements a set number of times. It uses a loop control variable, also called a counter, to keep track of the repetitions. You specify the starting and ending values for this counter, and you can optionally specify the amount by which it increases from one repetition to the next. Generally, the For loop is useful when we are sure about how many times we need to execute the block of statements. Following is the syntax of defining the For…Next loop. Visual Basic For Loop Syntax In Visual Basic 2015, the structure of a For...Next loop is as shown below: For counter=startNumber to endNumber (Step increment) One or more Visual Basic 2015 statements Pictorial representation of For loop process flow diagram Next Example: The following program will enter number 1 to 10 into the list box. Dim counter As Integer For counter = 1 to 10 ListBox1.Items.Add (counter) Next Example: The following program will calculate the sum of the numbers 1+10+20+30+40+……+100 Dim counter As Integer Dim sum As Integer For counter = 1 to 100 step 10 sum+ = counter ListBox1.Items.Add(sum) Next In order to exit a For…..Next Loop, you need to place the Exit For statement within the loop. It is normally used together with the If….Then statement. In the code example, we used Exit statement to exit from For loop whenever the variable i value equals to 3. Example: Whenever the variable i value equals to 3, then automatically the For loop execution has stopped. For i As Integer = 1 To 4 If i = 3 Then Exit For ListBox1.Items.Add (i) Next 3 Chapter 6: Loops (Iterations/Repetition) Structures Do loop also known as Do….While loop The Do... Loop allows you to test a condition at either the beginning or the end of a loop structure. You can also specify whether to repeat the loop while the condition remains True or until it becomes True. The Do…loop repeats a block of statements while a Boolean condition is True or until the condition becomes True. The DoWhile loop will execute the statements at least once because first it will execute the block of statements and then it will checks the condition. Syntax for Do loop: Do Block of statements Loop While condition Pictorial representation of Do loop process flow Do // Statements to Execute Loop While boolean_expression In the above syntax, Do…..While loop starts with Do keyword followed by the block of statements and While with a parameter called boolean_expression. Example: The program will enter number 1 to 4 into the list box. Dim i As Integer = 1 Do ListBox1.Items.Add (i) i += 1 Loop While i <= 4 If you observe the above example, first we are executing the statements within the Do-While loop and increasing the variable i (i++) value to 1 by using increment operator. After that, the condition (i <= 4) will be evaluated and again it will execute the block of statements in case the defined condition returns true otherwise it will terminate the loop. In Visual Basic, we can exit or terminate the execution of Do-While loop immediately by using Exit keyword. Following is the example of using Exit keyword in DoWhile loop to terminate the execution of loop in Visual Basic programming language. Dim i As Integer = 1 Do ListBox1.Items.Add (i) i += 1 If i = 5 Then Exit Do Loop While i < 10 If you observe the above example, whenever the variable (i) value become 5 we are terminating the loop using Exit statement. 4 Chapter 6: Loops (Iterations/Repetition) Structures While…..End While loop In the previous section, we learned about For loop in Visual Basic with examples. Generally, the For loop is useful when we are sure about how many times we need to execute the block of statements. In case, if we are unknown about the number of times to execute the block of statements, then While loop is the best solution. In Visual Basic, the While loop is useful to execute the block of statements as long as the specified condition is true. In Visual Basic 2015, the structure of a While….End While Loop is very similar to the Do loop. It takes the following syntax. Syntax of the While….End While loop: Generally, we will use While keyword to create a while loop in Visual Basic applications. Following is the syntax of defining a while loop in Visual Basic programming language to execute the block of statements as long as the defined condition is true. While conditions Pictorial representation of While loop process flow in Visual Basic programming language. Visual Basic 2015 statements End While Example: will keep on adding a number by 1 and display the results in a list box Dim sum, n As Integer While n <> 10 n += 1 sum += n ListBox1.Items.Add(n & vbTab & sum) End While If you observe the above syntax, we used While keyword to define the while loop and it contains a parameter called boolean_expression. Here, if boolean_expression returns true, then the statements inside of while loop will be executed. After executing the statements, again the boolean_expression will be evaluated to execute the statements within the While loop. In case, the boolean_expression is evaluated to false, then the While loop stops the execution of statements and the program will come out of the loop. 5 Chapter 6: Loops (Iterations/Repetition) Structures References https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/controlflow/loop-structures#for-loops https://www.vbtutor.net/vb2015/vb2015_lesson15.html https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/controlflow/loop-structures https://www.tutlane.com/tutorial/visual-basic/vb-for-loop https://www.tutlane.com/tutorial/visual-basic/vb-while-loop https://www.tutlane.com/tutorial/visual-basic/vb-do-while-loop https://books.google.co.za/books?id=21FthymRusoC&pg=PA187&dq=visual+basic+loops&hl=en&sa=X&ved=2a hUKEwjwhqWVx83zAhWRQ0EAHeGVAIUQ6AF6BAgEEAI#v=onepage&q=visual%20basic%20loops&f=false https://www.tutorialspoint.com/vb.net/vb.net_loops.htm 6