Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
Lecture 7 Loops

Lecture 7 Loops

Ratings: (0)|Views: 31 |Likes:
Published by leth_alix

More info:

Published by: leth_alix on Sep 10, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

09/10/2010

pdf

text

original

 
Republic of the Philippines
B A T A N G A S S T A T E U N I V E R S I T Y Apolinario R. Apacible School of Fisheries Nasugbu CampusNasugbu, BatangasCOLLEGE OF ENGINEERING AND COMPUTING SCIENCES
Computer Programming IILecture 7LOOP STRUCTURES
Loop structures allow you to execute one or more lines of code repetitively.Many tasks consist of trivial operations that must be repeated over and over again,and looping structures are an important part of any programming language. VisualBasic supports the following loop structures:
For…Next
Do…Loop
While…End While
For…Next
 The For…Next loop is one of the oldest loop structures in programminglanguages. Unlike the other two loops, the For…Next loop requires that you knowhow many times the statements in the loop will be executed. The For…Next loopuses a variable (it’s called the loop’s
counter 
) that increases or decreases in valueduring each repetition of the loop. TheFor…Next loop has the following syntax:For counter = start To end [Step increment]statementsNext [counter] The keywords in the square brackets are optional. The arguments
counter 
,
start 
,
end 
,
and
increment 
 
are all numeric. The loop is executed as many times asrequired for the
counter 
to reach (or exceed) the
end 
value.In executing a For…Next loop, Visual Basic completes the following steps:
1.
Sets
counter 
equal to
start 
2.
 Tests to see if 
counter 
is greater than
end 
. If so, it exits the loop. If 
increment 
is negative, Visual Basic tests to see if 
counter 
is less than
end 
. If it is, it exitsthe loop.3.Executes the statements in the block
4.
Increments
counter 
by the amount specified with the
increment 
argument. If the
increment 
argument isn’t specified,
counter 
is incremented by 1.5.Repeats the statementsSample Program:
 
Dim intCount As IntegerDim a As StringFor intCount = 0 To 5a = a & vbCrLf + ("Counter is currently: " + CStr(intCount))TextBox1.Text = aNext intCount
Prepared by:Djoanna Marie T. VasquezInstructorPage 1
 
 The single most important thing to keep in mind when working with For…Next loops is that the loop’s
counter 
is set at the beginning of the loop. Changingthe value of the
end 
variable in the loop’s body won’t have any effect. For example,the following loop will be executed 10 times, not 100 times:endValue = 10For i = 0 To endValueendValue = 100{ more statements }Next i You can, however, adjust the value of the
counter 
from within the loop. Thefollowing is an example of an endless (or infinite) loop:For i = 0 To 10Console.WriteLine(i)i = i - 1Next i This loop never ends because the loop’s
counter,
in effect, is never increased. The
increment 
argument can be either positive or negative. If 
start 
is greater than
end 
, the value of increment must be negative. If not, the loop’s body won’t beexecuted, not even once. Finally, the
counter 
variable need not be listed after theNext statement, but it makes the code easier to read, especially when For…Nextloops are nested within each other.
Do…Loop
 The Do…Loop executes a block of statements for as long as a condition is True. Visual Basic evaluates an expression, and if it’s True, the statements areexecuted. When the end of block is reached, the expression is evaluated again and,if it’s True, the statements are repeated. If the expression is False, the programcontinues and the statement following the loop is executed. There are two variations of the Do…Loop statement; both use the same basicmodel. A loop can be executed either while the condition is True or until thecondition becomes True. These two variations use the keywords While and Until tospecify how long the statements are executed. To execute a block of statementswhile a condition is True, use the following syntax:Do While conditionstatement-blockLoop To execute a block of statements until the condition becomes True, use thefollowing syntax:Do Until conditionstatement-blockLoopWhen Visual Basic executes these loops, it first evaluates
condition
. If 
condition
is False, a Do…While loop is skipped (the statements aren’t evenexecuted once) but a Do…Until loop is executed. When the Loop statement isreached, Visual Basic evaluates the expression again and repeats the statementblock of the Do…While loop if the expression is True, or repeats the statements of the Do…Until loop if the expression is False.In short, the Do While loop is executed when the condition is True, and the DoUntil loop is executed when the condition is False. The Do…Loop can execute anynumber of times as long as
condition
is True or False, as appropriate (zero ornonzero if the condition evaluates to a number). Moreover, the number of iterationsneed not be known before the loops starts. In fact, the statements may neverexecute if 
condition
is initially False for While or True for Until.
Prepared by:Djoanna Marie T. VasquezInstructorPage 2
 
Sample Program: The following code samples demonstrate both uses of the
While
keyword.Note that the second loop will never execute because the first loop has alreadyincreased the value of 
intTotal
to 10.
Dim intTotal As Integer
 
DointTotal = intTotal + 1MessageBox.Show("intTotal = " + CStr(intTotal))Loop While intTotal < 10Do While intTotal < 10intTotal = intTotal + 1MessageBox.Show("intTotal = " + CStr(intTotal))Loop
 The following code example shows the
Do ... Until ... Loop
in action:
Dim intTotal As IntegerDointTotal = intTotal + 1MessageBox.Show("intTotal = " + CStr(intTotal))Loop Until intTotal > 10Do Until intTotal > 10intTotal = intTotal + 1MessageBox.Show("intTotal = " + CStr(intTotal))Loop
While…End While
 The While…End While loop executes a block of statements as long as acondition is True. The While loop has the following syntax:While conditionstatement-blockEnd While
VB VB6
VB.NET6B.NET
 The End While statement replaces the Wend statement of VB6.If 
condition
is True, all statements are executed and, when the End Whilestatement is reached, control is returned to the While statement, which evaluates
condition
again. If 
condition
is still True, the process is repeated. If 
condition
isFalse, the program resumes with the statement following End While. The loop in Listing 3.15 prompts the user for numeric data. The user can typea negative value to indicate that all values are entered.
Listing 3.15: Reading an Unknown Number of Values
Dim number, total As Doublenumber = 0While number => 0total = total + numbernumber = InputBox(“Please enter another value”)End While
Nested Control Structures
 You can place, or
nest,
control structures inside other control structures (suchas an If…Then block within a For…Next loop). Control structures in Visual Basic canbe nested in as many levels as you want. It’s common practice to indent the bodiesof nested decision and loop structures to make the program easier to read.
Prepared by:Djoanna Marie T. VasquezInstructorPage 3

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->