Skip to main content

Jump Statements in C – break, continue, goto, return


Jump Statement makes the control jump to another section of the program unconditionally when encountered. It is usually used to terminate the loop or switch-case instantly. It is also used to escape the execution of a section of the program.

·        Break

·        Continue

·        Goto

·        Return

1. Break Jump Statement

break statement is used to terminate the execution of the rest of the block where it is present and takes the control out of the block to the next statement.

It is mostly used in loops and switch-case to bypass the rest of the statement and take the control to the end of the loop. The use of the break keyword in switch-case has been explained in the previous tutorial Switch – Control Statement.

Another point to be taken into consideration is that the break statement when used in nested loops only terminates the inner loop where it is used and not any of the outer loops.

Example

#include <stdio.h>

int main() {

  int i;

  for (i = 1; i <= 15; i++) {

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

    if (i == 10)

      break;

  }

  return 0;

}

Output:-

1

2

3

4

5

6

7

8

9

10

 2. continue Jump Statement

The continue jump statement like any other jump statement interrupts or changes the flow of control during the execution of a program. Continue is mostly used in loops.

Rather than terminating the loop it stops the execution of the statements underneath and takes control to the next iteration.

Similar to a break statement, in the case of a nested loop, the continue passes the control to the next iteration of the inner loop where it is present and not to any of the outer loops.

example

#include <stdio.h>

int main() {

  int i, j;

  for (i = 1; i < 3; i++) {

    for (j = 1; j < 5; j++) {

      if (j == 2)

        continue;

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

    }

  }

  return 0;

}

Output:-

1

3

4

1

3

4

 3. goto Jump Statement

goto jump statement is used to transfer the flow of control to any part of the program desired. The programmer needs to specify a label or identifier with the goto statement in the following manner:

goto label;

This label indicates the location in the program where the control jumps to.

Example”

#include <stdio.h>

int main() {

  int i, j;

  for (i = 1; i < 5; i++) {

    if (i == 2)

      goto there;

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

  }

  there:

    printf("Two");

  return 0;

}

Output:-

1

Two

In this program, we see that when the control goes to the goto there; statement when i becomes equal to 2 then the control next goes out of the loop to the label(there: ) and prints Two.

 

4. Return Jump Statement

Return jump statement is usually used at the end of a function to end or terminate it with or without a value. It takes the control from the calling function back to the main function(main function itself can also have a return).

An important point to be taken into consideration is that return can only be used in functions that is declared with a return type such as int, float, double, char, etc.

The functions declared with void type does not return any value. Also, the function returns the value that belongs to the same data type as it is declared. Here is a simple example to show you how the return statement works.

Example

#include <stdio.h>

char func(int ascii) {

  return ((char) ascii);

}

int main() {

  int ascii;

  char ch;

  printf("Enter any ascii value in decimal: \n");

  scanf("%d", & ascii);

  ch = func(ascii);

  printf("The character is : %c", ch);

  return 0;

}

Output:-

Enter any ascii value in decimal:

110

The character is : n

 

Happy coding!! 😊



 

Comments

Popular posts from this blog

C++ Lec-2 Introduction to Object-Oriented Programming (OOP) in C++

  Introduction to Object-Oriented Programming (OOP) in C++ Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to organize software design. It allows for modelling real-world entities and relationships in a program. C++ is an object-oriented programming language that provides features to implement OOP concepts effectively. Key Concepts of OOP   Class and Object o    Class : A blueprint for creating objects. It defines properties and behaviours of objects.   class Car { public:     string brand;     string model;     int year; };   o    Object : An instance of a class.   Car car1; car1.brand = "Mahindra"; car1.model = "THAR"; car1.year = 2024;   Encapsulation o    Encapsulation is the bundling of data and methods that operate on that data within a single unit, or class. It restricts direct acces...

Flow Charts - What Is a Flow Chart? When to Use a Flowchart? Flowchart Symbols & Components

  Flow Charts     Flow charts are a useful tool in many situations, as we make a process easy to understand at a glance. Using just a few words and some simple symbols, they show clearly what happens at each stage and how this affects other decisions and actions.   What Is a Flow Chart?     In 1921, the Frank and Lillian presented what was only a "graphic-based method" in a presentation titled: “ Process Charts: First Steps in Finding the One Best Way to do Work ”, to members of the American Society of Mechanical Engineers (ASME). When to Use a Flowchart?     Flowchart is a very simple yet powerful tool to improve productivity in both our personal and work life. Here are some ways flowchart can be helpful:   ·      Document a process ·      Present solution to a problem ·      Brainstorm ideas in a meeting ·      Design an operation system ·    ...

Difference Between POP and OOP

  What is the Difference Between POP and OOP? Procedure-Oriented Programming (POP) and Object-Oriented Programming (OOP) are two fundamental programming paradigms. Both have their own methodologies, approaches, and use cases. Below, we will define both paradigms, outline their key differences, and provide examples of each for better understanding.   Definition of POP (Procedure-Oriented Programming) Procedure-Oriented Programming (POP) is a programming paradigm where programs are designed around procedures or functions. It focuses on dividing the program into smaller units called functions, which operate on data. Key Features of POP: Program is divided into functions or procedures. Functions are the main building blocks of the program. Emphasizes a step-by-step procedural approach. Data and functions are separate, with functions manipulating data.   Definition of OOP (Object-Oriented Programming) Object-Oriented Programming (OOP) is a p...