# Decision and Loop Structures

Posted on in Programming

Decision structures (also known as selection structures) are used in programming to take an action based on the result of a question (Farrell, 2008). An if-then-else statement is the most basic kind of decision structure. The question is represented as a Boolean expression that results in a true or false result. Depending on the result, one of two code paths is taken. Once the end of an if-then-else statement is reached, the program continues at the same point, regardless of which decision was made (Thomas, 2005). The following pseudocode diagrams a scenario when an invoice system must add a late fee to an invoice if the previous invoice is more than 30 days overdue.

start
if daysLate > 30 then
else
no lateFee
endif
print invoice
stop


A case statement is a decision structure that is used when a single variable can produce several different actions based on its value. Although this result can be achieved by using nested if-then-else statements, the case statement is a convenient way to represent this common situation (Farrell, 2008). In a real world application, a billing system may print different bills depending on which week of the month it is as the pseudocode below describes.

start
case based on week
case 1
print week one invoices
case 2
print week two invoices
case 3
print week three invoices
default
print monthly report
endcase
stop


There are two main categories of loop structures. Logically controlled loop structures execute based on the result of a Boolean comparison. Logically controlled loop structures can be further broken down into pre-test and post-test loops. Pre-test loops perform a Boolean comparison and perform the iteration only if the comparison is true. Sometimes called while-do loops, this loop structure is useful if code inside the loop does not have to execute at least once (Scott, 2009). In keeping with the theme of invoice generation, the following pseudocode prints invoices until there is none left to print. A pre-test loop is used here since there may be no invoices to print when the application is run.

start
while invoices > 0 do
print invoice
invoices = invoices - 1
endwhile
stop


A post-test loop performs the statements in the iteration portion of the code before checking the Boolean comparison that controls continued iteration. This structure is also known as a do-until loop structure. A post-test loop is most useful when the code must be executed at least once (Scott, 2009). For example, when printing paychecks for a small business, there will always be at least one paycheck, the owner's.

start
do
print paycheck
paychecks = paychecks - 1
until paychecks = 0
stop


Enumeration-controlled loop structures are executed once for ever value in a defined set. Although for loops are implemented differently across programming languages, the concept is still the same. When the number of iterations of a loop is known, the for loop is the most common implementation (Scott, 2009). A for loop could be used to print a company's work schedule for the upcoming week.

start
for day = 0 to 4
print schedule
endfor
stop