If - Else
Part of the Fundamentals section of Coddy's Verilog journey — lesson 53 of 90.
The if-else statement allows you to choose between two different actions based on a condition. If the condition is true, one block executes. If false, the other block executes.
The if-else statement gives your code a decision point: do one thing if the condition is true, do another thing if the condition is false.
Syntax:
if (condition) begin
// Executes when condition is true (1)
end else begin
// Executes when condition is false (0)
endSimple Example
if (reset) begin
count = 0;
end else begin
count = count + 1;
end- If
resetis 1 →countbecomes 0 - If
resetis 0 →countincreases by 1
Multiple Statements
Use begin and end when you have more than one statement:
if (enable) begin
out = data_in;
valid = 1;
end else begin
out = 0;
valid = 0;
endIf-Else with Multiple Conditions
You can chain if-else statements:
if (a > b) begin
max = a;
end else if (b > a) begin
max = b;
end else begin
max = a; // a and b are equal
endImportant Rules
| Rule | Explanation |
|---|---|
else is optional | You can have if without else |
else belongs to the nearest if | Be careful with nesting |
Use begin/end for multiple statements | Required for more than one line |
Challenge
What to do:
- Add the missing
if-elsestatement to make this work. - When
enableis 1,outshould equala & b. - When
enableis 0,outshould equala | b.
Cheat sheet
The if-else statement executes one of two blocks based on a condition:
if (condition) begin
// Executes when condition is true (1)
end else begin
// Executes when condition is false (0)
endChain multiple conditions with else if:
if (a > b) begin
max = a;
end else if (b > a) begin
max = b;
end else begin
max = a; // a and b are equal
endelseis optional- Use
begin/endwhen multiple statements are in a block elsealways belongs to the nearestif
Try it yourself
module ifelse_challenge;
reg a, b, enable;
reg out;
initial begin
a = 1;
b = 0;
enable = 1;
// TODO: Add if-else statement
// If enable is 1: out = a & b
// Else: out = a | b
$display("out = %d (should be 0 because 1&0=0)", out);
$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