Jump statements are the statements when encountered the control shifts from one part to another part of the program. The different types of jump statement are:
- Break
- Continue
- Goto
Let us discuss them one by one.
Break Statement:
A break statement is a statement that terminates the block when called and continues with the rest of the program. The break statement is initialised with the break keyword. The break statement is most frequently used in switch case or within is loop else it results in a compile-time error.
Example of Break Statement:
#include<stdio.h> int main() { int a=1; while(a!=10) { if(a==5) { printf("\nBreak encountered"); break; } else printf("\nBreak not encountered"); a++; } printf("\nBlock encountered after break statement"); return 0; }
Output:
Continue Statement:
The continue statement makes the control move to the next iteration without actually terminating the block. It is also used in loops and is initialized with the continue keyword.
Example Of Continue Statement:
#include<stdio.h> int main() { int a=1; while(a!=10) { a++; if(a==5) { printf("\nContinue encountered"); continue; } else printf("\ncontinue not encountered"); } return 0; }
Output Of Continue Statement:
Goto Statement:
The goto is a decision-making statement, when encountered it points to the statement which is initialized in the label: statement. It is depicted with the goto keyword.
Syntax Of Goto Statement:
goto label; ... .. ...... .. ... label: statement;
goto Statement Example:
#include<stdio.h> int main() { int a=30,b=20; if(a>b) { goto label; } label: printf("Goto executed"); return 0; }
Output:
Here we come to the end of the topic on jump statements in C, hope we have cleared all your doubts.
Please write comments or WhatsApp if you find anything incorrect, or you want to share more information about the topic discussed above.