Asterisks

I am doing homework, but I am lost on while statements. I know this is currently if for/if, but every time I try to switch it up to while, I get confused and severely lost. I am not trying to get anyone to do my homework for me. I just want to understand how to correctly use while and do while and have it work.

#include<iostream>
using namespace std;

int main(){

int x,y;
char star = '*';
int temp;


for(x=1; x <= 11; x++)
{

if((x%11) != 0)
{

for(y=2; y <= x ; y++)
{

cout << star;

}


cout << endl;

}

}
system ("pause");
return 0;
}
the main thing to remember with while and do-while loops that is different that for loops is that they do not contain the logic that updates the test condition in them (like the x++ in your first for loop). While and do-while are usually used when the test condition is a product of the normal program flow, whereas for loops usually use conditions build specifically for use in the loop.

While and do-while are more useful when you don't know ahead of time when you will need to exit the loop. Consider a loop that reads characters from a text file. You probably won't know how big the file is, so you just keep reading until you hit it.
1
2
3
4
while( !myFile.eof() )
{
     // Read the next character from the file
}



to use a while loop for the example you're using, you have to set up the condition manually outside of the loop first, and increment your test value inside the loop.
1
2
3
4
5
6
x=1;
while( x <= 11 )
{
     // do stuff
     x++;
}
I think I understand, what you are stating and when I did the initial run with just the first while statement it came out as it was supposed too. I then set up the y statement to look like the x statement. When I debug and run it shows a single asterisk.

#include<iostream>
using namespace std;

int main()
{

int x,y;
char star = '*';


x=1;
while (x <= 11)
{
x++;
}
y=2;
while (y <= x)
{
y++;
}
{

cout << star;

}


cout << endl;




system ("pause");
return 0;
}
Properly formatting and indenting your code helps to visualize what's going on. Also, use code tags when posting code samples: http://www.cplusplus.com/articles/jEywvCM9/

Here's your initial code with for loops reformatted to nest loops:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main(){
	int x,y;
	char star = '*';
	int temp;

	for(x=1; x <= 11; x++)
	{
		if((x%11) != 0)
		{
			for(y=2; y <= x ; y++)
			{
				cout << star;
			}

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

Notice that it's much easier to see how the for(y) loop is nested inside the for(x) loop.
Now your 'while' example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main()
{
	int x,y;
	char star = '*';

	x=1;
	while (x <= 11)
	{
		x++;
	}
	y=2;
	while (y <= x)
	{
		y++;
	}
	{
		cout << star;
	}

	cout << endl;

	system ("pause");
	return 0;
}

notice that your while( y <= x ) loop is not nested within the while( x <= 11 ) loop.
Also, your "cout << star;" line is outside of either loop, which is why it only executes once.
Last edited on
No, I do not see it. I am trying, but I do not see how to nest y.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
	int x,y;
	char star = '*';

	x=1;
	while (x <= 11)
	{
		y=2;
		while (y <= x)
		{
			cout << star;
			y++;
		}
		x++;
	}

	cout << endl;

	system ("pause");
	return 0;
}
right when I enter for the nesting I either get * or I get ***********. I am trying to get this:

************
***********
**********
*********
********
*******
******
*****
****
***
**
*
Adding "cout << endl;" after the "while (y <= x )" loop produces the opposite result:


*
**
***
****
*****
******
*******
********
*********
**********
***********
************

to get it to reverse, change the X loop to count down from 10 to 1, rather than from 1 to 10.
Last edited on
Topic archived. No new replies allowed.