Repetition Do Loops n n n A Loops

  • Slides: 15
Download presentation
Repetition - Do Loops n n n A Loops, is used to repeat a

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

Repetition - Do Loops n Do loop of the form Do While condition statement(s)

Repetition - Do Loops n Do loop of the form Do While condition statement(s) Loop n n n It first checks the truth value of condition. If condition is false, then the statements inside the loop are not executed The program continues with the line after the Loop statement. If condition is true, then the statements inside the loop are executed. 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. Chapter 7 2

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

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

Repetition - Do While Loops n n Example 1 (p 251) The following segment

Repetition - Do While Loops n n Example 1 (p 251) The following segment displays the numbers from 1 through 10. Private Sub cmd. Display_Click() Dim num As Integer 'Display the numbers from 1 to 10 num = 1 Do While num <= 10 pic. Numbers. Print num; num = num + 1 Loop End Sub Chapter 7 4

Repetition - Do While Loops n Example 2 - p. 251 Required user gives

Repetition - Do While Loops n Example 2 - p. 251 Required user gives password before accessing a file n Private Sub cmd. Display_Click() Dim pass. Word As String, info As String If UCase(txt. Name. Text) = "SECRET. TXT" Then pass. Word = "" Do While pass. Word <> "SHAZAM" pass. Word = Input. Box("What is the password? ") pass. Word = UCase(pass. Word) Loop End If Open App. Path & "" & txt. Name. Text For Input As #1 Input #1, info pic. Item. Cls pic. Item. Print info Close #1 End Sub Chapter 7 5

Repetition - Do Until Loops n n In examples 1 and 2 the condition

Repetition - Do Until Loops n n In examples 1 and 2 the condition was checked at the top of the loopthat is, before the statements were executed. Alternatively, the condition can be checked at the bottom of the loop when the statement Loop is reached. Do statement(s) Loop Until condition 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. If condition is false, then the entire process is repeatedly executed until condition is true. Chapter 7 6

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

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

Repetition - Do Until Loops n The following program is equivalent to Example 2,

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 (p 253) Private Sub cmd. Display_Click() Dim pass. Word As String, info As String If UCase(txt. Name. Text) = "SECRET. TXT" Then Do pass. Word = UCase(Input. Box("What is the password? ")) Loop Until pass. Word = "SHAZAM" End If Open App. Path & "" & txt. Name. Text For Input As #1 Input #1, info pic. Item. Cls pic. Item. Print info Close #1 End Sub Chapter 7 8

List of Data with Do loops n n n One of the main applications

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

EOF function n Suppose a file has been opened with reference number n, At

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 cmd. Display_Click() Dim nom As String, phone. Num As String pic. Numbers. Cls Open App. Path & "PHONE. TXT" For Input As #1 Do While Not EOF(1) Input #1, nom, phone. Num pic. Numbers. Print nom, phone. Num Loop Close #1 End Sub Chapter 7 10

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

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

Counter & Accumulator n n n A counter is a numeric variable that keeps

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

Flag A flag is variable that keeps track of whether a certain situation has

Flag 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. n Chapter 7 13

Nested Loop n n n Statements inside of Do loop can consist of another

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

Exercises n n Ex 6. 1 no. 3, 4, 7, 21, 23 Ex 6.

Exercises n n Ex 6. 1 no. 3, 4, 7, 21, 23 Ex 6. 2 no. 1, 5, 21 Chapter 7 15