- C Programming
- C Tutorial
- C Basic Syntax
- C Data Types
- C if...else Statement
- C switch Statement
- C for Loop
- C while Loop
- C do...while Loop
- C Jump Statements
- C Arrays
- C Strings
- C Pointers
- C Functions
- C Library Functions
- C Recursion
- C Variable Scope
- C Structures
- C Linked List
- C Stacks
- C Queues
- C Binary Tree
- C Header Files
- C File I/O
- C Programming Examples
- C Programming Examples
Jump statements in C with examples | The break, continue, and goto statements in C
In this post, you will learn about the first three of the following four statements, which are also called "jump statements" in the C programming language:
- The break statement
- The continue statement
- The goto statement
- The return statement
The first three jump statements are covered in this post, while the final one, the "return statement," is covered in another post. The "return" statement is described in the post that describes the "C functions." However, I have included the link for your convenience.
The break statement in C
When we need to either terminate a case in the "switch" statement or force the termination of a loop, we use the "break" statement to bypass the normal loop conditional test.
Simply put, the "break" statement ends the execution of the following enclosing statement, such as "for," "while," "do...while," or "switch." For example:
#include <stdio.h> int main() { for(int i=1; i<=10; i++) { if(i == 5) break; printf("%d \n", i); } return 0; }
The following is the program's output:
1 2 3 4
When the value of "i" is equal to 5, the condition "5 == 5" (on putting the value of "i") evaluates to true, and the program flow executes the body of the "if", which is:
break;
which terminate the further execution of the nearest loop or switch. Because in this case, the nearest one is a "for" loop, the further execution of the "for" loop will be terminated.
The continue statement in C
Unlike the "break" statement, the "continue" statement moves program control to the next iteration of the nearest loop, skipping the execution of the remaining statements in the same block following the "continue" statement.
Consider the following program as an illustration of the "continue" statement in the C programming language:
#include <stdio.h> int main() { for(int i=1; i<=10; i++) { if(i == 5) continue; printf("%d \n", i); } return 0; }
This time, the output produced is:
1 2 3 4 6 7 8 9 10
You can see that only the "5" is not printed. Because when the value of "i" becomes equal to "i," then the condition evaluates to true, and the "continue" statement is executed, which immediately transfers the control to the next iteration or to the update part of the loop, skipping the statement to be executed following the "continue" statement.
The goto statement in C
Unlike the "break" and "continue" statements, the "goto" statement is used to move program control to a specific location within the program.
The "goto" statement is most commonly used when we need to change the program's normal flow.
To move the program control to another section of the program, create a label, and then use "goto" followed by the label name to move the program control to the specified label.
Consider the following program as an example of a C "goto" statement.
#include <stdio.h> int main() { int i = 1; fresherearth: printf("%d \n", i); i++; if(i <= 10) goto fresherearth; return 0; }
The following is the output of this program:
1 2 3 4 5 6 7 8 9 10
Let me explain the above program step by step. I will only explain the code written inside "main()."
- Since the program starts its execution from the "main()" method. Therefore, using the first statement, 1 is set to "i."
- The program flow is now entering the label "fresherearth." In this label, the value of "i" is printed on the output console, and then its value is incremented using the statement "i++;"
- Now the condition of the "if" block is evaluated. If its condition evaluates to true, the program flow enters the "if"'s body and the statement:
goto fresherearth;
will be executed, which will transfer control to the "fresherearth" label. And the value of "i" again will be printed and then incremented. - This process continues until the condition of the "if" block evaluates to false.
- Because if the condition of the "if" block evaluates to false, then the program flow does not enter its body, and the statement:
goto fresherearth;
will not be executed.
« Previous Tutorial Next Tutorial »