While Loop

Syntax: looping_statement::==
while (conditional) statement

The while loop executes while the conditional is true, the conditional can consist of any logical expression. Statements in the loop can be grouped using the keywords begin ... end. The example below illustrates a while loop.

        /* How many cups of volume 33 ml does it
           take to fill a jug of volume 1 litre  */

        `define JUGVOL 1000
        `define CUPVOL 33

        module cup_and_jugs;
           integer cup, jug;

           initial begin
              cup = 0; jug = 0;
              while (jug < `JUGVOL) begin
              jug = jug + `CUPVOL;
              cup = cup + 1;
              end // while (jug < JUG_VOL)

              $display("It takes %d cups", cup);
           end // initial begin

        endmodule // cup_and_jugs


Notice the use of the "define" statement.

EXERCISE
How many times does the folowing while loop say hello?

        initial begin
         a = 5;
         c = 0;

        while (a != c) begin
             $display("Hello");
             c = c + 1;
        end
        end

Answers


previous next contents