Sorry for the new questions... I've created this program to display a table . How could I write this code using while statements instead of for?

#include <iostream>

using namespace std;

int main()
{
	for (int x = 1; x < 4; x++ )
	{ 
		cout << x << " ";
		for (int y = 0; y < 10; y++)
		{

			cout << x* y << " ";
			

		} //end for

		cout << endl;
	} //end for
	cout << endl;
		system ("pause");
	return 0;


} //end of main function

Recommended Answers

All 4 Replies

start x at 1
while x less than 4
  display x
  start y at 0
  while y less than 10
    display result of x times y followed by a space
    increment y by one
  start a new line
  increment x by 1

You don't need while loop. IMO the forloop looks better and easier to read( most of the time).

This seems so easy but I still cannot get it.. any help to point me in the right direction would be much appreciated. Oh and the reason I have to bother changing it to while loops is just for homework... The for loops make so much more sense in this case.

Thanks!

#include <iostream>

using namespace std; // So we can see cout and endl

int main()
{ 
  int x = 1;  // Don't forget to declare variables
  while ( x < 4 ) 
  { // While x is less than 4
    cout<< x <<endl;
    int y = 0;             // Update x so the condition can be met eventually
	while ( y < 10 )
	{
		cout << x*y << endl;
		y++;
	}
	cout << endl;
	x++;
  }
  cout << endl;
  cin.get();
}
#include <iostream>

using namespace std; // So we can see cout and endl

int main()
{ 
  int x = 1;  // Don't forget to declare variables
  while ( x < 4 ) 
  { // While x is less than 4
    cout<< x << " ";
    int y = 0;             // Update x so the condition can be met eventually
	while ( y < 10 )
	{
		cout << x*y<< " ";
		y++;
	}
	cout << endl;
	x++;
  }
  cout << endl;
  cin.get();
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.