Jump Statements in Programming

Jump statements in programming are used to change the flow of control within a program. They allow the programmer to transfer program control to different parts of the code based on certain conditions or requirements. Here are common types of jump statements:

1. Break Statement in Programming:

The break statement is primarily used to exit from loops prematurely. When encountered inside a loop, it terminates the loop’s execution and transfers control to the statement immediately following the loop.

C++




#include <iostream>
using namespace std;
int main()
{
    for (int i = 0; i < 10; i++) {
        if (i == 5)
            break;
        cout << i << " ";
    }
    return 0;
}


C




#include <stdio.h>
 
int main()
{
    for (int i = 0; i < 10; i++) {
        if (i == 5)
            break;
        printf("%d ", i);
    }
    return 0;
}


Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        for (int i = 0; i < 10; i++) {
            if (i == 5)
                break;
            System.out.print(i + " ");
        }
    }
}


C#




using System;
 
public class GFG {
 
    static public void Main()
    {
        for (int i = 0; i < 10; i++) {
            if (i == 5)
                break;
            Console.Write($"{i} ");
        }
    }
}


Javascript




for (let i = 0; i < 10; i++) {
    if (i === 5)
        break;
    console.log(i + " ");
}


Python3




for i in range(10):
    if i == 5:
        break
    print(f"{i} ", end="")


Output

0 1 2 3 4 

2. Continue Statement in Programming:

The continue statement is used to skip the current iteration of a loop and proceed to the next iteration.

C++




#include <iostream>
using namespace std;
int main()
{
    for (int i = 0; i < 10; i++) {
        if (i % 2 == 1)
            continue;
        cout << i << " ";
    }
    return 0;
}


C




#include <stdio.h>
 
int main()
{
    for (int i = 0; i < 10; i++) {
        if (i % 2 == 1)
            continue;
        printf("%d ", i);
    }
    return 0;
}


Java




import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 1)
                continue;
            System.out.print(i + " ");
        }
    }
}


C#




using System;
 
public class GFG {
 
    static public void Main()
    {
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 1)
                continue;
            Console.Write($"{i} ");
        }
    }
}


Javascript




for (let i = 0; i < 10; i++) {
    if (i % 2 === 1)
        continue;
    console.log(i + " ");
}


Python3




for i in range(10):
    if i % 2 == 1:
        continue
    print(f"{i} ", end="")


Output

0 2 4 6 8 

3. Return Statement in Programming:

The return statement is used to exit a function and optionally return a value to the caller.

C++




#include <iostream>
using namespace std;
bool isEven(int N) { return N % 2 == 0; }
int main()
{
    int N = 5;
    if (isEven(N)) {
        cout << "N is even";
    }
    else {
        cout << "N is odd";
    }
    return 0;
}


C




#include <stdio.h>
 
int isEven(int N) { return N % 2 == 0; }
 
int main()
{
    int N = 5;
    if (isEven(N)) {
        printf("N is even");
    }
    else {
        printf("N is odd");
    }
    return 0;
}


Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    static boolean isEven(int N) { return N % 2 == 0; }
    public static void main(String[] args)
    {
        int N = 5;
        if (isEven(N)) {
            System.out.println("N is even");
        }
        else {
            System.out.println("N is odd");
        }
    }
}


C#




using System;
 
public class GFG {
    static bool IsEven(int N) { return N % 2 == 0; }
    static public void Main()
    {
        int N = 5;
        if (IsEven(N)) {
            Console.WriteLine("N is even");
        }
        else {
            Console.WriteLine("N is odd");
        }
    }
}


Javascript




function isEven(N) {
    return N % 2 === 0;
}
 
let N = 5;
if (isEven(N)) {
    console.log("N is even");
} else {
    console.log("N is odd");
}


Python3




def isEven(N):
    return N % 2 == 0
 
N = 5
if isEven(N):
    print("N is even")
else:
    print("N is odd")


Output

N is odd

4. Goto Statement in Programming:

Some programming languages support the goto statement, which allows transferring control to a labeled statement within the same function or block of code. However, the use of goto is generally discouraged due to its potential for creating unreadable and unmaintainable code.

C++




#include <iostream>
using namespace std;
int main()
{
    int i = 0;
loopStart:
    if (i < 5) {
        cout << i << " ";
        i++;
        goto loopStart;
    }
    return 0;
}


C




#include <stdio.h>
 
int main() {
    int i = 0;
loopStart:
    if (i < 5) {
        printf("%d ", i);
        i++;
        goto loopStart;
    }
    return 0;
}


C#




using System;
 
public class GFG {
 
    static public void Main()
    {
        int i = 0;
    loopStart:
        if (i < 5) {
            Console.Write(i + " ");
            i++;
            goto loopStart;
        }
    }
}


Output

0 1 2 3 4 



Control flow statements in Programming

Control flow refers to the order in which statements within a program execute. While programs typically follow a sequential flow from top to bottom, there are scenarios where we need more flexibility. This article provides a clear understanding about everything you need to know about Control Flow Statements.

Table of Content

  • What are Control Flow Statements in Programming?
  • Types of Control Flow statements in Programming
  • Conditional Statements in Programming
  • Looping Statements in Programming
  • Jump Statements in Programming

Similar Reads

What are Control Flow Statements in Programming?

Control flow statements are fundamental components of programming languages that allow developers to control the order in which instructions are executed in a program. They enable execution of a block of code multiple times, execute a block of code based on conditions, terminate or skip the execution of certain lines of code, etc....

Types of Control Flow statements in Programming:

Control Flow Statements Type Control Flow Statement Description Conditional Statements if-else Executes a block of code if a specified condition is true, and another block if the condition is false. switch-case Evaluates a variable or expression and executes code based on matching cases. Looping Statements for Executes a block of code a specified number of times, typically iterating over a range of values. while Executes a block of code as long as a specified condition is true. do-while Executes a block of code once and then repeats the execution as long as a specified condition is true. Jump Statements break Terminates the loop or switch statement and transfers control to the statement immediately following the loop or switch. continue Skips the current iteration of a loop and continues with the next iteration. return Exits a function and returns a value to the caller. goto Transfers control to a labeled statement within the same function. (Note: goto is generally discouraged due to its potential for creating unreadable and error-prone code.)...

Conditional Statements in Programming:

Conditional statements in programming are used to execute certain blocks of code based on specified conditions. They are fundamental to decision-making in programs. Here are some common types of conditional statements:...

Looping Statements in Programming:

...

Jump Statements in Programming:

...