download

Matakuliah
Tahun
Versi
: D0524 / Algoritma dan Pemrograman Komputer
: 2005
:
Pertemuan 06
Repetition
1
Learning Outcomes
Pada akhir pertemuan ini, diharapkan mahasiswa
akan mampu :
• Mendemonstrasikan penggunaan perintah
pengulangan
2
Outline Materi
• The Do … Loop Structure
• The For…Next Loop Structure
• Nested Loops
3
The Do … Loop Structure
• Syntax and Action of the Do…Loop
Do
statementblock
Loop
• Run Time: The Effect of the Do…Loop Structure
– Statements in a loop’s interior are executed one at a
time.
– The Do marks the top of the loop.
– The Loop sends the execution back to the top.
4
The Do … Loop Structure
• Do While…Loop
Do While condition
statementblock
Loop
• Run Time: The Effect of the Do
While…Loop Structure
– Loop continues to execute “while the
condition is true.”
– Loop is exited when the condition is false.
5
The Do … Loop Structure
• Coding the Loop Body
– One or more statements in the loop body must
eventually cause the condition to become false.
– Order of statements inside loop affects the result of
the loop.
– Loop may terminate immediately.
– Loop can be viewed as a meta statement.
6
The Do … Loop Structure
• Do Loop…While
– Works like the Do While…Loop.
– Termination at the bottom of the loop.
– Syntax:
Do
statementblock
Loop While condition
7
The Do … Loop Structure
• Do Until…Loop and Do…Loop Until
– Do Until…Loop
• Termination condition at the top of the loop.
• Same syntax and action as the Do While…Loop.
– Except:
» Difference in keywords.
» Loop statements inside the Do Until…Loop are executed when the
conditions are False.
– Do…Loop Until
• Termination condition at the bottom of the loop.
• Exit when the condition is True.
• Initialization and Termination
• Loops may start or end at the wrong place.
– The initialization statement may be off.
– The condition operator may need to be changed.
– The order of statements within the loop may need to be changed.
8
The For…Next Loop Structure
• Syntax and Action of For…Next
For counter = start To end
statementblock
Next
– Run Time: The Effect of the For…Next Structure
• Counter variable and start and end expressions must be
evaluated to control the number of times the loop will be
processed.
• For…Next versus Do…Loop Variations
– Use a For…Next loop when the number of loop
iterations can be determined.
– Use a Do…Loop variety only when the number of
iterations depends on the results of statements inside
the loop.
9
The For…Next Loop Structure
• The Step Amount
– Counter variable may be incremented by a step
amount, such 3 instead of 1.
– Syntax:
For counter = start To end Step increment
statementblock
Next
• Avoiding For…Next Errors
– Do not place statements inside the loop that change
start, end, and increment expressions.
– Do not change the counter variable inside the loop.
10
Nested Loops
• Loops may be embedded inside of other loops.
• Nested Do While…Loops
Do While condition1
statementblock1a
Do While condition2
statementblock2
Loop
statementblock1b
Loop
• Nested For…Next Loops
For counter1 = start1 To end1
statementblock1a
For counter2 = start2 To end2
statementblock2
Next
statementblock1b
Next
11