Know this already? Skip to the exercise: Battery Charge Controller
A conditional assignment chooses which value drives an output. It describes selection hardware that responds to its inputs rather than a stored decision, and it builds on concurrent assignments.
A fan must follow this temperature rule:
| Temperature in degrees Celsius | Speed code |
|---|---|
| Below 18 | 00, off |
| 18 through 24 | 01, slow |
| 25 through 34 | 10, medium |
| 35 or higher | 11, fast |
A candidate checks temperature >= 18 first and immediately selects slow when that is true, and only otherwise does it check the higher thresholds.
Quick check
Question 1 of 1
A simple sensor selector illustrates the syntax:
shown_o <= main_i when ok_i = '1' else backup_i;
Read when ... else as the condition selecting the preceding value or the following alternative. It remains one concurrent assignment and one output driver.
Both inputs can exist at the same time, and the selector is a multiplexer, often shortened to mux. No clock is needed for this combinational decision.
At 40 degrees all three threshold conditions are true, so the required highest speed must take priority.
speed_o <= "11" when temp_i >= 35 else
"10" when temp_i >= 25 else
"01" when temp_i >= 18 else
"00";
The first true condition supplies the result, and later conditions handle only the inputs not already selected. Synthesis may optimize the logic, but the required priority must remain the same. The final fallback covers temperatures below every threshold.
Keep the unconditional final alternative, because an incomplete combinational assignment can retain its previous output and infer storage.
Suppose a maintenance input must force the fan off even at 40 degrees. Would you check it before or after the temperature choices? The override must win over every matching threshold. This is a design question about a changed spec; the shipped example has no such input.
The shared-pin recipe chooses between driving a value and releasing the pin:
line_io <= value_i when enable_i = '1' else 'Z';
Here Z means the driver releases the shared connection. Real tri-state support depends on where that connection exists in the target device.
A comparison can also drive a flag:
over_o <= '1' when total_o > LIMIT else '0';
The conditional form converts the comparison's Boolean result into a logic value.
Open Fan Speed Selector in the example panel on the right. Its fan_speed source implements the required threshold order.
Before reading each output, work out the codes at 24, 25, 34, and 35 degrees. The waveform changes speed exactly at 25 and 35, and at 40 speed_o remains 11 despite all three comparisons being true.
Cooling back through 30, 20, and 10 gives medium, slow, then off, with no memory of the previous higher speed.