Repeat Loop
Part of the Fundamentals section of Coddy's Verilog journey — lesson 60 of 90.
The repeat loop executes a block of code a fixed number of times. Unlike the for loop, it does not use a loop counter variable.
A repeat loop runs a specified number of times. You give it a constant or expression that determines how many iterations to execute.
Syntax:
repeat (number) begin
// Code to repeat
endSimple Example
repeat (5) begin
$display("Hello");
endOutput:
Hello
Hello
Hello
Hello
HelloThe message prints exactly 5 times.
Repeat with an Expression
integer count = 3;
repeat (count) begin
$display("Looping");
endOutput:
Looping
Looping
LoopingRepeat vs For Loop
| For Loop | Repeat Loop | |
|---|---|---|
| Counter variable | Yes (explicit) | No |
| When to use | Need index value | Just need repetition |
| Example | for (i=0; i<5; i=i+1) | repeat (5) |
Generating Multiple Clock Cycles
initial begin
clk = 0;
repeat (20) begin
#5 clk = ~clk;
end
endThis generates 20 clock edges (10 complete cycles).
Important Rules
| Rule | Explanation |
|---|---|
| Number must be non-negative | Cannot repeat negative times |
| Can use constant or expression | repeat (10) or repeat (count) |
| No loop variable available | Cannot track iteration number |
Use begin/end for multiple statements | Required for more than one line |
Challenge
What to do:
Add the missing repeat loop to print "Verilog" 4 times.
Cheat sheet
The repeat loop executes a block of code a fixed number of times without a loop counter variable.
repeat (number) begin
// Code to repeat
endCan use a constant or variable expression:
integer count = 3;
repeat (count) begin
$display("Looping");
endCommon use case — generating clock cycles:
repeat (20) begin
#5 clk = ~clk;
endRepeat vs For Loop: Use repeat when you only need repetition (no index needed); use for when you need the iteration counter value.
Try it yourself
module repeat_challenge;
initial begin
$display("Printing 4 times:");
// TODO: Add repeat loop
// Repeat 4 times
// Inside, print "Verilog"
$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