# 7.4: if-elseif-else Statements

The final type of branch to be introduced in this text allows the programmer to choose one of several options. It is implemented as an if-elseif-else statement. The if and elseif statements will contain a conditional to decide if they will be executed or not. The else will be automatically chosen if no condition is true.

To introduce the if-elseif-elsestatement, the following program which translates a number grade into a letter grade is implemented. The following pseudo code fragment shows the logic for this if-elseif-else statement.

if (grade > 100) || grade < 0)
{
}
{
}
{
}
{
}
{
}
else
{

}


To translate the if-elseif-else statement, once again the overall structure for the statement will be generated, and then the code blocks will be filled in. Students and programmers are strongly encouraged to implement algorithmic logic in this manner. Students who want to implement the code in a completely forward fashion, where statements are generated as they exist in the program, will find themselves completely overwhelmed and will miss internal and stopping conditions, especially when nest blocks containing nested logic is used late in this chapter.

The steps in the translation of the if-elseif-else statement are as follows.

1. Implement the beginning of the statement with a comment, and place a label in the code for each elseif condition, and for the final else and end_if conditions. At the end of each code block place a branch to the end-if label (once any block is executed, you will exit the entire if-elseif-else statement). Your code would look as follows:
#if block
# first if check, invalid input block
b end_if
b end_if
b end_if
b end_if
b end_if
else:
b end_if
end_if:

2. Next put the logic conditions in the beginning of each if and elseif block. In these if and elseif statements the code will branch to the next label. When this step is completed, you should now have code that looks like the following:
#if block
lw $s0, num slti$t1, $s0, 0 sgt$t2, $s0, 100 or$t1, $t1,$t2
beqz $t1, grade_A #invalid input block b end_if grade_A: sge$t1, $s0, 90 beqz$t1, grade_B
b end_if
sge $t1,$s0, 80
beqz $t1, grade_C b end_if grade_C: sge$t1, $s0, 70 beqz$t1, grade_D
b end_if
sge $t1,$s0, 60
beqz $t1, else b end_if else: b end_if end_if:  3. The last step is to fill in the code blocks with the appropriate logic. The following program implements this completed if-elseif-else statement. Program 7-6: Program to implement if-elseif-else statement in assembly .text #if block lw$s0, num
slti $t1,$s0, 0
sgt $t2,$s0, 100
or $t1,$t1, $t2 beqz$t1, grade_A
#invalid input block
la $a0, InvalidInput jal PrintString b end_if grade_A: sge$t1, $s0, 90 beqz$t1, grade_B
la $a0, OutputA jal PrintString b end_if grade_B: sge$t1, $s0, 80 beqz$t1, grade_C
la $a0, OutputB jal PrintString b end_if grade_C: sge$t1, $s0, 70 beqz$t1, grade_D
la $a0, OutputC jal PrintString b end_if grade_D: sge$t1, $s0, 60 beqz$t1, else
la $a0, OutputD jal PrintString b end_if else: la$a0, OutputF
jal PrintString
b end_if
end_if:

jal Exit
.data
num: .word 70
InvalidInput: .asciiz "Number must be > 0 and < 100"