Question on Nested Loop (C++) :: Forum

Start Date Last Update Igniter Last Update By Status
7:00pm on Thursday 11th July 2013 11 years ago

HTHVampire

HTHVampire




HTHVampire

#include <iostream>
using namespace std;

int main()
{
int i;
int a;
int p;

i=4;
while (( i <=4 ) && ( i > 0 ))
{
int j;
j = 1;
while ( j <= i )
{
cout << ".";
j = j +1;
}
p=1;
while ( p <= 7)
{
int q;
q=1;
while ( q <= p)
{
cout << "*";
q=q+1;
}
cout << endl;
p=p+2;
}

i = i -1;
}
cout << "*" << endl;
a=1;
while (( a <= 4 ) && ( a > 0 ))
{
int b;
b = 1;
while ( b <= a )
{
cout << ".";
b = b + 1;
}
cout << "*";
cout << endl;
a=a+1;
}
return 0;
}

Above is my code in order to create the pattern as below:


or a better view of my code:

http://codepad.org/PIkrSKms

Please ignore the code starting from line 36 onwards. May I know that how can I "escape" the while loop in line 25 so that it will proceed to i=i-1?

Thanks for your guides!

 





suzzett

If you are looking to break out of while loop or any kind of loop before the condition you can use either break or continue statement. I have talked about it in my video on looping.

To generate the given pattern, one nested loop will be enough.

If you are looking to paste a code like how I have changed yours above you can click on source in the editor and enclose your code like below:

<pre>
<code class="cpp">



your code here...

....................

 

</code>
</pre>





suzzett

You can now actually just use the code function in toolbar to post a code here.

By the way, did it work? Your code?




HTHVampire



#include
using namespace std;

int main()
{
int i;
int a;
int p;

i=4;
while (( i <=4 ) && ( i > 0 ))
{
int j;
j = 1;
while ( j <= i )
{
cout << ".";
j = j +1;
}
p=1;
while ( p <= 7)
{
int q;
q=1;
while ( q <= p)
{
cout << "*";
q=q+1;
}
cout << endl;
p=p+2;
}

i = i -1;
}
cout << "*" << endl;
a=1;
while (( a <= 4 ) && ( a > 0 ))
{
int b;
b = 1;
while ( b <= a )
{
cout << ".";
b = b + 1;
}
cout << "*";
cout << endl;
a=a+1;
}
return 0;
}




Hi, thanks for the reply! May I know that what did you meant by:
"If you are looking to paste a code like how I have changed yours above"

Thanks!



suzzett

Just meant to say that there was a bug in pasting the code here. But I have fixed it. I see you have already been abble to put the code properly in here. Thats exactly what I meant. Sorry about about the confusion. Did you got the answer to your programming question?




HTHVampire

Hi,

Sorry for the late reply, I think I have solved the problem above, Thanks suzzett!


Please login to post your reply!
 

Login

Username:
Password:
Don't have an account?
Register here
Register

Subscribe Youtube



Network DigitalStage.org


Some interesting stuffs