Unconditional Statement In C.

 Unconditional Statement In C.

unconditional statement in c

Hello Programmers,

In few last articles, we studied conditional statement in c but today we will discuss unconditional statement which is rarely used by any programmers.

Programmers always try to avoid unconditional statement in their program because the unconditional statement makes the program more complex and increase redundancy in the program. 

Unconditional statement transfer control unconditionally i.e. control is transferred without any condition. In C language there is various unconditional control transfer statement supported by C.

1) goto Statement

2) break Statement

3) continue Statement

1) goto Statement:-  

The "goto" statement is used to transfer the control unconditionally from one point to another point in the program. Although the programmer tries to avoid using the "goto" statement in their program there may be occasions when the use of goto is necessary. 

Some important point which is necessary to using goto statement:

1) The "goto" statement requires a label in order to identify the place where the branch is to be made.

2) You can use any valid variable name as label and must be followed by a colon(:). 

3) The label is placed immediately before the statement where the control is to be transferred. 

4) The label can be anywhere in the program either before or after the goto label statement. 

5) If the "label:" is before the statement "goto label;" the jump is known as a backward jump.

6) If the "label:" is placed after the "goto label;" the jump is known as a "forward jump".

forward jump in goto statement
Forward Jump

Backward jump in goto statement
Backwards Jump

2) break Statement:

A break statement is used to terminate the execution of the loop and the control is transferred to the next statement immediately following the loop. Break statement can be used within a for, while, do-while loop or also used withing switch statement. 

The break statement is written simply as a break;

break statement

3) continue statement:

The continue statement is used within a loop body. The continue statement takes control at the beginning of the loop. When applied with do-while or while loops, the condition testing takes place, with for loop next iteration takes place.

Whenever you used continue statement in the loop must ensure that loop does not terminate when a continue statement is encountered. Instead, the remaining loop statement is skipped and the computation proceeds directly to the next pass through the loop. 

It is simply written as "continue". The continue statement tells the compiler "skip the following statement and continue with the next iteration."

continue statement pic

Why Avoid Unconditional Transfer In Program?

Programmers always avoid unconditional transfer condition in a program it makes the program unstructured. Some point considers why we should avoid unconditional transfer in the program.

1) Debugging very difficult.

2) Poor way of coding.

3) Program unreliable and unreadable.

4) The logic of the program complex and tangled.

Thus a programmer who wants to write a structured program should avoid unconditional transfer statements in the programs.

Comments :

Post a Comment