If Statement
Part of the Fundamentals section of Coddy's Verilog journey — lesson 52 of 90.
The if statement is a decision-making block that executes code only when a condition is true. The if statement checks a condition.
If the condition is true (1), the code inside executes. If the condition is false (0), the code is skipped.
Syntax:
if (condition) begin
// Code executes when condition is true
endSimple Example
if (reset) begin
count = 0;
endIn this example:
beginandendare used to mark the start and end of the code block
- If
resetis 1 (true), the code insidebeginandendexecutes →countbecomes 0 - If
resetis 0 (false), the code inside is skipped → nothing happens
begin and end work like curly braces { } in other programming languages. They group statements together so Verilog knows which code belongs to the if condition. Even though there is only one statement here, using begin and end is still good practice for consistency.
If Statement in Always Block
always @(posedge clk) begin
if (reset)
count <= 0;
endNote: For a single statement, begin and end are optional. For example, in the always block above no begin/end is needed after if because it has only one statement.
Condition Can Be Any Expression
if (a > b) begin
max = a;
end
if (a && b) begin
out = 1;
end
if (data == 8'hFF) begin
match = 1;
endImportant Rules
| Rule | Explanation |
|---|---|
| Condition must be a single bit | Or expression that evaluates to 0 or 1 |
begin / end needed for multiple statements | Like { } in other languages |
Without begin/end, only one statement follows | The next line only |
Challenge
What to do:
- Add the missing
ifstatement to make this work.
- When
enableis1,outshould equala & b - When
enableis0,outshould remain0(not change)
The starter code initializes out = 0 and tests both cases.
Cheat sheet
The if statement executes code only when a condition is true (1), and skips it when false (0).
if (condition) begin
// executes when condition is true
endFor a single statement, begin/end are optional:
always @(posedge clk) begin
if (reset)
count <= 0;
endConditions can be any expression evaluating to 0 or 1:
if (a > b) begin max = a; end
if (a && b) begin out = 1; end
if (data == 8'hFF) begin match = 1; endKey rules:
- Use
begin/endto group multiple statements (like{ }in other languages) - Without
begin/end, only the immediately following line belongs to theif
Try it yourself
module if_challenge;
reg a, b, enable;
reg out = 0;
initial begin
a = 1;
b = 1;
// Test case 1: enable = 1
enable = 1;
// TODO: Add if statement (out = a & b)
$display("enable=1: out = %d (should be 1)", out);
// Test case 2: enable = 0
enable = 0;
out = 0;
// TODO: out should stay 0
$display("enable=0: out = %d (should be 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