A computer program involves three basic steps; - accepting inputs, processing them and finally generating output according to the processed result. In these basic three steps inputs may come from any input device like key stroke, mouse click or may come from the output of another program. Similarly the output may be displayed over an output device like computer monitor, printer etc. or it may initiate another program to start. In the above three steps the processing unit is the core area of a program. Here a program takes the decision that what output it will generate against a given set of inputs. In this passage a program uses mathematical processing and conditional execution of code according to the logic set by the programmer. Branches and loops are two basic forms of conditional statement.
When program flows in either direction between two parallel paths then the paths are known as branch of a program. "IF ELSE" statement is the most common form of branch statement used by all high level languages including the script languages. Here is a very simple example of IF ELSE statement. Say, a program displays the success or failure message to the students appeared in examination. When a student enters his or her role no. program searches the database for the aggregate of marks obtained by that student. If the marks obtained by that student greater than pass marks then a success message are sent to the screen, else a failure message will be displayed, i.e.
IF marks ≥ pass marks THEN display success message. ELSE display failure message
A program loop is a set of code which will be executed repeatedly until a given condition attached to the loop has been satisfied. Thus a loop is another form of conditional statement driven by a conditional logic. Two very common form of loop statements are "DO WHILE" loop and "FOR EACH" loop. These two methods of loop are universally used by all high level languages. Here are few examples to explain how these loops are used and their usefulness.
DO nothing WHILE Enter key not pressed
In this example the program will fall into an indefinite wait loop until user presses the Enter or Return key.
FOR EACH loop: This statement is usually used in connection to an array; - the code block is repeated for each elements of an array.
FOR EACH students AS student PRINT result of student.
The key feature of a computer is its ability to take logical decision against the inputs supplied to it. This logical decision is actually implemented by conditional statements, - branches and loops which allow a program to flow in a certain path according to the condition given to it. So without branches and loop statements a program loses its intelligence and it becomes a calculator.