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

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

Hi James,

That was better than any of my TA's. Jess, hope you get it this time.

Cheers,

Joseph P.S. Thinking about if I should write test program according to jess'
words.

 

From: programmingblind-bounce@xxxxxxxxxxxxx
[mailto:programmingblind-bounce@xxxxxxxxxxxxx] On Behalf Of Homme, James
Sent: Monday, July 19, 2010 8:29 AM
To: programmingblind@xxxxxxxxxxxxx
Subject: RE: How to convert for, statements into while loops?

 

Hi Jess,

A do while loop always runs once no matter what. A while loop may or may not
run once. This is because a do while loop runs first, then tests to see if
it should run again. A while loop tests before it tries to run. The same
with a for loop. 

 

So when your assignment said that it wanted you to use a "test after" loop,
that was your hint to use a do while loop. 

 

 

When you decide what kind of loop you want to write, unless you are doing
your assignment, it's often a matter of preference. When you want to make
sure your loops work, though, you need to think about what' is going to
happen right before they run the first time. This is why I happen to like
for loops the best.

 

I think that a simple example will explain better what I'm trying to say.
I'll comment the code and put in other comments to try to explain what I'm
talking about. I'm one of these people who likees lots of words, so please
forgive me if I'm verbose.

 

Please forgive me if I get the syntax wrong. I'm not at all a c++
programmer.

 

First, we'll use a for loop.

 

In the for loop, we want to do the following.

 

Set up a condition for the loop to start so that it is forced to run at
least once.

 Set up the test so that the loop can check to see if it's done.

Increment a counter so that we can eventually be done. 

Write a body for the loop so that we process whatever we are doing.

 

Here's the code.

 

int iCount; // Create the variable, but don't put anything in it yet. 

// We could also create the variable in the start of the loop, 

// But I wanted to make it easier to read.

 

for (iCount = 1; iCount <= 10; iCount++) { // All the stuff on one line.

                cout iCount; // Prove we're in the loop doing something.

} // End of the loop

 

I like for loops best because you can see the starting expression, the test,
and the increment all on one line. I think it's easier to read than the
other loops.

 

OK. Now, let's take the while loop.

 

Remember, we have to do all of the following.

 

Set up a condition for the loop to start so that it is forced to run at
least once.

 set up the test so that the loop can check to see if it's done.

Increment a counter so that we can eventually be done. 

Write a body for the loop so that we process whatever we are doing.

 

In the case of the while loop, though, the code is a little more spread out,
and I think that makes it harder to read, but here again, it's what I happen
to feel. You decide. Here's the code.

 

int iCount; // Create the variable.

iCount = 1; // We have to set up the test before the loop starts.

 

// Once again, I could have done this on one line, but I wanted to make it
easier to see.

 

while (iCount <= 10) { // The test to see if we're done

                cout iCount; // To show that we're in the loop now.

                iCount ++ // Move toward the time when the test will be true

} // End of the loop

 

 

See how it's spread out? It's easy to read everything with a small loop like
this, but what if this loop was a screen full of text?

 

OK. Now, for the do while loop.

 

Note that the things we have to do are in a different order this time. Here
they are. And note especially the first thing that happens.

 

The loop always runs once no matter what. 

Write a body for the loop so that we process whatever we are doing.

Increment a counter so that we can eventually be done. 

set up the test so that the loop can check to see if it's done.

 

int iCount = 1; 

 

// This time I did the variable creation and assignment all on one line.

// You should get used to doing it this way, because that's what the pros
do.

 

do { // It will run no matter what at least once.

                cout iCount; // Prove that we are running.

                iCount++; // Move toward the time when we will be done

} while(iCount <= 10); // The test is all the way down here to see if we're
done.

 

Now, I have a confession to make. I lied. You can set up a for loop and a
while loop so that they never run. Here's how. Take the code above and set
iCount to 11. But if you do that same thing to the do while loop, iCount
will display even though the condition is not the way you want it. That's
because the test is at the end of the loop.

 

I hope that this was somewhat clear.

 

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: Monday, July 19, 2010 10: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/19/10
06:36:00

Other related posts: