Module 0040: Nested statements

Tak Auyeung, Ph.D.

September 12, 2019

Contents

1 About this module
2 Why?
3 An example
4 A more complex example
5 How to interpret nested statements?

1 About this module

2 Why?

In other modules, we have explored sequences, conditional statements and loops. Although each type of statements has its application, they are not sufficient to express most algorithms by themselves.

Most algorithms involve the use of nesting. In other words, we can enclose a statement or statements by another statement. A sequence can enclose a loop, a loop can enclose a conditional statement, and etc. In fact, there is limitation as to what kind of statement can embed what kind of statement. Indeed, a loop and embed another loop!

3 An example

Let’s begin with an example that we have already seen in another module:


Listing 1: A simple algorithm with a nested statement
1x0 
2while (x<5do  
3  xx+1  
4end while 

In this algorithm, line 2 marks the beginning of the prechecking loop, whereas line 4 marks the end of the prechecking loop. The statement on line 3 is contained within the prechecking loop. We can say that the assignment statement (on line 3 is nested inside the prechecking loop.

Note that use indentation to indicate that line 3 is contained within the construct that begins on line 2 and 4. Whenever we nest a statement inside another one, the nested (or embedded) one is indented by one additional level. Although this is not strictly a required syntax of pseudocode, it is commonly used and expected.

4 A more complex example

Now that we understand the simpler example, let us consider a slightly more complex example:


Listing 2: A loop that embeds another loop
1x0  
2s0  
3while (x<3do  
4  y0  
5  while (y<2do  
6    yy+1  
7    ss+y+x  
8  end while  
9  xx+1  
10end while  
11whatever follows 

The indentation shows how the statements are nested. Lines 6 and 7 are the most nested statements. They are nested within the prechecking loop that begins on line 5 and ends on line 8.

Note that the assignment statements on lines 4 and 9 are “peers” of the prechecking loop that begins on line 5 and ends on line 8.

Everything from line 4 to line 9 are nested inside the outer prechecking loop.

It may be helpful to show a diagram of this algorithm. Figure 1 represents the same logic in a trail-map format.


PIC

Figure 1: Graphical representation of algorithm 2.

Of course, no explanation is complete without a trace! Table 1 traces this algorithm.


line#x y s

comments






pre? ? ?

we know nothing about the variables

1 0 ? ?

x is initialized

2 0 ? 0

s is initialized

3 0 ? 0

x = 0 < 3 is true, get into the outer loop

4 0 0 0

initialize y

5 0 0 0

y = 0 < 2 is true, get into the inner loop

6 0 1 0

y is incremented

7 0 1 1

s gets 0(s) + 1(y) + 0(x), loop back!

5 0 1 1

y = 1 < 2 is true, get into the inner loop

6 0 2 1

y is incremented again

7 0 2 3

s gets 1(s) + 2(y) + 0(x), loop back

5 0 2 3

y = 2 < 2 is false, exit inner loop

9 1 2 3

x is incremented, loop back to the beginning of the outer loop

3 1 2 3

x = 1 < 3 is true, get into the outer loop

... .........


Table 1: Partial trace of algorithm 2.

5 How to interpret nested statements?

When we have nested statements, there are a few points to observe.

When we go from an embedding (enclosing) statement to its nested content, there is no special consideration: we follow the order from top to bottom.

The most critical part about nested statements is when we finish the nested content, and are ready to get back to the embedding (enclosing) statement. In some cases, we need to match the beginning of enclosing statements. In other words, after we execute line 9 of algorithm 2, we need to find the matching beginning of line 10, which is on line 3.

It is also important to find the matching end of an embedding statement. For example, when x eventually becomes 3 to exit the outer loop in algorithm 2, we need to find the matching end of the outer prechecking loop that starts on line 3. Using indentation as a visual clue, we find that line 10 marks the end of this loop. Consequently, the next statement to execute (after line 3 and x = 4) is line 11.