7.4: if-elseif-else Statements
- Page ID
- 27137
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)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-else
statement, 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) { print("Grade must be } elseif (grade >= 90) { print("Grade is A") } elseif (grade >= 80) { print("Grade is B") } elseif (grade >= 70) { print("Grade is C") } elseif (grade >= 60) { print("Grade is D") } else { print("Grade is F") }
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.
- Implement the beginning of the statement with a comment, and place a label in the code for each
elseif
condition, and for the finalelse
andend_if
conditions. At the end of each code block place a branch to theend-if
label (once any block is executed, you will exit the entireif-elseif-else
statement). Your code would look as follows:#if block # first if check, invalid input block b end_if grade_A: b end_if grade_B: b end_if grade_C: b end_if grade_D: b end_if else: b end_if end_if:
- Next put the logic conditions in the beginning of each
if
andelseif
block. In theseif
andelseif
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 grade_B: sge $t1, $s0, 80 beqz $t1, grade_C b end_if grade_C: sge $t1, $s0, 70 beqz $t1, grade_D b end_if grade_D: sge $t1, $s0, 60 beqz $t1, else b end_if else: b end_if end_if:
- 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" OutputA: .asciiz "Grade is A" OutputB: .asciiz "Grade is B" OutputC: .asciiz "Grade is C" OutputD: .asciiz "Grade is D" OutputF: .asciiz "Grade is F" .include "utils.asm"