Debuggins Functions
Debuggins Functions
as a Teaching Tool
Kenneth P. Brannan1 and John A. Murden2
Abstract – Survey results from students taking a computer applications class using Mathcad as a programming
language indicate that loops and subscripted variables are difficult topics. In the earlier versions of Mathcad used in
the class, it had not been possible to demonstrate with the software package during the early stages of instruction on
loops how variables change during the line-by-line execution of a loop. With Mathcad 13, additional debugging
tools were included that could be used to monitor variables during the execution of a loop. Student responses in the
survey showed that the new debugging tools helped to improve understanding of loops and subscripted variables.
While the new features were perceived by over two-thirds of the students to be helpful in the teaching and learning
of loops and subscripted variables, other teaching methods such as doing weekly assignments, preparing for weekly
tests, “playing computer,” and working with on-line electronic workbook files were found to be more valuable aids
for learning. Students identified the most difficult topic to be nested loops, which is currently being taught as the
last topic in the programming sequence. However, three of the relatively difficult topics were taught in the first two
weeks, which may contribute to the difficulty experienced by students when they first encounter loops. A review of
previously used languages indicated that those languages had features that facilitated the teaching and learning of
loops in the early stages of teaching loops.
Keywords: programming, Mathcad, debugging tools, loops
INTRODUCTION
Based on experience gained from a combined forty-plus years of teaching programming in several languages, it
appears that one of the critical points at which students encounter difficulty is when loops are introduced. The
difficulty escalates when subscripted variables (arrays) are used in the loops. To teach these features in Mathcad,
the authors have employed several techniques, including explanations of example programs, flowcharts,
pseudocode, hands-on electronic workbook, and frequent tests and assignments. No single technique has eliminated
the barriers to learning, particularly for weaker students.
When Mathcad was selected a decade ago as the programming language, a technique was lost that had been
available in languages used previously – the ability to demonstrate during the early stages of instruction what
happens to loop and non-subscripted variables during line-by-line execution of a loop. With earlier versions of
Mathcad, only after arrays were introduced could students see actual output associated with a function, and by then
many weaker students were struggling to catch up. With Mathcad 13, several new debugging functions have been
added that allow the user to see annotated line-by-line output as the loop is executed. In addition, the debugger
allows execution to be suspended to examine output at a given point in the loop, and the user may step through the
loop interactively, observing the output during each pass through the loop. The purpose of this paper is to describe
how this new feature has been incorporated into an introductory programming class and assess whether the feature
has improved the teaching and learning of loops and subscripted variables.
1
The Citadel, Civil and Environmental Engineering Dept, 171 Moultrie St, Charleston, SC 29409-0225,
[email protected]
2
The Citadel, Civil and Environmental Engineering Dept, 171 Moultrie St, Charleston, SC 29409-0225,
[email protected]
For a number of years, the authors have noticed that their students appear to have difficulty with programming at the
point where loops are introduced. Although loops have always presented a challenge (regardless of the language),
the “loop challenge” seems to have intensified in recent years. One possible explanation for this observation is that
many students in the late 1980s and early 1990s received more programming training prior to beginning college
than current students have had. Yet, based on the authors’ experience, prior programming experience has not
always guaranteed that a student would have a greater advantage in a college programming class. Another
possibility was that something must have changed in the way loops are currently being presented compared to the
way they were presented in the past. Dusting off a few old programming textbooks and course notes revealed that
indeed some seemingly small but interesting changes had taken place over the years.
From a teaching perspective, the change to QuickBASIC was relatively seamless. The language was similar enough
to FORTRAN that essentially the same approach to teaching programming could be used. When the change to C++
was being considered, there were some concerns that the transition may not be smooth. Some of commands were
strikingly different (e.g., cout instead of PRINT or WRITE), the structure had a different appearance (e.g., use of
braces), the greater flexibility of the language was accompanied by more complexity, and most of the faculty
members were not familiar with C++. Nevertheless, the fundamental features of the language needed in an
introductory programming language were similar to previously used languages, which permitted the course to be
taught in much the same way as before. As will be pointed out later, there were some features associated with the
more modular approach to the language that caused a slightly different approach, but adjustments were quickly
made and the reasons were eventually forgotten. Students appeared to accept C++ and performed much the same as
when programming with the previous languages.
Mathcad offered so many valuable tools that the initial problem from a teaching standpoint was how to fit the
desired programming elements into the course syllabus along with the other features. Fundamental programming
elements were comparable to C++, but since all programming had to be accomplished within Mathcad functions, it
was not possible for students to monitor values of variables as they changed within a loop without first learning how
to use subscripted variables. To address this, students were taught in initial classes on looping how to manually
keep up with the variables whose values changed in loops. Later, when subscripted variables were introduced, a
matrix could be generated in a function that returned a printout of values generated in a loop. As time passed,
however, the authors noted that students continued to have difficulty with loops, and they annually made
adjustments in how loops were taught in an effort to improve the course.
With the release of version 13, Mathcad added enhanced debugging features that allow immediate monitoring of
loops. It is not necessary to teach subscripted variables to employ the debugging features. These new debugging
features were incorporated into the course during the fall semester of 2007. In order to illustrate why these Mathcad
debugging features were felt to offer potential for improving the teaching and learning of loops, it is helpful to
As more control structures were introduced, they could either be combined with the GO TO statement or used
independently to build upon a student’s initial experience with loops. For example, in Figure 2 a Block IF structure
is used to increment values for the slope and terminate the loop when all values of the slope have been used in the
computations.
Unrestrained use of the GO TO statement is the primary cause of FORTRAN code that resembles
spaghetti: numerous branchings up and down in a program that result in code that is unreadable
and unalterable. Be extremely conservative in using this statement. [2]
C234567890
REAL N,Q,SLOPE,AREA,HR
SLOPE = 0.001
N = 0.013
AREA = (3.141593*(21./12.)**2)/4.
HR = (21./12.)/4.
100 Q =
(1.49/N)*(HR**(2./3.))*SLOPE**(1./2.)*AREA
IF (SLOPE .LE. 0.004) THEN
WRITE(*,10) SLOPE,Q
SLOPE = SLOPE + 0.001
GO TO 100
ENDIF
10 FORMAT(F7.3,F9.1)
STOP
END
0.001 5.0
0.002 7.1
0.003 8.7
0.004 10.0
Regardless of whether or not the unconditional GO TO or other control statements are used to introduce the concept
of loops, students may follow how variables change in DO loops by placing an output statement inside the loop. In
Figure 3, a WRITE statement placed in the inner loop allows a student to see changes in the loop variables S and
MN, the corresponding values of SLOPE and N, and how the value of the flow, Q, changes with SLOPE and N.
C234567890
INTEGER MN,S
REAL N,Q,SLOPE,AREA,HR
DO 200 S = 1,4,1
SLOPE = REAL(S)/1000.
WRITE(*,*)
DO 100 MN = 11,13,1
N = REAL(MN)/1000.
AREA = (3.141593*(21./12.)**2)/4.
HR = (21./12.)/4.
Q = (1.49/N)*(HR**(2./3.))*SLOPE**(1./2.)*AREA
WRITE(*,10) S,SLOPE,MN,N,Q
100 CONTINUE
200 CONTINUE
10 FORMAT(I3,F7.3,I4,F10.3,3X,F9.1)
STOP
END
Before the switch to Mathcad, the authors took for granted that this would be a standard feature available in any
language. Similar output could be obtained with loops in QuickBASIC and C++. Figure 4 illustrates a for loop in a
C++ program similar to the FORTRAN DO loop in Figure 3. GNU C++ was used for the program.
In addition, the package used in the 1990s for teaching C++ (Borland Turbo C++) had several debugging features
that were ideal for teaching loops. The programming environment had a debugging window called a watch
window. Any variable could be selected for monitoring in the watch window. A program user could single step
through the program and watch the value of the selected variable change as each new step executed. Alternatively,
a breakpoint could be set on a given line of the program, and the program could be executed up to the breakpoint
and then single stepped through the next section.
#include <iostream>
#include <math.h>
#include <iomanip>
int main()
{
int mn,s;
float n,q,slope,area,hr;
Although nothing analogous to the unconditional GO TO statement was used in the C++ version of the course,
students did not appear to be at a disadvantage when loops were introduced in C++ because the utility of the watch
Mathcad Loops
Programs in Mathcad are written using a feature called the Mathcad programming operator. Figure 5 shows a
Mathcad program that from a computational viewpoint works similarly to the FORTRAN program in Figure 2. The
long vertical bar on the left of the program shows the extent of the program. The shorter vertical bar shows the
extent of the while structure. In this program, the slope increments through four values from 0.001 to 0.004 and
four corresponding values for the flow, q, are computed in the loop. However, in the first twelve versions of
Mathcad, there were no output features that could be used to monitor a set of values for each pass through the loop
without storing the values in a matrix. Therefore, until students learned matrices, only one value could be returned
by the program, as illustrated by the number 10.048 (the value of the flow, q, following the final pass through the
loop) to the right of the equals sign in Figure 5.
Beginning with version 13 of Mathcad, two new debugging features were added that permit students to see values
of variables produced in a loop. One feature is called the trace function. The trace function can display both text
and values generated in a loop in a special window referred to as a trace window. When the trace function is
incorporated into a program, execution proceeds without interruption, displaying all output from the trace function
in the trace window. The second feature is the pause function, which works exactly like the trace function except
that the user can step through the program, displaying one line of output from the pause function at one time. The
pause function is analogous to using a breakpoint in the Turbo C++ programming environment. Figure 6 shows the
same program as shown in Figure 5 with the trace function added. Output in the trace window is shown below the
program.
It should be noted that programming classes do not normally use the Mathcad programming operator in exactly the
same way as shown here. In class assignments, a program is usually part of a function definition. To see the output
0.001 5.02413074945107
0.002 7.1051938450094
0.003 8.70204972191835
0.004 10.0482614989021
from the function, the function is evaluated. The form shown in Figures 5 and 6 was selected for better comparison
with the FORTRAN and C++ examples.
Teaching of loops involves material covered over seven lessons of instruction, with each lesson designed to last one
week. The course is a two credit-hour course (one hour lecture, two hours laboratory) with three one-hour classes
(Monday/Wednesday/Friday) per week. Topics are introduced on the first day, students receive assignments on the
second day, and students submit assignments on the third day and take a weekly test. Lessons related to loops
include:
• Iteration (range variables) and graphing. Mathcad range variables are similar to the parameters of a DO
or for loop; they are variables that may change values, beginning at an initial value and incremented by a
• Programming with the if statement. Additional experience with range variables is gained by working with
if statements. Students do not encounter true loops in this lesson.
• Programming with the for statement. Students encounter loops for the first time using the for statement.
Once students are familiar with for statements, the break and continue operators are discussed. In
addition, the trace and pause functions are introduced to students through interactive exercises.
• Programming with the while statement. Work with loops is continued using a different structure – the
while loop.
• Vectors and vector operations. Students are introduced to subscripted variables through the concept of the
vector, which is a variable with a single subscript. The lesson also includes inserting external data files
arranged as a vector into worksheets. Any loops required in this lesson are single loops.
• Programming with vectors. Students learn to search data in a vector using nested loops.
• Programming with matrices. Students learn to search data in a matrix (multiple rows and columns) using
nested loops.
The trace and pause features were incorporated into all lessons that involved loops, beginning with the lesson on
the for statement. To help determine if the degree of emphasis in a class (students spending more time evaluating
computer programs including the trace and pause functions and incorporating these features into programs), the
trace and pause features were purposely emphasized in Section 2 of the class more than they were emphasized in
Section 1. Other aspects of the class were treated similarly. Students were not tested on the trace and pause
features. (This is the way the course was taught using the C++ watch window and breakpoints. Since this had been
very successful, the same methodology was followed for instruction in Mathcad.)
Not surprisingly and by a large margin, the students rated nested-loops as the most difficult of the eight topics in the
list (position eight). After nested loops, there were three topics that were very close in average rank within one
section and more spread in the other. The combined ranking placed the while-loop (with the associated
conditionals) at position seven, the break and continue operators at position six and matrices (variables with
multiple subscripts) at the fifth position on the difficulty list. The remaining four topics were ranked by the students
as for-loops in position four, vectors (variables with a single subscript) used in loops at number three, if statements
in multi-line functions in position 2, and the least difficult of the topics was vectors as a stand-alone topic. The if
statement topic and vectors as a stand-alone topic are not directly associated with loops, but were included to help
determine if
Question 2 on the survey provides additional information on how difficult students perceived working with loops to
be during their first week. Students responded with difficulty rating of 3.4 of 5.0, indicating that students perceived
working with loops to be moderately difficult from the beginning of instruction on loops. This tends to confirm the
previous ranking for the relative difficulty of the early topics (single for-loops, break and continue operators, and
the while-loop) obtained from first question on the survey. Over the seven-week period, then, the initial topics on
looping were perceived to be difficult, the difficulty level decreased following the lesson on while loops, and the
difficulty level then increased toward the end when nested loops were encountered. Ideally, the difficulty level
should begin at a level that promotes learning and increase gradually as students gain more experience with the
concepts.
The remaining items in the survey asked students about the benefit of six tools, techniques or activities used during
the course related to their understanding of loops in general and of subscripted variables in loops. Questions 2 and
3 addressed whether the students believed that Mathcad’s trace and pause features were valuable in helping
students to understand loops and subscripted variables. Over two-thirds of the students (69%) expressed that the
debugging features were helpful in helping them to learn either loops or subscripted variables or both. As noted
previously, these features were intentionally emphasized more in Section 2 than in Section 1. The additional
emphasis may have paid off by the time that subscripted variables were introduced, as almost twice the percentage
of students in Section 2 believed that they benefited from the debugging features as compared to the percentage in
Section 1.
Students were asked in Questions 5 and 6 to assign a helpfulness rating from 1 to 5 for each of the six listed items
associated with understanding loops or understanding subscripted variables. The average “helpfulness ratings” were
used to rank order these six items for each section and the combined enrollment in the lists presented in Table 2.
While 69% of the students responded in Questions 3 and 4 that the new debugging functions, trace and pause,
contributed to their understanding of loops or subscripted variables, there were several other teaching methods that
were considered to be more valuable. As shown in Table 2, students rated “doing a Mathcad assignment” as the
most helpful activity for understanding both loops in general and subscripted variables in loops. Preparing for the
weekly tests, using paper and pencil to track the values of the variables in a loop while “playing computer,” and the
on-line access to the electronic workbook files prepared for each week's lesson were closely grouped to fill out the
four most helpful items.
Even though flow charts appear ahead of the debugging features, the average “helpfulness ratings” for flow charts is
almost numerically equivalent to the trace and pause ratings. However, the authors agreed that in incorporating the
new debugging features into the course, flow charts had not been emphasized as much as in previous years. How a
topic is presented can significantly impact how valuable students perceive it to be. An excellent illustration of this
is occasionally recounted by a colleague [3]. While teaching a transportation class this colleague somewhat
apologetically introduced the subject of funding, legislation and the history of transportation, leaving the impression
with the students that these subjects would be boring. Following the course, this is exactly what the students
reported they experienced with this material. In future years, he changed his approach, demonstrating enthusiasm
for the subject. Students responded in kind as they reported on the course at the end of the semester. Moral: in
future years the authors look forward to representing not only flow charts, but the trace and pause features also with
enough enthusiasm that students will inherit the maximum benefit from these features.
CONCLUDING COMMENTS
Mathcad has been a valuable tool for students at The Citadel for over ten years. With each new version, the
potential of this package has continued to expand for a variety of uses. The new debugging tools introduced in
Version 13 add to the package both debugging and a way to enhance teaching and learning of loops and subscripted
variables.
Through the survey, the students verified that loops and subscripted variables were difficult to learn and that the
debugging tools contributed another dimension for improving their understanding. Although not as helpful as other
The survey also provided the authors with information that may help improve the course by facilitating the teaching
of loops and subscripted variables. In the first two weeks of instruction on loops, three features are currently
presented that are among the most difficult topics for students. It may be worthwhile to rearrange topics (e.g., break
and continue operators and while loops) so that the difficulty level gradually increases instead of having the most
difficult topics all at either the beginning or end of the time spent on loops and subscripted variables.
Using a scale of 1 to 5 (5 most helpful) rate how useful were the following techniques or features in helping
you to understand loops in general
Sec 01 Sec 02 Combined
Doing an assignment in Mathcad — Doing an assignment in Mathcad — Doing an assignment in Mathcad —
4.4 4.4 4.4
On-line lessons — 4.3 Preparing for weekly tests — 4.2 On-line lessons — 4.0
Preparing for weekly tests — 3.9 Using pencil and paper, track the Preparing for weekly tests — 4.0
value of variables through a loop —
3.9
Using pencil and paper, track the On-line lessons — 3.7 Using pencil and paper, track the
value of variables through a loop — value of variables through a loop —
3.6 3.8
Flow charts — 3.1 Flow charts — 3.4 Flow charts — 3.3
Using the trace window with the Using the trace window with the Using the trace window with the
pause or trace function — 2.8 pause or trace function — 3.3 pause or trace function — 3.1
Using a scale of 1 to 5 (5 most helpful) rate how useful were the following techniques or features in helping
you to understand subscripted variables in loops
Sec 01 Sec 02 Combined
Doing an assignment in Mathcad — Preparing for weekly tests — 4.2 Doing an assignment in Mathcad —
4.2 4.2
On-line lessons — 4.1 Doing an assignment in Mathcad — Preparing for weekly tests — 4.0
4.2
Using pencil and paper, track the Using pencil and paper, track the Using pencil and paper, track the
value of variables through a loop — value of variables through a loop — value of variables through a loop —
3.9 4.1 4.0
Preparing for weekly tests — 3.8 On-line lessons — 3.6 On-line lessons — 3.9
Flow charts — 3.0 Using the trace window with the Flow charts — 3.0
pause or trace function — 3.2
Using the trace window with the Flow charts — 3.0 Using the trace window with the
pause or trace function — 2.6 pause or trace function — 2.9
Table 2 Ranked by Students’ “Averaged Helpfulness Rating”
Kenneth P. Brannan
Kenneth P. Brannan is Professor and Head of Civil and Environmental Engineering at The Citadel. He was
President of the Southeastern Section of ASEE during 1998-1999, co-recipient of the Thomas C. Evans
Instructional Paper Award for 1990, and co-recipient of the Best Paper Award at the 2005 ASEE Annual
Conference. He earned B.C.E. and M.S. degrees from Auburn University and the Ph.D. from Virginia Tech. A
registered professional engineer, Dr. Brannan has interests in freshman engineering education, computers in
engineering education, water supply, and wastewater treatment systems.
2) Using a scale of 1 to 5, how difficult were loops to understand during the first week that you
encountered them (5 is the most difficult)? 1 2 3 4 5
3) Did the trace or pause function help your general understanding of loops?
Yes No
4) Did the trace or pause function help your understanding of subscripted variables in loops?
Yes No
5) Using a scale of 1 to 5, how useful were the following techniques or features in helping you to
understand loops in general (5 is the most helpful)?
Less - More
1 2 3 4 5 — Flow charts
1 2 3 4 5 — Using pencil and paper, tracking the value of variables through a loop
1 2 3 4 5 — Using the trace window with the pause or trace function
1 2 3 4 5 — Doing an assignment in Mathcad
1 2 3 4 5 — On-line lessons
1 2 3 4 5 — Preparing for weekly tests
6) Using a scale of 1 to 5, how useful were the following techniques or features in helping you to
understand subscripted variables in loops (5 is the most helpful)?
Less - More
1 2 3 4 5 — Flow charts
1 2 3 4 5 — Using pencil and paper, tracking the value of variables through a loop
1 2 3 4 5 — Using the trace window with the pause or trace function
1 2 3 4 5 — Doing an assignment in Mathcad
1 2 3 4 5 — On-line lessons
1 2 3 4 5 — Preparing for weekly tests