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

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 
  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

Other related posts: