There are very specific differences between the three types of iteration (or repetition) which are important to understand when selecting which one to use. The table below outlines these differences.

Repetition Type

Counter increment

Initialization

Condition to loop

For

Automatic

Automatic

Continues until the counter reaches the value specified in the TO clause

Repeat- Until

Manual

Manual

Will only loop if the condition is false

While

Manual

Manual

Will only loop if the condition is true

 

The for loop above is very simple and allows us to pick up the three main parts of repetition. These are-

for (<start value> ; <condition>; <increment>)

Below is the steps you must follow to perform the conversion -

  1. Swap the start and end of the loop for the new keywords
  2. Change the start value
  3. Add/remove increment code
  4. Alter the condition

Consider this for loop -

  1. for(int a=0; a<5; a = a + 1){
  2. // do stuff
  3. }

Step one - swap keywords

  1. while(int a=0; a<5; a = a + 1){
  2. // do stuff
  3. }

Step two - change the start value

  1. int a=0;
  2. while(a<5; a = a + 1){
  3. // do stuff
  4. }

Step three - increment

  1. int a=0;
  2. while(a<5){
  3. // do stuff
  4. a = a + 1;
  5. }

Step four - (not needed for java)

next nested for loops >>

Note - In psudeocode we need to do some more work. The image below shows this

 

 
Basics
Menu
Search