C Control statements (contd. 2)

Case Control Structures in C

There are some Case Control Structures in C language. Case Control Structures are used to transfer the control of the compiler (on meeting a specific condition) to a different location in the program. Although, switch statement can also be called a Case Control Statement because switch statement can also be used for checking case conditions and transferring the control out of the statement out of the switch block. 

In Case Control Structures, We will study the following statements :

1. break statement
2. continue statement
3. goto statement

Let's look each of them in detail :

1. break statement

The break statement is used inside loops and switch statements. Sometimes it becomes necessary to come out of the loop even before the loop condition becomes false. In such a situation, break statement is used to terminate the loop or take exit from loop on meeting a certain condition. This statement causes an immediate access from that loop in which this statement appears it can be written as -
break ;
When break statement is encountered loop is terminated and the control is transferred to the statement immediately after the loop. The break statement is generally return along with a condition. 

Flowchart of break statement
break statement flowchart

Let's understand break statement with the help of an example -


#include <stdio.h>
int main(){
    
    for (int i=1; i<=5; i++)
    /* The loop should repeat for 5 times as per the given condition */
    {
        if (i==3)
        {
            printf("The loop will be terminated without the condition being false");
            break;
        }
        printf("This statement should repeat 5 times\n");
    }
}

2. continue statement

The continue statement is used to go to the next iteration or repetition of the loop after skipping some statements of the loop. For skipping some statements on meeting a certain condition continue statement is used as given below:
continue ;
This is too used with a condition. When continue statement is encountered all the remaining statements after the continue statement will not be executed and loop continues with the next iteration or repetition.

Let's understand the working of continue statement with the help of an example:
#include<stdio.h>
int main(){    
    for(int i=1; i<=5; i++)
    {
        if (i==3)
        {
            printf("The repetition number will not shown\n");
            continue;
        }
        printf("This is repetition number %d\n",i);
    }

}

In the above program, the continue statement will be executed when "i is equals to 3" and this continue statement will take the loop for next iteration or repetition, without executing the remaining instructions.
Flowchart of continue statementcontinue statement flowchart

3. goto statement

The goto statement is different from the other two statements discussed above. It is an unconditional statement, which can transfer the flow of control to anywhere in the program without meeting any specific condition.

goto statement takes the following form :
goto label ;
// statements 
......................

label :
// statements 
......................
Here, label is any valid C identifier and it is followed by a colon (:).
During the execution of the program, when goto statement is encountered, the control is transferred to the label of the goto statement without any condition.

Let's understand the goto statement with the help of an example :
#include<stdio.h>
int main(){
    goto first;
    
    last :
    printf("\nThis will be printed last\n");
    goto end;
    
    second :
    printf("\nThis will be printed second");
    goto last;
    
    first :
    printf("This will be printed first");
    goto second;
    
    end :
    printf("The program ends here");
    
}

In the above code, 'first', 'second', 'last' and 'end' are the labels used with goto statements.

The label can be placed anywhere in the C Program.
The control can be transferred only within a function using a goto statement. (Functions will be discussed on the next page of the site)
Note : A goto label cannot be placed at the end of a program. A program cannot end with it's last statement as goto label. 

The use of goto statement should be avoided in writing a professional c program, as it makes the code hard to understand and debug. 

<<Previous

Post a Comment