PALMS Module 02 Lab: While Loops in C++ · PDF filePALMS MODULE 2 LAB: WHILE LOOPS IN C++ 3...
Transcript of PALMS Module 02 Lab: While Loops in C++ · PDF filePALMS MODULE 2 LAB: WHILE LOOPS IN C++ 3...
PALMS MODULE 2 LAB: WHILE LOOPS IN C++ 2
PALMS for CS1 v4.0
Introduction
This lab will provide students with an introduction to the fundamentals of conditional repetition
in the C++ programming language. The focus of this lab is on how to solve complex problems
using the C++ “while” statement. At the end of this module, students will…
Be able to solve simple problems using C++ “while” statement
Understand the structure of C++ “while” statement
These exercises have two different types of answers. Some questions ask for answers to specific
questions; you should collect these answers in a Microsoft Word document.
Other exercises ask you to write code; for these exercises, structure your storage drive in the
following manner. There should be a palms folder, and within that folder there should be a
LabWhile folder. All coding exercises (and related folders) for this lesson should be placed in the
LabWhile folder.
As always, be sure to properly document your code. Consult the C++ Coding Guidelines
document for proper coding standards. Use good design principles and design the solution before
attempting to write code. All programs should make use of multiple functions besides main()
(use the input/process/output cycle as your guide). Think about how each problem actually
involves more than one subproblem. At a minimum, you must construct functions aligned with
the input-process-output steps. Let’s begin!
Part 1: Conditional Repetition in C++
Many operations in computer programs (as in real life) involve repeating a sequence of events.
Consider the simple act of buying groceries. When you bring groceries up to the grocery store
clerk, he or she will scan each item and place it into a grocery bag. The act of scanning the item
you wish to buy is repeated for each item in your shopping cart until your cart is empty.
Similarly, the act of placing the item purchased into a grocery bag is repeated until there are no
more items. In algorithms and computer programming, the process of repeatedly performing the
same set of operation until a specific condition is false is known as conditional repetition.
Conditional repetition is the repeated execution of a statement or a sequence of statements.
This repetition of statements continues while a specified condition is true. Like conditional
selection, conditional repetition involves making a decision. In the grocery store example, the
decision is whether to scan the next item. This decision is made by checking the shopping cart: if
the cart is not empty, continue; if the cart is empty, stop.
PALMS MODULE 2 LAB: WHILE LOOPS IN C++ 3
PALMS for CS1 v4.0
Once again, conditional expressions are used to determine the next step. However, unlike with
conditional selection, the decision process may be repeated multiple times. If the conditional
expression is true, a set of nested statements are executed. At the end of this execution, flow of
control returns back to the original condition, and the process repeats. Only when the conditional
expression is false does this “loop” end. The process of conditional repetition is often
represented in algorithms and programs with the use of while statements, sometimes called
while loops. These compound statements execute a statement or a series of statements as long as
a specified condition is true. C++ provides a while statement to implement conditional
repetition. The general form for a while loop is as follows:
while ( /* conditional expression */ )
{
// one or more statements to execute if the
// condition is true
}
The loop behavior is controlled by the conditional expression enclosed in parentheses. Similar to
an if statement, the conditional expression is evaluated first. If the expression evaluates to true,
then the statements inside the loop body ( i.e., inside { } ) are executed in order. After making a
“pass” through the loop body, the whole process is repeated – control loops back around to the
conditional expression. The conditional expression is evaluated again, and if it succeeds, then the
statements inside the loop body are executed again. This process repeats until the conditional
expression evaluates to false. For example, consider the while loop in the C++ code below:
cout << "Please enter a number between 0 and 100: ";
cin >> user_input;
while ((user_input < 0) || (user_input > 100))
{
cout << "Invalid input – please try again." << endl;
cout << "Please enter a number between 0 and 100: ";
cin >> user_input;
}
The user is prompted for a number between 0 and 100. If the user enters a valid number, then the
while loop condition will fail (be false) and the code inside the loop body will not execute.
However, if the user entered a number outside the requested range, the while loop condition will
fail and he/she will be told of his/her mistake and will be prompted for a new number. This cycle
will repeat until the user enters a valid number.
It is important to note if a conditional expression never becomes false the loop will repeat
forever. This loop behavior is called an infinite loop. It appears to the user as a non-responsive
or “hanging” program.
PALMS MODULE 2 LAB: WHILE LOOPS IN C++ 4
PALMS for CS1 v4.0
Watch and listen to the While Loops in C++ podcast below:
Exercise 1: Create a new C++ Project called CompoundInterest. Add a new C++ code file
called CompoundInterest.cpp to the project, and type the following code into the new file:
/////////////////////////////////////////////////////////////////////////////
//
// Name: CompoundInterest.cpp
// Author: Jeffrey A. Stone
// Purpose: Displays the growth of a bank account, given a starting
// balance and an interest rate, if the interest is compounded.
// Interest rates are input as numbers less than zero (e.g.
// 5.9% is input as 0.059).
//
/////////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <iomanip>
using namespace std;
PALMS MODULE 2 LAB: WHILE LOOPS IN C++ 5
PALMS for CS1 v4.0
/////////////////////////////////////////////////////////////////////////////
//
// FUNCTION NAME: main
// PARAMETERS: None
// RETURN TYPE: int
// PURPOSE: Entry point for the application.
//
/////////////////////////////////////////////////////////////////////////////
int main()
{
double interest_rate = 0.0; // stores the interest rate
double balance = 0.0; // stores the user balance
int year = 0; // time counter (in years)
// prompt the user and input two values
cout << "Enter a starting balance and interest rate, "
<< "separated by a space: ";
cin >> balance >> interest_rate;
// set up the output for two digits of precision...
cout.setf(ios::fixed);
cout.precision(2);
while (year <= 10)
{
// increment the time counter...
year++;
// compute the new balance for this year...
balance = balance + (balance * interest_rate);
// show the balance each year...
cout << setw(4) << year << setw(15) << balance << endl;
}
// return "success" to the operating system...
return 0;
}
Compile and run the program. Remember that interest rates are numbers in the range 0-1: for
example, 7.6% is really 0.076. You should input the 0.076 into this program.
Exercise 2: Examine the CompoundInterest program. How many times does the loop repeat –
in other words, how many times do the statements in the loop body execute? How many times is
the conditional expression evaluated? Note: The answers to these two questions are not the same.
Exercise 3: Perform some research, either in your textbook or on the Web, to determine what the
setw code statement does. What effect does it have in a program?
PALMS MODULE 2 LAB: WHILE LOOPS IN C++ 6
PALMS for CS1 v4.0
Sentinel Values
The conditional expression used to control the number of loop iterations (cycles) often involves
a special value which signals the loop to “stop”. These values are called sentinel values, because
they act as a signal – when these values are encountered the loop should stop. For example:
while (factor != 1000)
{
...
}
The sentinel value in this example is 1000. When the variable factor assumes the value 1000,
the loop will end.
Part 3: Counter-Based Loops
Suppose you wanted to write a function to sum the integers from one (1) up to and including
some higher integer (e.g. 10, 20, etc). You could do this by using a counter variable. A counter
variable is a simply variable (usually an integer) which keeps track of the number of loop
iterations. The counter variable is tested before each iteration for a sentinel value. When the
sentinel value has been reached, the loop terminates. A counter-based while loop to compute a
sum of integers is as follows:
int sumOfIntegers( int terminal_int )
{
int count = 1; // acts as the counter variable
int sum = 0; // stores the sum
while (count <= terminal_int)
{
// add the value of the counter to our sum...
sum = sum + count;
// add one to the counter...
count = count + 1;
}
return sum; // return the summation...
}
The Binomial Coefficient and Factorials
In algebra, the binomial coefficient is symbolized as:
(𝑛
𝑟)
PALMS MODULE 2 LAB: WHILE LOOPS IN C++ 7
PALMS for CS1 v4.0
And is defined by the following formula, given integer values of n and r (both > 0):
𝑛!
𝑟! (𝑛 − 𝑟)!
The exclamation mark (!) means factorial, which is a mathematical concept expressed as
follows: The factorial of a given integer is the product of that integer and all the integers which
precede, down to and including 1. For example:
5! = (5 × 4 × 3 × 2 × 1) = 120
In the case of the binomial coefficient formula, we have three uses of factorial in conjunction
with multiplication and division. Since we are repeating a similar iterative calculation (a
factorial) three times, this problem lends itself to the use of a function.
The use of a function allows us to write one set of code for the calculation that can be called
three times with different arguments. The use of a while loop is warranted here, since the
factorial’s calculation is actually a repeated multiplication.
Suppose we wanted to write a C++ program to compute the value of this coefficient. Our
program would take two inputs at the beginning:
Inputs Data Type Restrictions
Value of n int [1…10]
Value of r int [1…10]
Our program would also need to aware of the following conditions:
When r is equal to n, the value of the binomial coefficient is 1. Note: 0! = 1.
When r is greater than n the formula does not apply and this situation should be
considered an error condition (requiring a suitable message for the end user).
Once the input is finished, our program would calculate the binomial coefficient and display it to
the user. An example dialog between the program and the user would look like the following:
Enter the value of n: 5
Enter the value of r: 3
The binomial coefficient for n=5, r=3 is 10.
PALMS MODULE 2 LAB: WHILE LOOPS IN C++ 8
PALMS for CS1 v4.0
Exercise 4: Create a new C++ Project called Binomial. Add a new C++ code file called
Binomial.cpp to the project, and construct the program described above. Here is some test data
you can use to assess your program:
n r Binomial Coefficient
5 1 5
7 2 21
4 2 6
4 4 1
4 7 <Your Error Message>
Compile, Build, and Test your program to verify it works as expected.
Part 4: The Game of Craps
Suppose we wanted to simulate a game of craps, a game of chance involving dice. The rules of
craps are as follows:
Craps is played by a single player.
The player rolls two die (each a fair die, containing faces 1, 2, 3, 4, 5, and 6).
The sum of each roll is what's important. Rolling a 7 or an 11 on the first throw of the
dice results in a "win" for the player.
If the sum of the dice is 2, 3, or 12 on the first throw, the player "loses".
If the player doesn’t "win" or "lose" on the first throw, the player has the option to
continue rolling the dice.
o The sum of the dice from the first throw becomes the players' "point" value. The
goal is to roll the dice until the "point" value is rolled again (a "win", i.e. "making
your point").
o If a 7 is rolled before the "point" value is rolled again, the player "loses".
Simulating this game would necessarily involve a conditional repetition structure, as the process
of rolling the dice repeats some number of times.
Exercise 5: Create a C++ program to simulate a game of craps and keep track of how long it
takes a user to "win" or "lose". Your program should begin by rolling the dice; if the player wins,
display an appropriate message to the user, something like:
Congratulations, you win!
If the player does not win, display an appropriate message indicating a loss. If the player neither
wins nor loses, ask the user if he/she wishes to continue:
PALMS MODULE 2 LAB: WHILE LOOPS IN C++ 9
PALMS for CS1 v4.0
Continue (Y/N)?
If the user elects to continue, roll the dice again. Repeat this process (rolling the dice, checking
for a win/loss, and asking the user to continue) until the user wins or loses. Regardless of the
outcome, display a count of the number of rolls that have occurred. To simulate a die roll you
can use the rollDie() function, as shown below. You will need to write this function into your
code:
/////////////////////////////////////////////////////////////////////////////
//
// FUNCTION NAME: rollDie
// PARAMETERS: None
// RETURN TYPE: int
// PURPOSE: Returns a random integer in the range [1…6].
//
////////////////////////////////////////////////////////////////////////////
int rollDie()
{
static bool initialized = false;
if (!initialized)
{
// Set the seed value for generating random numbers.
srand((unsigned)time(0));
// set the flag
initialized = true;
}
// This computes a random integer between low and high using the
// Standard C++ rand() function.
// Get the random number.
int rand_num = rand();
// Convert the number to a number in the range [1..6])…
rand_num = (rand_num % (6 - 1 + 1)) + 1;
// return the integer in the range [low…high]...
return rand_num;
}
PALMS MODULE 2 LAB: WHILE LOOPS IN C++ 10
PALMS for CS1 v4.0
Part 4: Amortization, Input Validation, and While Loops
Suppose we wanted to construct a C++ program to display a payment schedule for a bank loan.
Bank loans are commonly taken to provide for big-ticket purchases like a house or a car. Loan
terms and payment amounts vary based on interest rates, bank promotions, and other factors. A
payment schedule is used to show the distribution of payments over the life of a loan. Each
payment has two components – the amount applied to the principal, or the loan amount, and the
amount paid for interest on the loan. Calculating a loan payment schedule involves a series of
calculations over time; as a result, constructing a payment schedule involves the concept of
conditional repetition.
The program you will write will have four inputs:
Input Data Type Range of Valid Values
Principal double Must be greater than zero
Annual Interest Rate double Must be in the range [1-100]
Term of the Loan int Must be in the range [10-30]
Payments per Year int Must be in the range [1-12]
Each of these inputs requires the user to enter values which fall within a given range. Two
methods are used in combination to restrict the input the user provides. First, the prompt given
by the program should include a description of the “valid” range of acceptable input values.
Second, we can use while loops to force the user to enter in an acceptable value. A while loop
allows the program to check the user input for validity: if the input is invalid, the user can be
informed of his or her error and be given an opportunity to re-enter the value.
For example, the following function would be used to enforce the restrictions on the “principal”
input described above:
PALMS MODULE 2 LAB: WHILE LOOPS IN C++ 11
PALMS for CS1 v4.0
//////////////////////////////////////////////////////////////////////////
//
// FUNCTION NAME: inputPrincipal
// PARAMETERS: None
// RETURN TYPE: double
// PURPOSE: Grabs the loan amount from the user. Restricts the
// input to be > 0.
//
//////////////////////////////////////////////////////////////////////////
double inputPrincipal()
{
bool valid_input = false; // flag value for the input's validity
double input_value = 0; // stores the actual input value
while ( !valid_input )
{
// prompt the user...
cout << "Enter the Beginning Principal (> 0): ";
// read in the value...
cin >> input_value;
// check for validity...
if ( input_value > 0 )
{
// otherwise OK
valid_input = true;
}
else
{
// tell the user and set the flag...
cout << "Invalid input. Please try again." << endl << endl;
valid_input = false;
}
}
// return the validated value...
return input_value;
}
The prompt in this function informs the user of the input restrictions (> 0). Note also how a
bool variable (valid_input) is used to control the execution of the while loop. This approach
uses the bool variable as a so-called flag variable; if the user enters an invalid value, a “red flag”
is raised and the user is forced to try again. The approach used in this function guarantees that
the return value provide by the function is valid for the principal (i.e. not garbage).
Exercise 6: Create a new C++ Project called Amortization. Add a new C++ code file called
Amortization.cpp to the project. Construct four functions, one for each of the input values
described above. Each function should use descriptive prompts and a while loop to force the user
to enter valid values. Finally, add code to your main function which calls these four input
PALMS MODULE 2 LAB: WHILE LOOPS IN C++ 12
PALMS for CS1 v4.0
functions and records the return values. Compile and Build your program, and test it out. Try to
enter invalid values and see what happens – does it force you to enter in valid values?
Exercise 7: Enhance your Amortization.cpp file by adding a function called
computeAmortizedPayment. The purpose of this function is to compute the individual payment
amount for the loan (for example, a monthly payment). The formula to compute the amortized
payment is as follows:
𝐴 =𝑝 ∗ 𝑖 ∗ (1 + 𝑖)𝑛
(1 + 𝑖)𝑛 − 1
Where p is the principal, i is the [ (annual interest rate / 100.0) / payments per year ], and n is the
total number of payments for the loan (i.e. term of the loan * payments per year). After
constructing this function, add code to your main function which calls
computeAmortizedPayment with the four necessary arguments and records the return value. The
function should have the following prototype and comment header:
double computeAmortizedPayment( double p, double ir, double ppy, int n);
//////////////////////////////////////////////////////////////////////////
//
// FUNCTION NAME: computeAmortizedPayment
// PARAMETERS: double p -- the beginning principal
// double ir -- the annual interest rate
// int ppy -- the payments per year
// int n -- the total number of payments
// RETURN TYPE: double
// PURPOSE: Computes and returns the amortized payment amount.
//
//////////////////////////////////////////////////////////////////////////
Exercise 8: Enhance your Amortization.cpp file by adding a function with the following
function prototype and comment header:
void printHeaderInformation(double princ, double ir, double amort, int n);
//////////////////////////////////////////////////////////////////////////
//
// FUNCTION NAME: printHeaderInformation
// PARAMETERS: double princ -- the beginning principal
// double ir -- the annual interest rate
// double amort -- the amortized payment
// int n -- the total number of payments
// RETURN TYPE: void
// PURPOSE: Prints the header information to the screen.
//
//////////////////////////////////////////////////////////////////////////
PALMS MODULE 2 LAB: WHILE LOOPS IN C++ 13
PALMS for CS1 v4.0
The purpose of this function is to display summary information at the top of your eventual
payment schedule (a report “header” of sorts). The screen output should be formatted as follows:
AMORTIZATION SCHEDULE
Beginning Principal: NNNNNN.NN
Annual Interest Rate: NN.NN%
Total Payments: NNN
Payment Amount: NNN.NN
Payment# Interest Due Principal Balance Remaining
The use of “N” indicates that, in the actual output, a number should appear in that position. The
number of “N”s after a decimal point indicates the desired precision of the output. For example,
there should be two digits to the right of the decimal for the principal amount, annual interest
rate, and payment amount. Finally, add code to your main function which calls
printHeaderInformation with the four necessary arguments. Compile and Build your
program and test it out. Hint: use cout.precision() and setw() to format your output.
Exercise 9: Enhance your Amortization.cpp file by adding the following function to the end of
your file (don’t forget to add the function prototype!):
/////////////////////////////////////////////////////////////////////////////
//
//
// FUNCTION NAME: computeInterestPortion
// PARAMETERS: double remaining_balance -- the loan balance
// double ir -- the annual interest rate
// int ppy -- the payments per year
// RETURN TYPE: double
// PURPOSE: Computes and returns the interest portion for the
// current payment.
//
/////////////////////////////////////////////////////////////////////////////
double computeInterestPortion(double remaining_balance, double ir, int ppy)
{
// compute the interest portion of the payment...
return (remaining_balance * (ir / 100.0)) / ppy;
}
The purpose of this function is to compute the interest portion of each individual payment. This
amount fluctuates based on the remaining balance for the loan. The formula used as the basis of
this function is as follows:
PALMS MODULE 2 LAB: WHILE LOOPS IN C++ 14
PALMS for CS1 v4.0
(current balance) ∗ [(annual interest rate / 100.0)]
(payments per year)
This formula computes the interest amount present in any payment. Compile your file to make
sure there are no errors. You will not add code to your main function to test this function, but it
will be used in the next exercise.
Exercise 10: Enhance your Amortization.cpp file by adding a function called
computeAndPrintRows. The purpose of this function is to compute (and display) the payment
schedule for the loan. Recall that in Exercise 8 you produced a “report header” to the screen. The
last line of this header was as follows:
Payment# Interest Due Principal Balance Remaining
These four items are column headings for the report which follows. Your
computeAndPrintRows function must produce the rows which follow – in other words, the lines
of the report consisting of the four column values. There will be one row per payment. For
example, if the loan was for 10 years, 12 payments per year, there would be 120 rows of data in
your table (10 years * 12 payments per year).
The computeAndPrintRows function should have the following prototype and comment header:
void computeAndPrintRows(double p, double ir, double amort, int n, int ppy);
/////////////////////////////////////////////////////////////////////////////
//
//
// FUNCTION NAME: computeAndPrintRows
// PARAMETERS: double p -- the beginning principal
// double ir -- the annual interest rate
// double amort -- the amortized payment
// int n -- the total number of payments
// int ppy -- the payments per year
// RETURN TYPE: void
// PURPOSE: Computes the row values and prints them to the screen.
//
/////////////////////////////////////////////////////////////////////////////
Each row will show the payments’ interest amount, principal amount, and the balance remaining
on the loan. This will require a while loop which “ends” with the last payment. Each iteration of
the while loop will compute the interest due (using the computeInterestPortion function), the
principal amount provided by the payment (i.e. the amortized payment amount minus the interest
due) and the remaining balance (the prior balance minus the principal amount paid with this
specific payment).
PALMS MODULE 2 LAB: WHILE LOOPS IN C++ 15
PALMS for CS1 v4.0
Here is an example of what a sample report would look like for a small loan:
AMORTIZATION SCHEDULE
Beginning Principal: 150750.00
Annual Interest Rate: 7.38%
Total Payments: 30
Payment Amount: 12615.39
Payment# Interest Due Principal Balance Remaining
1 11125.35 1490.04 149259.96
2 11015.38 1600.01 147659.95
3 10897.30 1718.09 145941.86
4 10770.51 1844.88 144096.98
5 10634.36 1981.04 142115.94
6 10488.16 2127.24 139988.70
7 10331.17 2284.23 137704.48
8 10162.59 2452.80 135251.67
9 9981.57 2633.82 132617.86
10 9787.20 2828.20 129789.66
11 9578.48 3036.92 126752.74
12 9354.35 3261.04 123491.70
13 9113.69 3501.71 119990.00
14 8855.26 3760.13 116229.87
15 8577.76 4037.63 112192.24
16 8279.79 4335.61 107856.63
17 7959.82 4655.57 103201.06
18 7616.24 4999.15 98201.90
19 7247.30 5368.09 92833.81
20 6851.14 5764.26 87069.55
21 6425.73 6189.66 80879.89
22 5968.94 6646.46 74233.44
23 5478.43 7136.97 67096.47
24 4951.72 7663.67 59432.80
25 4386.14 8229.25 51203.55
26 3778.82 8836.57 42366.97
27 3126.68 9488.71 32878.26
28 2426.42 10188.98 22689.29
29 1674.47 10940.92 11748.36
30 867.03 11748.36 0.00
Total Interest: 227711.79
Note that, at the end of the report, the total amount of interest paid is displayed. You will need to
keep track of this in your while loop! Finally, add code to your main function which calls
computeAndPrintRows with the five necessary arguments. Compile, Build, and test your
program to make sure it works! Note: there are many online “mortgage calculators” you can use
to test your results.
PALMS MODULE 2 LAB: WHILE LOOPS IN C++ 16
PALMS for CS1 v4.0
Part 5: Projectile Motion
The motion of a projectile follows an arc trajectory, unless the starting velocity is large enough
to be considered an escape velocity (a velocity that allows a projectile to defy gravity and leave
the earth). For example, the path of a baseball once it leaves the bat follows an arc, beginning at
the bat and ending when it hits the ground.
On the other hand, NASA launches its space shuttles at a high enough velocity to outrun the pull
of the earth’s gravity. The height s of the projectile at time t with initial velocity v0 is calculated
by the formula
𝑠 = 𝑣0𝑡 −1
2𝑔𝑡2
Where g is the gravitational constant (9.8 m/s2).
Exercise 11: Write a C++ program to create a table of height vs. time for a given projectile.
Allow the user to enter an initial velocity in m/s (v0) and calculate the height of the projectile for
time t = 0, 1, 2, 3, …, n-1, n seconds. The last entry in your table (n seconds) should come when
the height of the projectile reaches 0.
Note that the final height may be less than 0; simply report this value as zero in your output.
Remember to break your program down into functions and follow the C++ Coding Guidelines.
Some other notes:
Use four digits of precision for real-number output.
Use the setw tool to properly format your output.
Remember to validate the input (must be > 0).
Create a constant for the gravitational constant (9.8 m/s2).
Four test cases are provided on the following pages.
PALMS MODULE 2 LAB: WHILE LOOPS IN C++ 17
PALMS for CS1 v4.0
Case #1:
Please enter you initial velocity (in m/s): 5
Projectile Height Chart
Initial Velocity: 5.0000 m/s
Time Height
---- ------
0 0.0000
1 0.1000
2 0.0000
Case #2:
Please enter you initial velocity (in m/s): 0.0
Projectile Height Chart
Initial Velocity: 0.0000 m/s
Time Height
---- ------
0 0.0000
Case #3:
Please enter you initial velocity (in m/s): 21.56
Projectile Height Chart
Initial Velocity: 21.5600 m/s
Time Height
---- ------
0 0.0000
1 16.6600
2 23.5200
3 20.5800
4 7.8400
5 0.0000
PALMS MODULE 2 LAB: WHILE LOOPS IN C++ 18
PALMS for CS1 v4.0
Case #4:
Please enter you initial velocity (in m/s): 100.789
Projectile Height Chart
Initial Velocity: 100.7890 m/s
Time Height
---- ------
0 0.0000
1 95.8890
2 181.9780
3 258.2670
4 324.7560
5 381.4450
6 428.3340
7 465.4230
8 492.7120
9 510.2010
10 517.8900
11 515.7790
12 503.8680
13 482.1570
14 450.6460
15 409.3350
16 358.2240
17 297.3130
18 226.6020
19 146.0910
20 55.7800
21 0.0000
PALMS MODULE 2 LAB: WHILE LOOPS IN C++ 19
PALMS for CS1 v4.0
Part 5: Vocabulary Words
You will be assessed on your knowledge of the following terms and concepts as well as the C++
concepts discussed in this document. As a result, you are encouraged to review these terms in the
preceding document.
Boolean – true or false.
Conditions/Conditional Expressions – expressions which evaluate to one of two possible
values: true or false.
Conditional repetition – the repeated execution of a statement or a sequence of statements.
Counter Variable – A variable (usually an integer) which keeps track of the number of loop
iterations.
Infinite Loop – Occurs when a conditional expression never becomes false, causing a loop to
repeat forever. It appears to the user as a non-responsive or “hanging” program..
Iterations – the “cycles” of a loop; in other words, the number of times the loop body is
repeated.
Loop –In C++, a construct that repeats a series of statements.
Loop Body – the portion of a loop inside { } .
Sentinel – A value which signals a loop to “stop”.
While loops – A basic C++ loop construct; compound statements which execute a statement or a
series of statements as long as a specified condition is true.