Home » » LOOPING


Written By Unknown on Sabtu, 21 Januari 2012 | 20.57

Lab RPL .
In Most Programs , a Loop can be used to do something an x number of times. For the Purpose of code saving it certainly helps and it can also prove quite useful.
A Loop is Used for repetition and constain some decision making. The decision you need to make is what condition(s) will continue to make this loop run.
There are a couple of types of loops. The firstis a While Loop. The other is a For Loop . This means that for a certain number of times, do something :
for ( loop counter ; condition(s); counter change)
      // Code For the For
 Let's Break down what each section of the for Loop mean. Note that there are 3 sections, each of whice ( except the last ) is separated by a semicolon.

First, the loop counter. The Loop counter is used to keep track of how many times you want the loop run . It is always a numeric value such as an int, double or float.

Second, the condition(s) of the loop. This is where the logic comes into play. This is Logic generally done on the loop counter. Say you wanted the loop to run 30 times. The counter would have logic that kept track of that (counter < 30 ).

Finally, the counter changes. Without this , you would have AN INFINITE LOOP ! Those are quite a pain in the neck as the loop will Never End . Generallly, you will either ++ or -- a loop but not always, sometimes , a program calls for a loop to have a change by 2 , 3 , 4 etc. Just use the correct operation.

0 komentar:

Posting Komentar