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

  • From: "Joseph Lee" <joseph.lee22590@xxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Mon, 19 Jul 2010 07:33:53 -0700

Hi,

From what I found, a while loop will run once if a condition is true. Next
time around, if the condition is false, it will exit. A do-while loop will
do its thing until the ending condition is reached. For instance, a program
that asks a user for a name and a salary until he or she is done adding
their entries is:

While (user_response == "y")

{

/* Ask the user for the name and salary.

At the end, ask if they wish to continue. If yes, return to the while loop.
If not, exit the loop. */

}

As opposed to this, a number guessing game using do-while loop would be:

Do

{

/* If the number is not right, first decrement (or subtract one) from the
number of guesses permited, then print out the warning that the guess was
not right followed by number of guesses remaining.

If the guess was right, exit the loop. */

} while (guess != right);

In our situation here, the best alternative to "for" loop is doing a
do-while loop.

For more info and a coding example, have a look at:

http://www.blurtit.com/q700863.html

Cheers,

Joseph

 

From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Jes
Sent: Monday, July 19, 2010 7:13 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: Re: How to convert for, statements into while loops?

 

No, I do not understand the difference between a do while loop and a while
loop. Any explanation would be helpful, Jim.

Thanks.

Jes

 

On Jul 19, 2010, at 7:26 AM, Homme, James wrote:





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
<http://mysites.highmark.com/personal/lidikki/Blog/default.aspx>
accessibility blog. Discuss
<http://collaborate.highmark.com/COP/technical/accessibility/default.aspx>
accessibility here.Accessibility
<http://collaborate.highmark.com/COP/technical/accessibility/Accessibility%2
0Wiki/Forms/AllPages.aspx>  Wiki: Breaking news and accessibility advice

 

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

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.

 

No virus found in this incoming message.
Checked by AVG - www.avg.com
Version: 8.5.441 / Virus Database: 271.1.1/3013 - Release Date: 07/18/10
06:35:00

Other related posts: