Objectives Event Introduction WhileWend Loop Types DoWhile Determinate

Objectives Event Introduction While-Wend Loop Types Do-While Determinate Do-Until Indeterminate For-Next Exit z. Introduction to Computer Programming IT-104 Unit Four – The Loop – Concepts and Uses 9/26/2020 Intro to Comp. Programming - IT 104 1

Objectives Home z. Define and discuss the concept of the Loop/Repetition structure. z. Discuss the use of the Boolean Expression in controlling loop execution. z. Discuss the three types of loops. z. Discuss the Do-While-Loop structure. z. Discuss the Do-Until-Loop structure. 9/26/2020 Intro to Comp. Programming - IT 104 2

Objectives z. Discuss the While-Wend Loop structure. z. Discuss the For-Next Loop structure. z. Discuss the use of pre-test loops and posttest loops. z. Discuss the Exit Do and Exit For commands. 9/26/2020 Intro to Comp. Programming - IT 104 3

Objectives z. Discuss the use of nested loops. 9/26/2020 Intro to Comp. Programming - IT 104 4

Introduction Home In the computer world, it is frequently necessary to repeat an action, which is usually comprised of a group of statements in program code, a number of times. Writing the same statements over and over in the code is very inefficient and wasteful, among other things. 9/26/2020 Intro to Comp. Programming - IT 104 5

Introduction For these reasons, early computer scientists created the loop structure. z. Various languages have their own unique syntactic implementations of the basic loop types and structures. z. Today, we will examine the VB implementation of the most important loop structures. 9/26/2020 Intro to Comp. Programming - IT 104 6

Generic Loop Types Home z. There are 3 basic types of loops in the computer world. y. Determinate Loops y. Indeterminate Loops y. Event-Driven Loops 9/26/2020 Intro to Comp. Programming - IT 104 7

Determinate Loops Home z. A Determinate Loop is one for which the number of times the loop will be executed is known in advance. The number of executions of the loop is hard-coded into the program. For example, we may be counting a number of objects whose maximum number cannot exceed 100. 9/26/2020 Intro to Comp. Programming - IT 104 8

Indeterminate Loops Home z. The Indeterminate Loop is one for which the number of executions is not known in advance. The number of executions is controlled by a Boolean expression. For example, we may be opening a file, whose length varies greatly from day to day, forcing us to create an Indeterminate Loop structure to read the file. 9/26/2020 Intro to Comp. Programming - IT 104 9

Event-Driven Loops Home z. Event-Driven loops are those loops in which either continuing execution, or perhaps, stopping execution, is dependent upon intervention by the user. A Boolean expression, again, typically controls execution of the loop. For example, an input screen for an application may depend upon the user to close it. 9/26/2020 Intro to Comp. Programming - IT 104 10

Event-Driven Loops z. The screen will prompt the user to continue or stop input at the completion of each input cycle. 9/26/2020 Intro to Comp. Programming - IT 104 11

The While-Wend loop Home z. The While-Wend loop is our first example of an Indeterminate Loop. Termination of the loop is controlled by the Boolean expression immediately after the While keyword. The expression may be a simple expression, or it may compound and quite complex. 9/26/2020 Intro to Comp. Programming - IT 104 12

While-Wend loop The syntax of the While-Wend loop is as follows : While expression (evaluates to TRUE) loop statement(s) Wend 9/26/2020 Intro to Comp. Programming - IT 104 13

While-Wend loop Example : Dim int. Product as Integer int. Product = 2 While int. Product <= 1000 int. Product = int. Product * 2 Debug. Print int. Product Wend 9/26/2020 Intro to Comp. Programming - IT 104 14

While-Wend loop z. The keyword While is used to control execution of the Boolean expression. As long as the expression is true (While), the execution of the loop structure will continue. Execution is only halted when the expression evaluates to FALSE. z. Once the expression evaluates the loop 9/26/2020 Intro to Comp. Programming - IT 104 15

While-Wend loop terminates immediately, and processing resumes at the next line after the keyword terminating the loop structure. z. If the Boolean expression initially evaluates to FALSE, then the loop will not execute, and again processing resumes at the keyword terminating the loop. 9/26/2020 Intro to Comp. Programming - IT 104 16

The Do-While Loop Home z. The Do-While Loop operates largely in the same way as the While-Wend loop. It is also an Indeterminate loop, but there are some differences, as we shall see when we define post and pre-loop evaluations. A Boolean expression again controls execution of the structure. The syntax is shown on the next slide. 9/26/2020 Intro to Comp. Programming - IT 104 17

