Virtually all programming languages have a facility to allow a section of code to be repeated (iterated or looped). There are several variations to iterative or looping constructs. For the most part, they fall into two categories: FOR loops and WHILE/DO loops.
FOR loops
Simple FOR loops iterate a specific number of times based on counting up (or down) on an integer variable. For example, if we want to do something 10 times, we can make a loop that counts up from 1 to 10 and put our work (what we want done) inside the loop.
Generally the three main parts that need to be written for a FOR loop are the initialization (the initial or starting value), the condition (the condition under which the loop will stop) and the increment (or decrement).
The other main part of the syntax is some indication of the start of the loop section and the end of the loop section. In many languages these are indicated by opening and closing curly brackets { and }.
The other main part of the syntax is some indication of the start of the loop section and the end of the loop section. In many languages these are indicated by opening and closing curly brackets { and }.
Below are some examples for different languages:
Programming Language | FOR loop |
---|---|
"C", C++, Java, Perl | for (initialize; condition; increment) { statements } |
Pascal | for variable := low_integer to high_integer do begin statements end; |
Oracle PL/SQL | FOR variable IN low_integer .. high_integer LOOP statements END LOOP; |
Basic | For variable = low_integer To high_integer statements Next variable |
In each of the following examples, the code will output "Hello World" 10 times.
Programming Language | Example |
---|---|
"C", C++ | for (i = 1; i <= 10; i = i+1) { printf("Hello World\n"); } |
Java | for (i = 1; i <= 10; i = i+1) { System.out.println ("Hello World"); } |
Pascal | for i := 1 to 10 do begin writeln('Hello World'); end; |
Oracle PL/SQL | FOR i IN 1..10 LOOP DBMS_OUTPUT.PUT_LINE('Hello World'); END LOOP; |
Basic | For i = 1 To 10 Print "Hello World" Next i |
WHILE and DO loops
Like FOR loops, the WHILE and DO loops are also used to repeat a section of code for some number of times. However while FOR loops basically specify some count, WHILE and DO loops repeat while a condition is true. The condition is a boolean expression that we can evaluate as either "true" or "false". As soon as the conditionevaluates to "false" the loop ends.
The main difference between a WHILE loop and a DO loop is where the condition is checked. WHILE loops check the condition first, and then run the statements if true. DO loops run the statements first, then check if thecondition is true.
Below are the general syntax
Programming Language | WHILE loop | DO loop |
---|---|---|
"C", C++, Java, Perl | while (condition) { statements } | do { statements } while (condition) |
Pascal | while (condition) do begin statements end; | Repeat statements until (condition); |
Oracle PL/SQL | WHILE condition LOOP statements END LOOP; | LOOP statements EXIT WHEN condition ; END LOOP; |
Basic | While condition statements Wend | Do statements Loop While condition |
WHILE loops can be set up to emulate what a FOR loop is doing. For example, the following "C" or C++ code prints "Hello World" 10 times:
Programming Language | FOR Loop | WHILE Loop | |
---|---|---|---|
"C", C++ | for (i = 1; i <= 10; i = i+1) { printf("Hello World\n"); } | i = 1; while (i <= 10) { printf("Hello World\n"); i = i + 1; } |
Materials to be added