while and do while loop

SystemVerilog provides two types of conditional looping constructs: while and do-while loops. These loops are used to repeatedly execute a block of code as long as a specified condition remains true. While similar in purpose, they differ in how they evaluate the loop condition.

The while Loop

The while loop is a pre-condition loop, meaning it evaluates the condition before executing the loop body. If the condition is false at the start, the loop body will not execute at all otherwise it will keep executing the statements until the condition is true.

Syntax

while (condition) begin
    // Code to execute as long as the condition is true
end

Example 1: Basic Usage of while

module array_processing;
    int arr [0:4] = '{1, 2, 3, 4, 5};
    int index = 0;

    initial begin
        // Process array using while loop
      while (index < 5) begin
            $display("arr[%0d] = %0d", index, arr[index]);
            index++;
        end
    end
endmodule

Output:

arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5

Example 2: Infinite while Loop with Termination

module infinite_while_example;
    int count;

    initial begin
        count = 0;

        // Infinite loop with a termination condition
        while (1) begin
            $display("Count = %0d", count);
            count++;

            // Break out of the loop after 10 iterations
            if (count == 10) begin
                $display("Terminating the loop.");
                break;
            end
        end
    end
endmodule

Explanation:

  • The while (1) creates an infinite loop.
  • The if statement and break terminate the loop after 10 iterations.

Output:

Count = 0
Count = 1
Count = 2
Count = 3
Count = 4
Count = 5
Count = 6
Count = 7
Count = 8
Count = 9
Terminating the loop.

The do-while Loop

The do-while loop is a post-condition loop, meaning it executes the loop body at least once before evaluating the condition. This guarantees that the code inside the loop is executed at least once.

Syntax

do begin
    // Code to execute
end while (condition);

Example 3: Basic Usage of do-while

module do_while_ex;
    int count;

    initial begin
        count = 0;

        // Execute the loop body at least once
        do begin
            $display("Count = %0d", count);
            count++;
        end while (count < 5);
    end
endmodule

Output:

Count = 0
Count = 1
Count = 2
Count = 3
Count = 4

Difference Between while and do-while:

The main difference between while and do-while is when the condition is evaluated. While loop will not execute if the condition is false initially but do-while loop will execute at least once.

Example 4:

module while_vs_do_while;
    int count;

    initial begin
        count = 5;

        // While loop
        $display("While Loop:");
        while (count < 5) begin
          $display("Count = %0d", count); //will not execute
        end

        // Do-while loop
        $display("Do-While Loop:");
        do begin
          $display("Count = %0d", count); //Execute for one time
        end while (count < 5);
    end
endmodule

Explanation:

  • In the while loop, the condition is evaluated first. Since count < 5 is false, the body is not executed.
  • In the do-while loop, the body executes once before checking the condition.

Output:

While Loop:
Do-While Loop:
Count = 5