The Do-While Loop The Syntax of the Do-While Loop is : Do While expression (evaluates to TRUE) statement(s) executed while expression evaluates to TRUE Loop 9/26/2020 Intro to Comp. Programming - IT 104 18

The Do-While Loop Example : Dim int. Counter As Integer ‘int. Counter is initialized to 0 Do While int. Counter <= 255 Debug. Print Ascii(int. Counter) int. Counter = int. Counter + 1 Loop 9/26/2020 Intro to Comp. Programming - IT 104 19

The Do-While Loop z. The preceding code example uses a loop to print the Ascii character set, using the Do-While loop and the Ascii function. z. The statement Debug. Print will open an window known as the Immediate Window, which is part of the debug machinery built in to the VB compiler, and the Ascii characters (those that have a printable 9/26/2020 Intro to Comp. Programming - IT 104 20

The Do-While Loop representation will then be printed into the Immediate Window. Each character will be printed on its own line, due to the way in which the print command was coded. 9/26/2020 Intro to Comp. Programming - IT 104 21

The Do-Until Loop Home z. The Do-Until Loop operates similarly to the Do-While Loop. It is controlled by a Boolean expression again, but it will only execute if the expression evaluates to FALSE. As with the Do-While loop, this loop is an Indeterminate loop. z. As in the case of the Do-While loop, if the initial evaluation is TRUE, or when the expression finally becomes TRUE, then 9/26/2020 Intro to Comp. Programming - IT 104 22

The Do-Until Loop the loop terminates and processing resumes at the next line after the Loop statement. z. The syntax of the Do-Until Loop is shown on the next slide. 9/26/2020 Intro to Comp. Programming - IT 104 23

The Do-Until Loop The Syntax of the Do-While Loop is : Do Until expression (evaluates to FALSE) statement(s) executed while expression evaluates to FALSE Loop 9/26/2020 Intro to Comp. Programming - IT 104 24

The Do-Until Loop Example : Dim int. Counter As Integer ‘int. Counter is initialized to 0 Do Until int. Counter > 255 Debug. Print Ascii(int. Counter) int. Counter = int. Counter + 1 Loop 9/26/2020 Intro to Comp. Programming - IT 104 25

Pre-test vs. Post-test loops z. There are two ways to implement either a Do-While or a Do-Until loop. z. The expression can be inserted immediately after the keyword Do, and we have a pre-test loop. z. By inserting the expression immediately after the keyword Loop, we have a posttest loop. 9/26/2020 Intro to Comp. Programming - IT 104 26

Pre-test Condition (Example) z Pre-test : int. Counter = 6 Do Until int. Counter > 5 int. Counter = int. Counter + 1 Debug. Print int. Counter Loop Output of this loop will be : 9/26/2020 Nothing Intro to Comp. Programming - IT 104 27

Post-test Condition (Example) z Post-test : int. Counter = 6 Do int. Counter = int. Counter + 1 Debug. Print int. Counter Loop Until int. Counter > 5 Output of this loop will be : 9/26/2020 6 Intro to Comp. Programming - IT 104 28

The For-Next loop Home z. It is possible to implement a determinant loop using Do-Until or Do-While loops, but Visual Basic has a structure that was made for this case, and the structure has its own built-in counter. z. The For-Next loop is the name of this structure, and the syntax is shown on the next slide. 9/26/2020 Intro to Comp. Programming - IT 104 29

The For-Next loop For variable = Starting. Value to Ending. Value Step n Statement(s) to be executed go here Next variable z The simplest way to illustrate the use of this structure is with an example : For int. Counter = 0 to 25 Step 1 Debug. Print int. Counter Next int. Counter 9/26/2020 Intro to Comp. Programming - IT 104 30

The For-Next loop z. The example on the preceding slide operates by assigning an initial value of zero (0) to the variable int. Counter. z. Execution then continues with the next line, which simply prints the current value of int. Counter to the Immediate Window. z. Execution then continues with the next line, which contains the statement ‘Next’. 9/26/2020 Intro to Comp. Programming - IT 104 31

The For-Next loop z. This statement then returns execution to the line containing the For keyword, where the variable is incremented (in this case one is added to the existing value of the variable), and an comparison is made to see if the current value of the variable is equal to the ending value of the loop (in this case 25). 9/26/2020 Intro to Comp. Programming - IT 104 32

The For-Next loop z. Execution of the loop will continue in this manner until the value of int. Counter does equal 25, at which point the evaluation in the For statement will terminate the loop, and execution of the program then continues with the next line following the ‘Next’ statement. 9/26/2020 Intro to Comp. Programming - IT 104 33

The For-Next loop z. The key to understanding the For-Next loop is that we must have a beginning value, and an ending value for our variable. z. The Step value (which we explicitly stated in our example) has a default value of one. 9/26/2020 Intro to Comp. Programming - IT 104 34

The For-Next loop z If a programmer intends to use the For-Next loop to count up by a Step value of one, the Step and Step value may be omitted completely. For int. Counter = 0 to 25 Debug. Print intcounter Next 9/26/2020 Intro to Comp. Programming - IT 104 35

The For-Next loop z. The preceding example is functionally identical to the previous example given. z. Note that int. Counter was also omitted after the ‘Next’ keyword in the preceding example. If you are using Visual Basic 6. 0 or later, you may omit the second mention of the variable, except in nested loops. 9/26/2020 Intro to Comp. Programming - IT 104 36

The For-Next loop z. The loop may also be used to count down as in the following example. For int. Counter = 60 to 0 Step – 1 Debug. Print int. Counter Next z. Note that since we are now not counting up by one, the Step value must be explicitly stated. 9/26/2020 Intro to Comp. Programming - IT 104 37

The For-Next loop z The For-Next loop can also be used to count up or down by any Step value we wish as in the following example. For int. Counter = 0 to 100 Step 5 Debug. Print int. Counter Next z This loop will count to 100 by 5 and print the results of that count in the Immediate Window. 9/26/2020 Intro to Comp. Programming - IT 104 38

Infinite loops Home z. A loop statement usually has a terminating condition that will eventually occur, causing the loop to end. z. If a programmer is careless, he may create a loop for which this condition will never be met. z. This creates an infinite loop, which is a 9/26/2020 Intro to Comp. Programming - IT 104 39

Infinite loops loop which, once initiated, will continue to execute forever. z. If you accidentally create such and test such a loop, Windows usually responds by locking all other processes and dedicating the processor cycles to the loop exclusively. 9/26/2020 Intro to Comp. Programming - IT 104 40

Infinite loops z. The only way out of such a loop is to intercept the process using the Task Manager in Windows (if your version will allow that operation to occur) and use the dialog to end the process. z. There are times when the programmer intentionally wishes to create an infinite loop. 9/26/2020 Intro to Comp. Programming - IT 104 41

Infinite loops z. Windows programs, and Windows itself could not exist without the infinite loop. z. Obviously, a way to exit an infinite loop would then be useful. The Exit Do and Exit For commands were created for this purpose. The following slide illustrates examples of each. 9/26/2020 Intro to Comp. Programming - IT 104 42

Infinite loops Examples : Do While TRUE If txt. End. Program. Text = “Yes” Then Exit Do End If Loop 9/26/2020 Intro to Comp. Programming - IT 104 43

Infinite loops Or : For int. Counter = 1 to int. Num. Customers If str. Cust. Last. Name = “Etter” Then Msg. Box “Found your Name”, vb. Exclamation, _ “Customer Search” Exit For End If Next 9/26/2020 Intro to Comp. Programming - IT 104 44

Nested Loops z. As in the decision structures, it is often the case that loops must be nested to perform a particular program action. z. The next slide shows a nested loop application. 9/26/2020 Intro to Comp. Programming - IT 104 45

Nested Loops For int. Counter = 0 to int. Num. Customers For int. Counter 1 = 0 to 20 If obj. Text = vb. Null Debug. Print int. Num. Customers. obj. Text(int. Counter) End If Next int. Counter 1 Next int. Counter 9/26/2020 Intro to Comp. Programming - IT 104 46

Nested Loops z. The important point about nested loops is like the nested decision structures, the inner loop must be entirely contained within the outer loop, and different control variables must be used. 9/26/2020 Intro to Comp. Programming - IT 104 47

Summary z. Define and discuss the concept of the Loop/Repetition structure. z. Discuss the use of the Boolean Expression in controlling loop execution. z. Discuss the three types of loops. z. Discuss the Do-While-Loop structure. z. Discuss the Do-Until-Loop structure 9/26/2020 Intro to Comp. Programming - IT 104 48

Summary z. Discuss the While-Wend Loop structure. z. Discuss the For-Next Loop structure. z. Discuss the use of pre-test loops and post -test loops. z. Discuss the Exit Do and Exit For commands. 9/26/2020 Intro to Comp. Programming - IT 104 49

Summary z. Discuss the use of nested loops. 9/26/2020 Intro to Comp. Programming - IT 104 50

LAB Work 9/26/2020 Intro to Comp. Programming - IT 104 51

LAB Work 9/26/2020 Intro to Comp. Programming - IT 104 52

Assignment z. Read Chapter 5 9/26/2020 Intro to Comp. Programming - IT 104 53

Next Week : 9/26/2020 Intro to Comp. Programming - IT 104 54
- Slides: 54