case statement is a multiway decision statement that tests whether an expression matches one of a number of other expressions, and branches accordingly.
case (expression) item1 : begin // statements end item2 : // statement ... default : // statement endcase
Note : The
default statement is optional and you cannot use more than once within the same
case construct. The case item expressions are evaluated in the exact order in which they are given. When the case item matches the expression then the statement associated with that case item will be executed, and then exit the case construct. If all comparisons fail and the
default item is given then the default item statement will be executed. If the
default is not given and all comparisons fail, then none of the case items will be executed, and will exit the case construct.
reg [31:0] data; reg [7:0] res; case (data) 32'hdead_beef : result = 8'hab; 32'hfade_cafe : result = 8'hbc; 32'hcafe_feed : result = 8'hcd; default : result = 8'hff; endcase
As you can see, this is a very good replacement for the
if-else-if construct. But