Jump Statements#

https://updatelab.files.wordpress.com/2016/08/jump-and-break-statements.jpeg?w=568

Overview#

Definition

Jump statements are control flow statements used in programming languages to alter the normal sequence of execution. These statements allow programmers to control the flow of their code by transferring control to different parts of the program based on certain conditions

Use Cases

break

Terminates the innermost loop or switch statement and transfers control to the statement immediately following the loop or switch.

continue

Skips the remaining statements in the current iteration of a loop and proceeds to the next iteration.

goto

Unconditionally transfers control to a specified label within the current function.

Pros & Cons

👍
Concise Controls [All]

break and continue statements provide a concise way to control loops and switch statements, improving code readability and efficiency.

Flexible Flow [C++]

The goto statement, although controversial, allows for flexible control flow in certain situations.

Passing Values [Python]

The return statement allows functions to pass values back to the caller.

Exit Flow [Rust & Go]

The return statement allows functions to exit early and return values.

👎
Overuse & Misuse [All]

Overusing break, continue, or goto statements can make code harder to read and understand, leading to potential bugs and maintenance issues, as well as unexpected behavior and infinite loops.

Spaghetti Code [C++]

The goto statement can create spaghetti code and make the program harder to maintain and debug.

Convoluted Logic [Rust & Go]

Relying too heavily on return statements may result in convoluted program logic and reduced maintainability.

continue & break statements#

start loop
- process statement(s)/task(s) until condition is met
  - continue    // process moves forward within the loop
  - break       // breaks out of the loop and moves to the next instruction
end for loop
 1#include <iostream>
 2using namespace std;
 3
 4void jumpStatementsExample() {
 5    for (int i = 0; i < 5; i++) {
 6        if (i == 2) {
 7            continue; // Skip the rest of the loop body for i = 2
 8        }
 9        cout << i << endl;
10        if (i == 3) {
11            break; // Terminate the loop when i = 3
12        }
13    }
14}
15
16int main() {
17    jumpStatementsExample();
18    return 0;
19}
1def jump_statements_example():
2    for i in range(5):
3        if i == 2:
4            continue  # Skip the rest of the loop body for i = 2
5        print(i)
6        if i == 3:
7            break  # Terminate the loop when i = 3
8
9jump_statements_example()
 1public class JumpStatementsExample {
 2
 3    public static void main(String[] args) {
 4        jumpStatementsExample();
 5    }
 6
 7    public static void jumpStatementsExample() {
 8        for (int i = 0; i < 5; i++) {
 9            if (i == 2) {
10                continue; // Skip the rest of the loop body for i = 2
11            }
12            System.out.println(i);
13            if (i == 3) {
14                break; // Terminate the loop when i = 3
15            }
16        }
17
18        int result = performCalculation();
19        System.out.println("Result: " + result);
20    }
21
22    public static int performCalculation() {
23        for (int i = 0; i < 5; i++) {
24            if (i == 2) {
25                return i * 2; // Exit the function and return a value for i = 2
26            }
27            System.out.println("Calculation: " + i);
28        }
29        return 0;
30    }
31}
 1fn jump_statements_example() {
 2    for i in 0..5 {
 3        if i == 2 {
 4            continue; // Skip the rest of the loop body for i = 2
 5        }
 6        println!("{}", i);
 7        if i == 3 {
 8            break; // Terminate the loop when i = 3
 9        }
10    }
11}
12
13fn main() {
14    jump_statements_example();
15}
 1package main
 2
 3import "fmt"
 4
 5func jumpStatementsExample() {
 6    for i := 0; i < 5; i++ {
 7        if i == 2 {
 8            continue // Skip the rest of the loop body for i = 2
 9        }
10        fmt.Println(i)
11        if i == 3 {
12            break // Terminate the loop when i = 3
13        }
14    }
15}
16
17func main() {
18    jumpStatementsExample()
19}

while loop#

start while loop
- while condition is true
  - continue some task(s) until condition is false
- condition is false
  - break loop
end while loop
#include <iostream>

int main() {
    int i = 0;
    while (i <= 4) {
        std::cout << "Value of i: " << i << std::endl;
        i++;
    }
    return 0;
}
i = 0
while i <= 4:
    print("Value of i:", i)
    i += 1

i = 0
while i <= 4:
    print("Value of i:", i)
    i += 1
fn main() {
    let mut i = 0;
    while i <= 4 {
        println!("Value of i: {}", i);
        i += 1;
    }
}
package main

import "fmt"

func main() {
    i := 0
    for i <= 4 {
        fmt.Println("Value of i:", i)
        i++
    }
}

Exit Controlled#

An exit-controlled loop, also known as a post-test loop, evaluates the loop condition after executing the loop body. This means that the loop body is guaranteed to execute at least once, even if the condition is initially false.

The loop body is executed first, and then the loop condition is checked. If the condition is true, the loop body is executed again for the next iteration. If the condition becomes false, the loop is terminated, and the program continues execution after the loop.

do while loop#

start while loop
- complete task(s) in loop once
- while condition is true
  - continue some task(s) until condition is false
- condition is false
  - break loop
end while loop
#include <iostream>

int main() {
    int i = 0;
    do {
        std::cout << "Value of i: " << i << std::endl;
        i++;
    } while (i <= 4);
    return 0;
}
i = 0
while True:
    print("Value of i:", i)
    i += 1
    if i > 4:
        break

https://res.cloudinary.com/teepublic/image/private/s--nY05dwdm--/t_Preview/b_rgb:ffffff,c_lpad,f_jpg,h_630,q_90,w_1200/v1536944098/production/designs/3155319_0.jpg
// Does not exist in Rust...
package main

import "fmt"

func main() {
    i := 0
    for {
        fmt.Println("Value of i:", i)
        i++
        if i > 4 {
            break
        }
    }
}

Compare & Contrast#

Language

continue

break

return

labeled break

C++

Works in loops (for, while, do-while) and skips the current iteration.

Terminates the innermost loop or switch statement and transfers control to the statement immediately following the loop or switch.

Exits the current function and returns a value (optional).

Can use the goto statement to transfer control to a labeled statement within the same function (controversial usage).

Python

Works in loops (for, while) and skips the current iteration.

Terminates the innermost loop (for loop or while loop) and transfers control to the statement immediately following the loop.

Exits the current function and returns a value (optional).

Does not support a direct labeled break statement, but can achieve similar behavior using boolean flags or exception handling.

Java

Works in loops (for, while, do-while) and skips the current iteration.

Terminates the innermost loop or switch statement and transfers control to the statement immediately following the loop or switch.

Exits the current function and returns a value (optional).

Can be used to break out of nested loops using a labeled identifier.

Rust

Works in loops (for, while) and skips the current iteration.

Terminates the innermost loop and transfers control to the statement immediately following the loop.

Exits the current function and returns a value (optional).

Does not have a direct labeled break statement, but can use the loop construct with break and a label to achieve similar behavior.

Golang

Works in loops (for) and skips the current iteration.

Terminates the innermost loop and transfers control to the statement immediately following the loop.

Exits the current function and returns a value (optional).

Supports labeled break statements to break out of nested loops using a labeled identifier.