Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 71 Repetition - Do Loops n A Loops, is used to repeat a sequence of statements a number of time n There are two loops commands in Visual Basic.

Similar presentations


Presentation on theme: "Chapter 71 Repetition - Do Loops n A Loops, is used to repeat a sequence of statements a number of time n There are two loops commands in Visual Basic."— Presentation transcript:

1 Chapter 71 Repetition - Do Loops n A Loops, is used to repeat a sequence of statements a number of time n There are two loops commands in Visual Basic 1. Do Loops 2. For…Next loops. n A Do Loop repeats a sequence of statements either as long as or until a certain condition is true. n A Do statement proceeds the sequence of statements, and a loop statement follows the sequence of statement. n The condition, along with either the word While or Until, follows the word Do or the word Loop.

2 Chapter 72 Repetition - Do Loops n Do loop of the form Do While condition statement(s) Loop It first checks the truth value of condition. If condition is false, then the statements inside the loop are not executed n The program continues with the line after the Loop statement. n If condition is true, then the statements inside the loop are executed. n The entire process is repeated, beginning with the test of condition in the Do While statement, In other worlds, the statements inside the loop are repeatedly executed only as long as the condition is true.

3 Chapter 73 Repetition - Do Loops n Flowchart for a Do While Loop.

4 Chapter 74 Repetition - Do While Loops n Example 1 (p251) n The following segment displays the numbers from 1 through 10. Private Sub cmdDisplay_Click() Dim num As Integer 'Display the numbers from 1 to 10 num = 1 Do While num <= 10 picNumbers.Print num; num = num + 1 Loop End Sub

5 Chapter 75 Repetition - Do While Loops n Example 2 - p.251 Required user gives password before accessing a file n Private Sub cmdDisplay_Click() Dim passWord As String, info As String If UCase(txtName.Text) = "SECRET.TXT" Then passWord = "" Do While passWord <> "SHAZAM" passWord = InputBox("What is the password?") passWord = UCase(passWord) Loop End If Open App.Path & "\" & txtName.Text For Input As #1 Input #1, info picItem.Cls picItem.Print info Close #1 End Sub

6 Chapter 76 Repetition - Do Until Loops n In examples 1 and 2 the condition was checked at the top of the loop- that is, before the statements were executed. n Alternatively, the condition can be checked at the bottom of the loop when the statement Loop is reached. Do statement(s) Loop Until condition n It executes the statements inside the loop and then checks the truth value of condition. If condition is true, then the program continues with the line after the loop statement. n If condition is false, then the entire process is repeatedly executed until condition is true.

7 Chapter 77 Repetition - Do Until Loops n Flowchart Of Do Until Loop

8 Chapter 78 Repetition - Do Until Loops n The following program is equivalent to Example 2, except that the condition is tested at the bottom of the loop (p253) Private Sub cmdDisplay_Click() Dim passWord As String, info As String If UCase(txtName.Text) = "SECRET.TXT" Then Do passWord = UCase(InputBox("What is the password?")) Loop Until passWord = "SHAZAM" End If Open App.Path & "\" & txtName.Text For Input As #1 Input #1, info picItem.Cls picItem.Print info Close #1 End Sub

9 Chapter 79 List of Data with Do loops n One of the main applications of programming is the processing of lists of data from a file n Data to be processed are often retrieved from a file by a Do loop. n EOF function, Counters, flags, accumulators and Nest loops are useful function in Visual Basic

10 Chapter 710 EOF function n Suppose a file has been opened with reference number n, At any time, the condition EOF(n) will be true if the end of the file has been reached, and false otherwise. n Example (p.263) Private Sub cmdDisplay_Click() Dim nom As String, phoneNum As String picNumbers.Cls Open App.Path & "\PHONE.TXT" For Input As #1 Do While Not EOF(1) Input #1, nom, phoneNum picNumbers.Print nom, phoneNum Loop Close #1 End Sub

11 Chapter 711 EOF function n Flowchart for processing data from a file

12 Chapter 712 Counter & Accumulator n A counter is a numeric variable that keeps track of the number of items that have been processed. n An accumulator is a numeric variable that totals number. n Example (p.265 Ex.23) n Do While Not EOF(1) numCoins=numCoins +1 ----------- Counter sum= sum+value ------------------- Accumulator n Loop

13 Chapter 713 Flag n A flag is variable that keeps track of whether a certain situation has occurred. The data type most suited to flags is the Boolean data type. n Explain Example (p.266 ex. 4) The flag variable called order flag, is initially assigned the Value True an is set to False if a pair of adjacent words is out of order.

14 Chapter 714 Nested Loop n Statements inside of Do loop can consist of another Do loops. n Such a configuration is referred to as nested loops and is useful in repearting a single data-processing routine several times. n Explain Example: (page 267 ex5) DO While (.........)....................... Do While (..........)........... Loop....................... Loop

15 Chapter 715 Exercises n Ex 6.1 no. 3, 4, 7, 21, 23 n Ex 6.2 no. 1, 5, 21


Download ppt "Chapter 71 Repetition - Do Loops n A Loops, is used to repeat a sequence of statements a number of time n There are two loops commands in Visual Basic."

Similar presentations


Ads by Google