Download presentation
Presentation is loading. Please wait.
1
1 Pertemuan 06 Repetition Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
2
2 Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mendemonstrasikan penggunaan perintah pengulangan
3
3 Outline Materi The Do … Loop Structure The For…Next Loop Structure Nested Loops
4
4 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.
5
5 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.
6
6 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.
7
7 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
8
8 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.
9
9 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.
10
10 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.
11
11 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
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.