Unlike other selection or branching statements that we have seen so far which branches based on a condition, the goto statement branches unconditionally. That is why the goto statement is also referred to as unconditional jump statement. There are two more unconditional branch statements in C. They are: break and continue. We have already seen the break statement in switch statement. But both break and continue are extensively used inside loops. So, we will discuss about these two unconditional branch statements later. By using the goto branch statement, we can either skip some instructions and jump forward in the program or jump back and again repeat a set of instructions. So there are two ways in which we can use the goto statement. They are:
- Forward jump
- Backward jump.
Syntax of forward jump and backward jump is as shown below:
As shown in the above syntax, if the label is after the goto statement, then it is known as forward jump and if the label is before the goto statement, it is known as backward jump.
Decision Making Looping Statements
While writing C programs, the programmer might want a set of instructions to be repeated again and again until some condition is satisfied. For this purpose, C provides decision making looping statements. The looping statements provided by C are:
All the looping statements in C essentially consist of two parts namely: control statement and body of the loop. The control statement decides when the loop will be stopped and the body of the loop contains the instructions that are to be repeated. Based on where the control statement is placed in the loop, the looping statements are categorized to two categories. They are:
- Entry controlled loops
- Exit controlled loops
Entry controlled loops
The looping statements in which the control statement is placed before the body of the loop are known as entry controlled loops. Ex: while and for loops. In entry controlled loops, the condition is checked first and if the value is true, then the body of the loop is executed. Otherwise the body is never executed. An entry controlled loop can be represented diagrammatically as shown below:
Exit controlled loops
The looping statements in which the control statement is placed after the body of the loop are known as exit controlled loops. Ex: do…while loop. In exit controlled loops, the body of the loop is executed once and then the condition is checked. If the value is true, the body of the loop is executed again. Otherwise, the execution of the loop stops. An exit controlled loop is represented diagrammatically as shown below:
Based on the nature of the loops, the loops can be categorized into two types namely:
- Definite loops (Ex: for)
- Indefinite loops (Ex: while and do…while)
Definite loops: If the programmer exactly knows how many times he/she is going to repeat the set of instructions (loop), such loops are known as definite loops.
Indefinite loops: If the programmer does not know exactly how many times he/she is going to repeat the set of instructions (loop), such loops are known as indefinite loops.
The variable used in the condition inside of a definite loop is known as a counter and such loops are also known as counter controlled loops. The variable used in the condition inside of a indefinite loop is known as sentinel and such loops are also known as sentinel loops.
Take your time to comment on this article.