Conditional Statements
If Statements
An if statement evaluates an expression and executes the subsequent statement if the expression evaluates to true, otherwise it skips that statement. For example:
if (i > max_count)
i = 0;
An if/else statement evaluates an expression and executes the statement before else if the expression evaluates to true, otherwise it evaluates the statement after the else. For example:
if (sel)
out = in0;
else
out = in1;
A common idiom is to nest if/else statements as follows:
if (sel==0)
out = in0;
else if (sel==1)
out = in1;
else if (sel==2)
out = in2;
else if (sel==3)
out = in3;
else
out = 0;
In this case an if keyword binds to the next closest else keyword.
Case Statements
A case statement tests and expression and then enumerates what actions should be taken for the various values that expression can take. For example:
case (sel)
0: out = in0;
1: out = in1;
2: out = in2;
3: out = in3;
endcase
If the needed case is not found, then no statements are executed. If multiple cases are found that match, the first one found is used (they are tried in order). If more that once cases should be associated with the same statement, they can be given in a comma separated list:
case (sel)
0: out = in0;
1: out = in1;
2: out = in2;
3: out = in3;
4: out = in4;
5, 6, 7: out = 0;
endcase
It is also possible to specify a default case:
case (sel)
0: out = in0;
1: out = in1;
2: out = in2;
3: out = in3;
4: out = in4;
default: out = 0;
endcase