0
0
Verilogprogramming~20 mins

If-else in always blocks in Verilog - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
If-else Always Block Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of if-else in always block with clock edge
What is the value of q after the positive edge of clk when d = 1 and reset = 0?
Verilog
module test(input clk, input reset, input d, output reg q);
  always @(posedge clk or posedge reset) begin
    if (reset)
      q <= 0;
    else if (d)
      q <= 1;
    else
      q <= 0;
  end
endmodule
Aq = 1
Bq = 0
Cq retains previous value
Dq toggles its value
Attempts:
2 left
💡 Hint
Check the order of if-else conditions and the values of inputs at clock edge.
Predict Output
intermediate
2:00remaining
Behavior of nested if-else in always block
Given the inputs enable = 1, mode = 0, and reset = 0, what is the value of out after the always block executes?
Verilog
module nested_if(input enable, input mode, input reset, output reg out);
  always @(*) begin
    if (reset)
      out = 0;
    else begin
      if (enable) begin
        if (mode)
          out = 1;
        else
          out = 0;
      end else
        out = 0;
    end
  end
endmodule
Aout retains previous value
Bout = 1
Cout = 0
Dout is undefined
Attempts:
2 left
💡 Hint
Trace the nested if-else conditions carefully with given inputs.
🔧 Debug
advanced
2:00remaining
Identify the error in if-else usage inside always block
What error will this Verilog code produce when synthesized or simulated?
Verilog
module error_example(input clk, input reset, input d, output reg q);
  always @(posedge clk) begin
    if (reset == 1)
      q <= 0;
    else
      q <= d;
  end
endmodule
ASyntaxError: Missing semicolon after if statement
BSyntaxError: Missing parentheses around if condition
CNo error, code runs correctly
DRuntimeError: q is not assigned in all branches
Attempts:
2 left
💡 Hint
Check the syntax of the if statement condition.
Predict Output
advanced
2:00remaining
Output of if-else with blocking and non-blocking assignments
What is the value of q after the always block executes on a positive clock edge if d = 1 and reset = 0?
Verilog
module blocking_nonblocking(input clk, input reset, input d, output reg q);
  always @(posedge clk or posedge reset) begin
    if (reset)
      q = 0;
    else
      q <= d;
  end
endmodule
Aq = 0
Bq retains previous value
Cq is unknown (X)
Dq = 1
Attempts:
2 left
💡 Hint
Understand difference between blocking (=) and non-blocking (<=) assignments.
🧠 Conceptual
expert
3:00remaining
Effect of missing else in if-else chain inside always block
Consider this always block snippet:
always @(posedge clk) begin
  if (reset)
    q <= 0;
  if (enable)
    q <= d;
end
What is the behavior of q when both reset and enable are high at the same clock edge?
Aq is set to d because the second if overwrites the first assignment
Bq is set to 0 because the first if executes and second is ignored
Cq retains previous value because assignments conflict
Dq is unknown (X) due to conflicting assignments
Attempts:
2 left
💡 Hint
Remember that both if statements execute independently if no else is used.