System Verilog break and continue

System Verilog, a widely used hardware description and verification language, provides control flow constructs to make loops more flexible and powerful. Among these constructs, the break and continue statements play a pivotal role in managing loop execution. This article delves into these statements, explaining their purpose, usage, and practical examples.

Break Statement

The break statement is used to terminate the execution of a loop prematurely. When encountered, it causes the program to exit the current loop immediately, regardless of the loop’s conditional expression. This is particularly useful when a certain condition is met, and continuing the loop serves no further purpose.

Syntax

break;

Example: Using Break in a For Loop

module break_example;
    initial begin
        for (int i = 0; i < 10; i++) begin
            if (i == 5) begin
                $display("Breaking at i = %0d", i);
                break; // Exit the loop when i equals 5
            end
            $display("i = %0d", i);
        end
    end
endmodule

Output:

i = 0
i = 1
i = 2
i = 3
i = 4
Breaking at i = 5

In this example, the loop exits as soon as i equals 5, skipping the remaining iterations.

Let’s take same example using while loop.

Example: Using break in while loop

module break_example;
  int i;
  
    initial begin
      while (i<10) begin
            if (i == 5) begin
                $display("Breaking at i = %0d", i);
                break; // Exit the loop when i equals 5
            end
        $display("i = %0d", i);
        i++;
        end
    end
endmodule

Output:

i = 0
i = 1
i = 2
i = 3
i = 4
Breaking at i = 5

Continue Statement

The continue statement skips the current iteration of a loop and jumps to the next iteration. It does not terminate the loop entirely; instead, it allows the loop to bypass the remaining statements in the current iteration and proceed with the next iteration.

Syntax

continue;

Example: Using Continue in a For Loop

module continue_example;
    initial begin
        for (int i = 0; i < 10; i++) begin
            if (i % 2 == 0) begin
                continue; // Skip even numbers
            end
            $display("Odd number: i = %0d", i);
        end
    end
endmodule

Output:

Odd number: i = 1
Odd number: i = 3
Odd number: i = 5
Odd number: i = 7
Odd number: i = 9

In this example, the continue statement ensures that even numbers are skipped while the loop processes only odd numbers.

Break vs. Continue: Key Differences

FeatureBreakContinue
FunctionExits the loop entirelySkips the current iteration and moves to the next
Loop TerminationYesNo
Use CaseWhen the loop no longer needs to execute furtherWhen specific iterations need to be bypassed

Practical Applications

Using Break and Continue Together

In real-world scenarios, the break and continue statements often appear together in complex loops to implement specific conditions.

module break_continue_example;
    initial begin
        for (int i = 0; i < 10; i++) begin
            if (i == 7) begin
                $display("Breaking at i = %0d", i);
                break; // Exit the loop when i equals 7
            end
            if (i % 2 == 0) begin
                continue; // Skip even numbers
            end
            $display("Odd number less than 7: i = %0d", i);
        end
    end
endmodule

Output:

Odd number less than 7: i = 1
Odd number less than 7: i = 3
Odd number less than 7: i = 5
Breaking at i = 7

In this case, even numbers are skipped using continue, while the loop exits entirely when i equals 7 using break.