Skip to main content

Section 13.4 Nested Statements

The last thing we should mention about algorithms is that you can nest statements within each other. For example, look at the following algorithm below in figure 13.4.1.
Figure 13.4.1. Example of nesting statements
Notice how we nested a for statement within an if statement. Nesting is a way to allow your algorithms to only loop if certain conditions are met (for within an if), or to make certain decisions a number of times (if within a for).
Good Algorithm Checklist
All good algorithms have the following characteristics:
  • Consider the variables that will be used in the algorithm. For example, in the 8 am the solution, the variable was forecast. Thinking about this ahead of time will make writing computer algorithms in MATLAB easier.
  • Check for bugs. To do this it is best to pretend you are really dumb (this can actually be difficult to do). Make sure that the algorithm performs exactly the way that you want it to
  • If there are decisions or loops necessary, make sure that they are written out in such a way that they are easy to read and understand. Only use them if necessary.
You have attempted of activities on this page.