Jump Control Statement In C: Understanding Use Of Break,Continue and Goto

Jump Control Statement In C 

Jump statements in C switch over it's control or jump in program unconditionally.
We can jump from the flow of execution at any point without any condition.

There are three types of jump statements : break, goto and continue. All are keywords in C.

Break Keyword - 

It breaks the execution flow and comes out. It works inside the "Selective(switch-case) and "Iterative" (looping) control statements.

It does not work with "Decision Making" (if-else) statements. If we use this keyword outside of the control statement it will give you an error.

#Program 1

Write a program to ask odd numbers from user. Stop asking numbers if user enters even number.



# include <stdio.h>


void main() {


int num;


for (int i=1; i<=10 ; i++) {


printf (" Please enter odd number ");

scanf ("%d" ,&num);


 if (num%2 == 0) {


    printf (" It is even number , break the loop ");

    break;  // It will break the loop

  }

 }

}


Note: Break is not depended on any condition we can use it within the loop and switch case unconditionally. 

As you can see in the above program, user entering odd numbers. Each number is checking by the "even number" condition, if condition is true then break keyword will jump from the loop. 

Continue Keyword -

Continue works inside the "Iterative"(looping) statements only. It does not work with "Selective" (switch-case) and "Decision Making" (if-else) statements.

Continue jumps from the current iteration flow and will go to next iteration condition checking.

#Program 2

Write a program to get sum of all 5's up to 20.



# include <stdio.h>


void main() {


int sum = 0;


for (int i=1; i<=20; i++ ) {


if (i%5 !=0) continue;


printf("%d\n",i);

sum = sum + i;


}

printf(" Sum of all 5's - %d", sum);

}

OUTPUT :

5

10

15

20

sum of all 5's - 50


As you can see in the above program, loop is counting 1 to 20, condition checking with each number if number is divided by 5 than add this number otherwise jump to the next iteration.

If condition does not satisfy then last two lines executes, you notice I did not put last lines into else block. Continue keyword will not execute last two lines as it will continue with next iteration (If condition satisfies).

Note: Continue is not depended on any condition we can use it within the loop unconditionally. 

Goto Keyword -

Goto is the another jumping statement, it transfers the program control from one
statement to another statement. 

The statement should be labeled properly. We can use goto sometimes but it is not recommended because it is not secure and reliable. 

Label will be defined by ":" operator and label name will follow same variable's naming convention rules.  Variable Naming Convention Rules.  

Now goto statement has been replaced by "Control Statements".

#Program 3

Write a program to ask student's name and marks in math,phy and chem.
Find the total marks and ask user's to continue, if user enters 1 then continue with same flow.  


# include <stdio.h>


void main() {


char name[10], choice;

int math, phy, chem, total;


start:


printf ("Please enter your name");

scanf("%s",name);


printf ("Please enter your marks");

scanf("%d%d%d",&math,&phy,&chem);


total =math +phy +chem;


printf("Your total marks %d", total);

printf("\n Do you want continue. Y/N");

scanf("%c",&choice);


if(choice == 'Y') goto start;


printf(" Thanks for using this application");


}

As you can see in the above program that if user enter Y then we are transferring control to start (name of the label) using goto statement. 

We can achieve same result by using "Iterative Control Statement" example: run the while loop until the choice is N.


Thanks for reading, please do share and comment your suggestions. 

No comments

Powered by Blogger.