Procedural Programming Loops

2k views Asked by At

I want to clarify a few things about loops in procedural programming. So, I'm doing a unit on procedural programming in my IT course at sixth form and we need to write about three different types of loops, these are "Fixed for loop", "Pre-check while loop" and "Post-check while loop". I think I have an idea of what these are however, I cannot find sufficient evidence to back up what I think I know.

Fixed for loop: I think the fixed for loop is what I call the "generic" for loop, I think that this is where you define the counter variable with the for loop e.g.:

for (int i=0; i <= 6; i++ ){
     print("Iteration " + i);
}

Pre-defined while loop: I think this is a while loop where the counter is defined before the loop itself e.g.:

int counter = 0;
while ( i <= 10){
    print ( "Iteration " + i);
    i ++;
}

However, if the above is true then I don't understand what a post-defined while loop would be. If anyone would be kind enough to clarify what each of these loops are, I would much appreciate it.

My question in case it's not clear: What do the following loops do and how are they defined: "Fixed for loop", "Pre-defined while" and "Post-defined while"

2

There are 2 answers

1
DoctorMick On BEST ANSWER

You can use a do...while loop which is basically the same as a while but the condition comes at the end rather than at the start.

int i = 0;
do {
    print ( "Iteration " + i);
    i ++;
} while ( i <= 10)

It's very useful if you want to perform a block of code at least once before checking the condition.

2
Roy Lin On

I could be wrong, but I believe there is a do-while construct. Using Java as example, you have

do {
     statement(s)
} while (expression);

Where the condition is evaluated "post", or in other words, "after" the first iteration.