INF 110 Visual Basic Programming AUBG Spring semester
INF 110 Visual Basic Programming AUBG Spring semester 2011 Lecture 7 Title: Visual Basic (Repetition/Iteration statements 1) 1
Lecture Contents: § § § Repetition/Iteration statements: Pre Test Loops Post Test Loops Logically Controlled Loops Counter Controlled Loops 2
An iterative statement is one which executes one or more other statements a certain number of times i. e. the execution of one or more statements is repeated The iterative statement comes in a variety of “flavours”. They are used for a number of purposes, such as: • searching a collection of data items for one with a specific value, or • performing a calculation involving a collection of data items (e. g. adding them all together to get a total) or • performing some action repeatedly until an event occurs which tells it to stop (imagine a robot placing boxes on a conveyor belt. . . ) 3
• Repetition statements (also iterative statements) may classify in two sub groups: Do…Loop statements For…Next statements 4
Do Loops • A loop is one of the most important structures in programming. • Used to repeat a sequence of statements a number of times. • The Do loop repeats a sequence of statements either: • as long as, a certain condition is True. • until, a certain condition becomes True. • It has a few variants … 5
Comments • Be careful to avoid infinite loops – loops that never end. • VB. NET allows for the use of either the While keyword or the Until keyword at the top or the bottom of a loop. We will mostly use While at the top of a loop (pretest loop) and Until at the bottom of a loop (posttest loop). • Semantics: “Do these actions while condition is true” or “Do these actions until a condition becomes true” 6
Do Loops in VBasic Pretest loops --------------------- Do While boolean expression statements Loop --------------------Do Until boolean expression statements Loop --------------------7
Do Loops in VBasic Posttest loops --------------------Do statements Loop While boolean expression --------------------Do statements Loop Until boolean expression --------------------8
Do Loops in VBasic Infinite /endless/ loops --------------------Do statements Loop --------------------- 9
Do While … Loop Syntax Do While condition statement(s) Loop Condition is tested. If it is True, the loop is run. If it is False, the statements following the Loop statement are executed. These statements are inside the body of the loop and are run if the condition above is True. condition is a Boolean expression 10
Pseudocode and Flow Chart for a Do Loop 11
Dim Counter As Integer = 20 Do While Counter <= 100 Console. Writeline(Counter) Counter = Counter + 10 Loop The above example will keep on adding until Counter’s value becomes > 100 12
Example Dim Pass. Word As String Pass. Word = “” Do While Pass. Word <> “SHAZAM” Console. Writeline(“Enter password: ") Pass. Word = Console. Readline() Loop Pass. Word is the loop control variable because the value stored in Pass. Word is what is tested to determine if the loop should continue or stop. 13
‘Accumulate sum of the numbers from 1 to 7 Dim Num As Integer = 1 Dim Sum As Integer = 0 Do While Num <= 7 Sum = Sum + Num = Num + 1 'Add 1 to the value of Num Loop 14
Do … Loop Until Since the condition is tested at the bottom the loop, the loop will execute at least once. Do statement(s) Loop Until condition Loop is executed once and then the condition is tested. If it is False, the loop is run again. If it is True, the statements following the Loop statement are executed. 15
Pseudocode and Flowchart for a Do … Loop Until 16
Dim Counter As Integer = 20 Do Console. Writeline(Counter) Counter = Counter + 10 Loop Until Counter > 100 17
Example Dim Pass. Word As String Do Console. Writeline(“Enter password: ") Pass. Word = Console. Readline() Loop Until Pass. Word = "SHAZAM" 18
Examples I=1 Do While (I <= 10) Console. Write. Line(I) I=I+1 Loop How many iterations are to be run? (10) Try it 19
Examples I=1 Do Until I <= 10 Console. Write. Line(I) I=I+1 Loop How many iterations are to be run? (0) Try it 20
Examples to achieve the same effect I=1 Do While (I <= 10) Console. Write. Line(I) I=I+1 Loop ============ same effect I=1 Do Until Not(I <= 10) Console. Write. Line(I) I=I+1 Loop How many iterations are to be run? (10) Try it 21
Examples to achieve the same effect I=1 Do While Not(I <= 10) Console. Write. Line(I) I=I+1 Loop ============ same effect I=1 Do Until (I <= 10) Console. Write. Line(I) I=I+1 Loop How many iterations are to be run? (0) Try it 22
Post test loops with While and Until 23
Do Loops • All the variations use the same basic model. A posttest loop is executed either – While the condition is True. Uses While reserved word Do statement-block Loop While condition – Until the condition becomes True. Uses Until reserved word Do statement-block Loop Until condition 24
Examples I=1 Do Console. Write. Line(I) I=I+1 Loop While (I <= 10) How many iterations are to be run? (10) Try it 25
Examples I=1 Do Console. Write. Line(I) I=I+1 Loop Until (I <= 10) How many iterations are to be run? (1) Try it 26
Examples to achieve the same effect I=1 Do Console. Write. Line(I) I=I+1 Loop While (I <= 10) ========== same effect I=1 Do Console. Write. Line(I) I=I+1 Loop Until Not(I <= 10) How many iterations are to be run? (10) Try it 27
Examples to achieve the same effect I=1 Do Console. Write. Line(I) I=I+1 Loop While Not(I <= 10) ========== same effect I=1 Do Console. Write. Line(I) I=I+1 Loop Until (I <= 10) How many iterations are to be run? (1) Try it 28
From Logically controlled loop To Counter controlled loop Introduction to For … Next Loop statement 29
You use a Do loop when you do not know how many times the statements should be run. If you know that they should be run for a specific number of times, then use a For … Next loop. - coming on next slides! This loop variant uses a counter which increases or decreases on each repetition of the loop and the looping ends when this counter reaches a set value. 30
For … Next Loops • Used when we know how many times we want the loop to execute. • A counter-controlled loop 31
For … Next Loop Syntax The format is: For counter = start. Number To end. Number Step increment One or more VB statements Next starts with counter = start. Number and increments counter each time through the loop by an amount equal to increment until it reaches end. Number. The bounds (start. Number, end. Number) are integer quantities, and Step controls the increments (which may be negative). 32
For … Next Loop Syntax One more format is: For counter = start. Number To end. Number [Step increment] One or more VB statements Next [counter] The brackets [ … ] are meta characters and indicate that the keywords in the square brackets are optional. 33
Control variable Start value Stop value Amount to add to index For index = 0 To n Step s . . . Next 34
For … Next Loop Syntax The most common use of the FOR loop omits Step increment – the default increment is +1, i. e. the counter is increased by 1 each time through the loop. 35
Sample For i = 1 To 5 . . . Next The loop control variable (i. e. counter), i, is – Initialized to 1 – Tested against the stop value, 5 – Incremented by 1 at the Next statement 36
For i = 1 To 5 [Step 1] . . . Next And its corresponding Do While equivalent i = 1 Do While i <= 5 . . . i = i + 1 Loop 37
Examples (a) For counter=1 to 10 Console. Writeline(counter) Next (b) For counter=1 to 100 Step 10 Console. Writeline(counter) Next (c) For counter=100 to 5 Step -5 Console. Writeline(counter) Next 38
For Num = 1 To 10 Num. Squared = Num*Num Console. Writeline(Num. Squared) Next Num The statements between the "For Num = 1 To 10" line and the "Next Num" line are executed ten times. The first time, the variable Num contains the value 1. The second time, Num contains the value 2, the third time, 3, and so on. On the tenth time around (the tenth "iteration") num contains the value 10. After that there are no further iterations - the computer moves on to the line following the "Next num" line. 39
Start, Stop, and Step values • Consider a loop beginning with For i = m To n Step s • The loop will be executed exactly once if m equals n no matter what value s has. • The loop will not be executed at all if m is greater than n and s is positive. • The loop will not be executed at all if m is less than n and s is negative. 40
Thank You For Your Attention! 41
- Slides: 41