0
0
Verilogprogramming~10 mins

If-else in always blocks in Verilog - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - If-else in always blocks
Start always block
Evaluate if condition
Yes No
Execute if block
Update outputs
End always block
The always block starts, checks the if condition, executes the matching branch, updates outputs, then ends.
Execution Sample
Verilog
always @(posedge clk) begin
  if (reset) begin
    q <= 0;
  end else begin
    q <= d;
  end
end
On each clock edge, if reset is high, q is set to 0; otherwise, q takes the value of d.
Execution Table
StepCondition (reset)Branch TakenActionq value
11 (true)ifq <= 00
20 (false)elseq <= d (assume d=5)5
30 (false)elseq <= d (assume d=10)10
41 (true)ifq <= 00
50 (false)elseq <= d (assume d=3)3
💡 Simulation ends after 5 clock cycles traced.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5
resetX10010
dXX510X3
qX051003
Key Moments - 3 Insights
Why does q get set to 0 when reset is 1, even if d has a value?
Because the if condition (reset == 1) is true, the always block executes the if branch (q <= 0), ignoring d. See execution_table rows 1 and 4.
What happens if reset is 0? Does q keep its old value?
No, q gets updated to d's value in the else branch. See execution_table rows 2, 3, and 5 where reset is 0 and q changes to d.
Is the else block mandatory after if in always blocks?
No, but if else is missing and if condition is false, q would keep its old value (no assignment). Here, else ensures q updates when reset is 0.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3, what is the value of q?
A0
B5
C10
D3
💡 Hint
Check the 'q value' column at Step 3 in the execution_table.
At which step does the if condition become true again after being false?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Condition (reset)' column in the execution_table to find when reset changes from 0 to 1.
If the else block was removed, what would happen when reset is 0?
Aq would be set to 0
Bq would keep its previous value
Cq would be set to d
DSimulation would error
💡 Hint
Without else, if condition false means no assignment; check key_moments explanation about else necessity.
Concept Snapshot
always @(event) begin
  if (condition) begin
    // if block
  end else begin
    // else block
  end
end

- If condition true, execute if block
- Else execute else block
- Used for sequential logic triggered by events
Full Transcript
This visual execution shows how an if-else statement works inside a Verilog always block. The always block triggers on a clock edge. It checks the reset signal. If reset is 1, q is set to 0. If reset is 0, q takes the value of d. The execution table traces five clock cycles showing reset, d, and q values. The variable tracker shows how these variables change step by step. Key moments clarify why q changes or stays the same depending on reset and the presence of else. The quiz tests understanding of q's value at specific steps, when reset changes, and the role of else. The snapshot summarizes the syntax and behavior of if-else in always blocks.