1. Tracing through the execution of a program 2. Variations on Loops/Repetition (while loop) - counting up (how much accomplished) - counting down (how much left to do) - keeping track of current index Tracing through the execution of a program The following slides show the step-by-step execution of the SmileyApp:Row of Smileys executionLoop Variations 2.1. Version 1 (summary)
The meaning we assign to a variable tells us how to use it. |
void drawSmileyRow( double x, double y, double radius, int n )
{
double curX = x;
double curY = y;
int count = 0; // how many drawn so far
while ( count < n ) // as long as not all have been drawn
{
drawSmiley( curX, curY, radius );
curX = curX + 2*radius;
count = count + 1; // another one drawn
}
}
|
We could have assigned a different meaning to count:count represents number of smileys left to drawThe new meaning of count changes how we initialize, update, and use it in the loop condition: - init: int count = n; * all faces left to draw - update: count = count - 1; * one less left to draw - loop: while ( count > 0 ) * keep drawing as long as smileys left to draw |
void drawSmileyRow( double x, double y, double radius, int n )
{
double curX = x;
double curY = y;
int count = n; // how many left to draw
while ( count > 0 ) // as long as smileys left to draw
{
drawSmiley( curX, curY, radius );
curX = curX + 2*radius;
count = count - 1; // one less left to draw
}
}
|
In the previous versions we kept track of: * how much work was accomplished (Version 1) * how much work was left to do (Version 2) Yet another way to think about the problem is to keep track of the index of the smiley we are about to draw, i.e. the 1st, the 2nd, the 3rd, etc.count represents the index of the smiley to be drawnThe new meaning of count changes how we initialize, update, and use it in the loop condition: - init: int count = 1; * about to draw the 1st smiley - update: count = count + 1; * change to index of next smiley - loop: while ( count <= n ) * keep drawing as long as the index is for one of the smileys; if we are asked to draw n=4 smileys, the indices are 1,2,3,4 |
void drawSmileyRow( double x, double y, double radius, int n )
{
double curX = x;
double curY = y;
int count = 1; // index of smiley to draw
while ( count <= n ) // as long as valid index
{
drawSmiley( curX, curY, radius );
curX = curX + 2*radius;
count = count + 1; // move to index of next smiley
}
}
|
void drawSmileyRow( double x, double y, double radius, int n )
{
double curX = x;
double curY = y;
int count = 0; // how many drawn so far
while ( count < n ) // as long as not all have been drawn
{
drawSmiley( curX, curY, radius );
curX = curX + 2*radius;
count = count + 1; // another one drawn
}
}
|
void drawSmileyRow( double x, double y, double radius, int n )
{
double curX = x;
double curY = y;
int count = n; // how many left to draw
while ( count > 0 ) // as long as smileys left to draw
{
drawSmiley( curX, curY, radius );
curX = curX + 2*radius;
count = count - 1; // one less left to draw
}
}
|
void drawSmileyRow( double x, double y, double radius, int n )
{
double curX = x;
double curY = y;
int count = 1; // index of smiley to draw
while ( count <= n ) // as long as valid index
{
drawSmiley( curX, curY, radius );
curX = curX + 2*radius;
count = count + 1; // move to index of next smiley
}
}
|