Menu
Coddy logo textTech

Conditional Operator

Part of the Fundamentals section of Coddy's Verilog journey — lesson 28 of 90.

The conditional operator ? : selects between two values based on a condition. It is also called the ternary operator. The conditional operator evaluates a condition and returns one of two values:

condition ? value_if_true : value_if_false

If the condition is true (1), the first value is returned. If false (0), the second value is returned.

Why Use Conditional Operator

It is a compact way to write simple if-else logic in a single line. Common uses include:

  • Selecting between two signals
  • Creating multiplexers
  • Setting default values
  • Inline conditional assignments

Syntax and Examples

Basic example:

result = (a > b) ? a : b;   // result gets the larger of a and b

Select between values:

data_out = (enable) ? data_in : 8'b00000000;   // Output data if enabled, else 0

Invert signal conditionally:

out = (invert) ? ~in : in;   // If invert is 1, output inverted; else unchanged

Code Example

module conditional_demo;
  reg [3:0] a, b, max;
  reg enable;
  reg [3:0] data_in, data_out;
  
  initial begin
    a = 4'd7;
    b = 4'd9;
    enable = 1;
    data_in = 4'b1010;
    
    max = (a > b) ? a : b;           // 9
    data_out = (enable) ? data_in : 4'b0000;   // 1010
    
    $display("max = %d", max);
    $display("data_out = %b", data_out);
    $finish;
  end
endmodule

Output:

max = 9
data_out = 1010

Nested Conditional Operators

You can nest conditional operators for multiple selections:

result = (a > b) ? a : (b > c) ? b : c;   // Find the largest of three values

Use parentheses to make nested conditions clear.

Important Notes

  • The condition must be a single bit (or expression that evaluates to 0 or 1)
  • Both value options must have the same bit width
  • The operator can be used in continuous assignments (assign) and procedural blocks (always, initial)
challenge icon

Challenge

Write the correct conditional expressions for each task.

What to do:

  1. Set max to the larger of x and y
  2. Set abs to the absolute value of val (if negative, make positive)
  3. Set output_data to data if enable is 1, otherwise to 8'b0

Cheat sheet

The conditional (ternary) operator ? : selects between two values based on a condition:

condition ? value_if_true : value_if_false

Examples:

max      = (a > b) ? a : b;                  // larger of a and b
data_out = (enable) ? data_in : 8'b0;        // output data if enabled, else 0
out      = (invert) ? ~in : in;              // conditionally invert signal

Nested conditional operators:

result = (a > b) ? a : (b > c) ? b : c;     // largest of three values

Notes:

  • Condition must evaluate to 0 or 1
  • Both value options must have the same bit width
  • Usable in assign, always, and initial blocks

Try it yourself

module conditional_challenge;
  reg [3:0] x, y;
  reg [3:0] max;
  reg signed [3:0] val;
  reg [3:0] abs;
  reg enable;
  reg [7:0] data;
  reg [7:0] output_data;
  
  initial begin
    x = 4'd12;
    y = 4'd8;
    val = -4'sd5;
    enable = 1;
    data = 8'b10101010;
    
    max = ______;          // larger of x and y
    abs = ______;          // absolute value of val
    output_data = ______;  // data if enable, else 0
    
    $display("max = %d", max);
    $display("abs = %d", abs);
    $display("output_data = %b", output_data);
    $finish;
  end
endmodule
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals