RE: How to convert for, statements into while loops?

  • From: "Homme, James" <james.homme@xxxxxxxxxxxx>
  • To: "programmingblind@xxxxxxxxxxxxx" <programmingblind@xxxxxxxxxxxxx>
  • Date: Mon, 19 Jul 2010 07:26:01 -0400

Hi Jess,
The assignment wants you to turn the for loop into a do while loop, not a while 
loop. Do you understand the difference between a do while loop and a while loop?

Thanks.

Jim

Jim Homme,
Usability Services,
Phone: 412-544-1810. Skype: jim.homme
Internal recipients,  Read my accessibility 
blog<http://mysites.highmark.com/personal/lidikki/Blog/default.aspx>. Discuss 
accessibility 
here<http://collaborate.highmark.com/COP/technical/accessibility/default.aspx>. 
Accessibility Wiki: Breaking news and accessibility 
advice<http://collaborate.highmark.com/COP/technical/accessibility/Accessibility%20Wiki/Forms/AllPages.aspx>

From: programmingblind-bounce@xxxxxxxxxxxxx 
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Jes
Sent: Sunday, July 18, 2010 11:45 PM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: How to convert for, statements into while loops?

Hi all,
Here is the exact assignment from the text book. I'm just trying to follow what 
the book assigns.
***Assignment**

In this lab, you will modify the program you created in Lab 8.2. The modified 
program will use a posttest loop (rather than a pretest loop) to display the 
multiplication tables.
Currently, the program uses a for statement to display the multiplication 
tables. Your task is to modify the program so that it uses a do while statement 
rather than a for statement. Make the appropriate modifications to the program.

Jes

On Jul 18, 2010, at 10:29 PM, Dale Leavens wrote:


Usually you don't want to do that. The reason is that they are intended for 
different purposes. A "for" loop is intended where an operation is to be 
performed a known number of times. If that number is to be changed by some 
other aspect of the programme, for example if you wanted to work with a 
multiplicand and another selected number of multipliers then you would input a 
value for the last value, the 10, in your present "for" loop and in that way 
alter the number of multiplications but it is still a known number of 
operations.

A while loop is intended for use while a given condition is true. In this case 
while you want to continue entering numbers to multiply.

You could of course still make a "while" loop achieve this with a statement 
like:

while i < 11 and initializing i to 0 on entry to the loop but this is pretty 
well pointless.

Now it is a matter of form but you should give your instruction to run or exit 
the programme before entering the "while" loop, something like:

cout << "Press 'Y' to display a multiplication table, any other key to quit. ";
do {
cin >> anotherTable;

In this way the user can choose to abort the programme before it enters the 
loop.

Not clear why you set up your "for" loop like you did.
for (int multiplier = -1; multiplier < 10; multiplier += 1)

Don't you want the multiplier to start at 1 and loop until it multiplies by 10 
then quits? This looks like it begins as a negative 1 through 0 and on up to 
10. I think it should be:

for (int multiplier = 1; multiplier < 11; multiplier++)

Note the double + signs after multiplier. This increments the integer by 1 
after it is used so, after it becomes 10 and the loop executes it becomes 11 
and when the loop reaches the top again it fails.

If you made that a while loop you would have to set multiplier to either 1 
before entering the loop and still have to have a statement incrementing 
multiplier by 1 immediately before the while statement, something like

multiplier++;
while (multiplier <11);

so that multiplier will fail the test when it reaches 11. It must do this at 
the end of the loop.

Alternately you could set multiplier to 0 before entering the loop then 
immediately increment it to 1 before acting on it. You have to be certain 
though that it will be reset to 0 or 1 depending on your choice every time 
before the loop is executed.

The "for" loop is the correct choice in that circumstance just as the while 
loop is the correct choice for repeating the sequence because there is no 
reasonable way to determine in advance if the user wants to continue inputting 
numbers or how many times.

I hope this clarifies this for you.

Some languages also have a do -- until loop construct.

Dale Leavens.




----- Original Message -----
From: Jes<mailto:theeternalkid@xxxxxxxxx>
To: programmingblind@xxxxxxxxxxxxx<mailto:programmingblind@xxxxxxxxxxxxx>
Sent: Sunday, July 18, 2010 9:38 PM
Subject: How to convert for, statements into while loops?

Hi all,
I got my program to work correctly, but now I want to learn how to convert the 
pre test loop, the for statement, into a while loop.
***code**
//Ch8lab2.cpp
//Displays one or more multiplication tables
//Created/revised by Jes Smith on July 18, 2010

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

int main()
{
//declare variables
char anotherTable = ' ';
int multiplicand = 0;
int product = 0;

do
{
//get the multiplicand
cout << "Enter the multiplicand: ";
cin >> multiplicand;

//display the multiplication table
for (int multiplier = -1; multiplier < 10; multiplier += 1)
{
product = multiplicand * multiplier;
cout << multiplicand << " * " << multiplier
<< " = " << product << endl;
} //end for

cout << endl;
cout << "Display another multiplication table? (Y/N) ";
cin >> anotherTable;
} while (toupper(anotherTable) == 'Y');
return 0;

} //end of function


________________________________
This e-mail and any attachments to it are confidential and are intended solely 
for use of the individual or entity to whom they are addressed. If you have 
received this e-mail in error, please notify the sender immediately and then 
delete it. If you are not the intended recipient, you must not keep, use, 
disclose, copy or distribute this e-mail without the author's prior permission. 
The views expressed in this e-mail message do not necessarily represent the 
views of Highmark Inc., its subsidiaries, or affiliates.

Other related posts: