While Loop
Part of the Fundamentals section of Coddy's Verilog journey — lesson 59 of 90.
The while loop repeats a block of code as long as a condition is true. Unlike the for loop, it does not have a built-in counter — you must update the loop variable yourself.
A while loop checks a condition before each iteration. If the condition is true, the loop executes. If false, the loop stops.
Syntax:
while (condition) begin
// Code to repeat
endSimple Example
integer i = 0;
while (i < 5) begin
$display("i = %d", i);
i = i + 1;
endOutput:
i = 0
i = 1
i = 2
i = 3
i = 4The loop runs as long as i < 5.
While vs For Loop
| For Loop | While Loop | |
|---|---|---|
| Counter | Built-in | You manage it |
| When to use | Known number of iterations | Unknown when loop will stop |
| Syntax | for (i=0; i<5; i=i+1) | while (i < 5) |
While Loop in Testbenches
While loops are useful when you don't know exactly how many iterations you need:
integer i = 0;
while (i < 16) begin
$display("Testing value %d", i);
i = i + 1;
endFinding a Specific Value
integer i = 0;
reg [7:0] data;
while (data[i] != 1) begin
i = i + 1;
end
$display("First 1 found at position %d", i);Important Rules
| Rule | Explanation |
|---|---|
| Condition must become false | Otherwise infinite loop |
| Update loop variable inside | Or loop never ends |
Loop variable must be integer | Cannot be reg |
Use begin/end for multiple statements | Required for more than one line |
Challenge
What to do:
Add the missing while loop to print numbers from 0 to 2.
Cheat sheet
The while loop repeats a block of code as long as a condition is true. You must update the loop variable manually.
integer i = 0;
while (i < 5) begin
$display("i = %d", i);
i = i + 1;
endKey rules:
- Loop variable must be
integer(notreg) - Always update the loop variable inside the loop to avoid infinite loops
- Use
begin/endfor multiple statements
While vs For: Use for when the number of iterations is known; use while when the stop condition is dynamic (e.g., searching for a value).
// Finding first set bit
while (data[i] != 1) begin
i = i + 1;
endTry it yourself
module while_challenge;
integer i;
initial begin
i = 0;
$display("Printing 0 to 2:");
// TODO: Add while loop
// Loop while i < 3
// Inside, print i
// Increment i by 1
$finish;
end
endmoduleThis lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 1
Arithmetic OperatorsModulo OperatorComparison OperatorsRecap - Simple MathBitwise Operators7Assign And Gates
Continuous AssignmentAssign With OperatorsBuilt In Gate PrimitivesAND OR NOT GatesXOR XNOR GatesRecap - Logic Gate Circuit10Decision Making
If StatementIf - ElseRecap - Simple ComparatorCase StatementCasex And CasezRecap - ALU Design5Operators Part 2
Logical OperatorsReduction OperatorsShift OperatorsConcatenation OperatorConditional OperatorRecap - Operator Challenge3Number System
Binary RepresentationSized NumbersUnsized NumbersNegative NumbersSpecial Values X And ZRecap - Number Formats6Modules
Module StructureInput And Output PortsInout PortsModule InstantiationPort Mapping By NamePort Mapping By OrderRecap - Build A Module9Procedural Blocks
Always BlockInitial BlockSensitivity ListBlocking AssignmentNon Blocking AssignmentRecap - Always vs Initial15Traffic Light Controller
Defining The StatesState Machine Logic