Jump Statements
What are jump statements?
Imagine you are watching a movie. Normally, the movie plays scene by scene. But sometimes you may want to stop watching, skip a scene, jump to a particular scene, or finish the movie.
C programming also provides statements that change the normal flow of execution. These are called jump statements.
C has four commonly taught jump statements:
1. break — Stop
Immediately exits the nearest enclosing loop or switch statement.
Think: “Stop here!”
2. continue — Skip
Skips the remaining statements in the current loop iteration and proceeds to the next iteration.
Think: “Skip this turn!”
3. goto — Jump to a label
Transfers control to a labelled statement in the same function.
Think: “Go directly there!”
4. return — Leave a function
Ends the current function and can send a value back to the calling function.
Think: “My task is complete!”
break statement
The break statement immediately terminates the nearest enclosing loop or switch. Execution continues with the first statement after it.
Syntax
break;
Example: Stop counting at 4
#include <stdio.h>
int main(void)
{
int i;
for (i = 1; i <= 6; i++)
{
if (i == 4)
{
break;
}
printf("%d ", i);
}
return 0;
}
1 2 3
How does it work?
Important points
breakcan be used infor,while,do-while, andswitch.- It exits only the nearest enclosing loop or
switch. - It does not automatically terminate the entire program.
continue statement
The continue statement skips the remaining statements in the current loop iteration and moves to the next iteration.
Syntax
continue;
Example: Print numbers from 1 to 6, except 4
#include <stdio.h>
int main(void)
{
int i;
for (i = 1; i <= 6; i++)
{
if (i == 4)
{
continue;
}
printf("%d ", i);
}
return 0;
}
1 2 3 5 6
When i becomes 4, continue skips printf(). The loop then proceeds to the next iteration.
Difference between break and continue
| break | continue | |
|---|---|---|
| Action | Exits the loop | Skips one iteration |
| Numbers printed (at 4) | 1 2 3 (Stop) | 1 2 3 |
for loop, continue moves to the update expression and then the condition check. In a while loop, the condition is checked again. Ensure your loop variable still progresses so you do not create an infinite loop.goto statement
The goto statement transfers control directly to a labelled statement within the same function.
Syntax
goto label;
/* Other statements */
label:
statement;
Here, label is an identifier followed by a colon.
Example
#include <stdio.h>
int main(void)
{
printf("Start\n");
goto finish;
printf("This line is skipped.\n");
finish:
printf("Program reached finish.\n");
return 0;
}
Start
Program reached finish.
Explanation
Startis printed.goto finish;transfers control to the labelfinish:.- The intervening
printf()statement is skipped. - The final message is printed.
Advantages and disadvantages
- It can be useful in some carefully designed error-handling or cleanup paths.
- Excessive use makes programs difficult to read, understand, and debug.
- Prefer loops, functions, and structured control statements whenever possible.
return statement
Meaning
The return statement terminates the execution of the current function and transfers control back to the calling function.
It is commonly used in two forms:
return; /* No value: void function */
return value; /* Return a value */
Example: Return the square of a number
#include <stdio.h>
int square(int n)
{
return n * n;
}
int main(void)
{
int result = square(5);
printf("Square = %d\n", result);
return 0;
}
Square = 25
How it works
square(5)calls the function withn = 5.return n * n;calculates and sends back 25.- The value is stored in
result. main()prints the result and returns 0.
void must not return a value. A non-void function should return an appropriate value when its result is required. Reaching the closing brace of main() is equivalent to returning 0 in C.Comparison table
| Feature | break | continue | goto | return |
|---|---|---|---|---|
| Main purpose | Exit loop or switch | Skip current loop iteration | Jump to a label | Exit current function |
| Used with loops | Yes | Yes | Can transfer control around code | Can exit a function containing a loop |
| Used in switch | Yes | No, not to skip a case | Possible, but use carefully | Yes, exits the function |
| Returns a value | No | No | No | Can return a value |
| Good memory word | Stop | Skip | Jump | Finish |
Common mistakes to avoid
- Confusing
breakwithcontinue. - Using
continueoutside a loop. - Forgetting to update a loop variable when using
continuein awhileloop. - Using
gotowithout defining the target label in the same function. - Returning a value that is inconsistent with the function's declared return type.
- Assuming
breakexits all nested loops; it exits only the nearest enclosing loop orswitch.
Important questions for B.Tech exams
Short-answer questions
- Define jump statements in C and list their types.
- Differentiate between
breakandcontinue. - Explain the syntax and purpose of
goto. - What is the role of
returnin a function?
Long-answer and programming questions
- Explain the four jump statements with suitable examples.
- Write a C program to print numbers from 1 to 10, skipping 5 using
continue. - Write a C program that stops printing numbers when the counter reaches 6 using
break. - Write a function that returns the larger of two integers using
return.