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...

C++ Lec-1 (Introduction to Programming Languages)

  Introduction to Programming Languages ________________________________________________________________________ P rogramming languages are formal languages comprising a set of instructions that produce various kinds of output. They are used in computer programming to implement algorithms and control the behavior of machines. Language Generation Programming languages have evolved through several generations: First Generation (1GL) : Machine language, the most basic level of programming languages, consisting of binary code. Second Generation (2GL) : Assembly language, a low-level language with a strong correspondence to the machine language instructions. Third Generation (3GL) : High-level languages like C, C++, Java, Python, which are more abstract and easier for humans to understand. Fourth Generation (4GL) : Languages closer to human language, often used for database querying and report generation (e.g., ...

DATA TYPE in C/C++

  BUILT IN OF PRIMARY DATA TYPE     int (Integer) float(Floating-point) double (Double Floating-point) char (Character) wchar_t (Wide Character) bool (Boolean) void (Empty) Modified Data Types List    signed int - (used for integers) unsigned - int (can only store positive integers) short (used - for small integers (range -32768 to 32767)) unsigned short - (used for small positive integers (range 0 to 65,535)) Long - (used for large integers) unsigned long - (used for large positive integers ) long long - (used for very large integers) unsigned long long - (used for very large positive integers) long double - (used for large floating-point numbers) signed char - (used for characters (guaranteed range -127 to 127)) unsigned char - (used for characters (range 0 to 255)) Derived Type Data Type    Function Array Pointers References User-Defined Data Type   Class Structure Union Enumeration Typedef defined DataType