Iteration Control Structure
Fourth Quarter
Involves loops or cycles
Loopsmeans that process may berepeated as long as certain conditionremains true or remains false.
Means multiple processing
Provides means of repeating apart of instruction withoutrewriting the part again and again.
Fundamental Concept
Body of the Loop Set ofstatements which isrepeated.
Loop Exit Condition:condition to be testedbefore each repetition.
Parts of the Loop
Start : the starting or thebeginning point of theloop.
One may start with firstor the last record in thefile.
Parts of the Loop: 3 S
Step  the manner onhow the records are tobe processed in thegiven file.
Proper sequencing isneeded (ascending ordescending).
Stop  : the ending point .It is normallyrepresented in a form ofa conditional expression,thus, a decision symbolis used in the flowchart
Is variable that defines thestarting, ending point and step oflooping statement.
It should be variable that willuniquely identify all the records inthe file.
It is representation of field inthe record. The shorter the value,the better  is the field
Control Variable
Draw flowchart that will read the grades in PT,CS and QS of  student. Compute the average ofthe student. Print the name and the computedaverage.
Input :
Process:
Output:
Will there be multiple processing? How many times theinputting, processing and outputting will be performed?WHY?
Problem No. 1
Draw flowchart that will read the grades in PT,CS and QS of all the students in the class of II -____. Compute the average of all the students.Print the names and the computed averages.
Input :
Process:
Output:
Will there be multiple processing? How many times theinputting, processing and outputting will be performed?WHY?
Problem No. 2
Are used to literally count the number oftimes a portion of the flowchart is traced.
Need to be initialized / prepared prior to itsuse or application.
The operation involved is “addition”
The increment value is a constant
Example:   C = C + 1
 
Increment Value
               Current Value
       New Value
Counters
numerical value that collects the result of arepeated mathematical operation.
Used to keep running total of an item theoperation involved is “addition”.
Need to be initialized prepared prior to itsuse or application.
The incremental value is “variable”(subjected to change)
Example:   N
                    Increment Value
          Current Value
New Value
Accumulators
Executes a group of instructionsrepeatedly
Has 3 Structures:
For …….  Next
Do …….. Loop  : has three types:
Do While ….  Loop
Do  Until ….  Loop
Do  ……  Loop  while
While ….. Wend
Looping Statements
Executes section of  the code aspecified number of times.
Begins with the “for” statement andends with the “next” statement.
Can only be used if the programmerknows the number of times the loopmust be performed prior to itsexecution.
For …. Next   Statement
For <counter > = <Start> to<Stop>  Step  <Step>
     <Statements>
Next  <counter>
For … Next Syntax 1(no skipping)
Dim UserName as string
Dim times as integer
Username =  “Paul”
Lbloutput.text = “ ”
For times = 1 to 10 step 1
Lbloutput.text =lbloutput.text & chr(13) &Username
Next  times
Example:
Dim UserName as string
Dim times as integer
Username =  “Paul”
Lbloutput.text = “ ”
For times = 1 to 10
Lbloutput.text =lbloutput.text & chr(13)&  Username
Next
Example:
For <counter > = <Start> to<Stop>   Step <Step>
     <Statements>
Next  <counter>
For … Next Syntax 2(with skipping/decrement)
Dim counter as integer
Lbloutput.text = “”
For counter = 10 to 1 step -1
   lbloutput.text =lbloutput.text & chr(13) &counter
Next
Example 1
Dim counter as integer
Lbloutput.text = “”
For counter = 9 to 1 step -2
   lbloutput.text =lbloutput.text & chr(13) &counter
Next
Example 2
Most common statement among thedo ..loop statements
Test condition appears at the TOP ofthe loop
As long as the test condition is TRUE, theblock of code in the body of the loop willbe continuously executed
When the test condition becomes FALSE,the loop terminates.
Condition before iteration
Do while ….. Loop
Do while  <condition>
<statement>
<statement>
Loop
Syntax: Do while …. Loop
Private Sub Command1_Click( )
Dim N As Integer
N = 0
Do While N < 10
N = N + 1
Print N
Loop
End Sub
Example:
Example:
Private Sub Command1_Click( )
Dim N As Integer
N = 0
Do While N < 10
N = N + 1
Print N
Loop
End Sub
Loop Statement
The test condition appearsat the bottom of the loop.
When the test conditionstays TRUE, the loop stillexecutes until it becomesfalse
Condition after iteration
Do ….. Loop While
Do
<  statement  >
<  statement  >
<  statement  >
Loop while < condition >
Do … Loop While Syntax
Private Sub Command1_Click( )
Dim N As Integer
N = 0
Do
N = N + 1
Print N
Loop While N < 10
End Sub
Example:
Test condition also appearsat the TOP of the LOOP.
Executes the block ofstatements as long as thetest condition is FALSE
Do  Until …. Loop
Do until  <  condition >
<  statement  >
<  statement  >
Loop
Do  Until … Loop Syntax
Private Sub Command1_Click( )
Dim N As Integer
N = 0
Do Until N >= 10
N = N + 1
Print N
Loop
End Sub
Example:
DO WHILE     /     DO UNTILDO WHILE     /     DO UNTIL
Display numbers fromDisplay numbers from
to 10to 10
Do While 10Do While 10
11
Print NPrint N
Body of Loop will be executedas long as the condition isTRUE.Body of Loop will be executedas long as the condition isTRUE.
Display numbers fromDisplay numbers from
to 10to 10
Do Until >= 10Do Until >= 10
11
Print NPrint N
Body of Loop will be executedas long as the condition isFALSE.Body of Loop will be executedas long as the condition isFALSE.
Hands – On Session
a.Make a program that will displaythis output on the form:
b.Make a program that will display“Patience is a Virtue” 5 times.
Activity 1 & 2
Filenames:
Activity No.1
4Act12CN
Activity No.2
4Act22CN