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 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()."

C Online Test


« Previous Tutorial Next Tutorial »