convert for loop to while loop

Hi can anyone help me on how to convert this to while loop, any help would be appeciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>

using namespace std;

int main()
{
	for (int i = 1; i <= 10; i++)
	{
		for (int j = 1; j <= i; j++)
			cout << "*";
	
		for (int j = i + 1; j <= 10; j++)
			cout << " ";
		cout << " ";
		for (int j = 1; j <= (11 - i); j++)
			cout << "*";
		for (int j = (11 - i) + 1; j <= 10; j++)
			cout << " ";
		cout << " ";
		for (int j = 1; j < i; j++)
			cout << " ";
		for (int j = i; j <= 10; j++)
			cout << "*";
		cout << " ";
		for (int j = 1; j < (11 - i); j++)
			cout << " ";
		for (int j = (11 - i); j <= 10; j++)
			cout << "*";
		cout << endl;
	}
	cout << endl;
}

Last edited on
For loops and while loops are basically the same thing.
Aside from some oddities with break and continue, you can change one to the other simply.

A for loop has this structure.
1
2
3
for ( a ; b ; c ) {
  d;
}


When you've identified what a,b,c,d are in your for loop, you can write
1
2
3
4
5
a;
while ( b ) {
  d;
  c;
}


Eg.
1
2
3
4
5
6
7
8
9
10
for ( int i = 0 ; i < 10 ; i++ ) {
  cout << i;
}

// becomes
int i = 0;
while ( i < 10 ) {
  cout << i;
  i++;
}


Hello ethan313,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



salem chas a good example and here is another option:
1
2
3
4
5
6
7
8
int maxRows{ 10 };

while (maxRows)
{
    // Other while loops.

    maxRows--;
}

This would be for the outer most for loop.

By starting the loop counter, i.e., (maxRows), at 10 and subtracting each time through the loop when you reach (0)zero this is considered "false" and the while condition fails.

For your for loops C++ is zero based and you for loops would normally start at (0)zero. If you get use to using (<=) in the condition eventually your for loop will have to start at (0)zero and the (<=) will loop 1 more time than you want meaning that you would be going past the end of an array, vector, or list or some other container which usually causes the program to crash.

Just as an example this is your code made more readable:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>

using namespace std;  // <--- Best not to use.

int main()
{
    for (int i = 1; i <= 10; i++)
    {
        for (int j = 1; j <= i; j++)
            cout << "*";

        for (int j = i + 1; j <= 10; j++)
            cout << " ";

        cout << " ";

        for (int j = 1; j <= (11 - i); j++)
            cout << "*";

        for (int j = (11 - i) + 1; j <= 10; j++)
            cout << " ";

        cout << " ";

        for (int j = 1; j < i; j++)
            cout << " ";

        for (int j = i; j <= 10; j++)
            cout << "*";

        cout << " ";

        for (int j = 1; j < (11 - i); j++)
            cout << " ";

        for (int j = (11 - i); j <= 10; j++)
            cout << "*";

        cout << endl;
    }

    cout << endl;

    return 0;  // <--- Not required, but makes a good break point for testing.
}

A few blank lines to break up the code and you can see what you are doing. It really helps both of us.

Andy
How to convert this into a while loop

1
2
for (int j = (11 - i) + 1; j <= 10; j++)
			cout << " ";
> How to convert this into a while loop
Read post https://www.cplusplus.com/forum/beginner/278586/#msg1202763 again.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;

int main()
{
   const int N = 10, NN = 2 * N + 1;
   string line = string( 1 + N, ' ' ) + string( NN, '*' ) + string( N + 1, ' ' ) + '\n';
   int i = -1;
   while ( ++i < N )
   {
      line[i] = line[2*NN-i] = '*';
      line[NN-i] = line[NN+i] = ' ';
      cout << line;
   }
}


*          ********** **********          *
**         *********   *********         **
***        ********     ********        ***
****       *******       *******       ****
*****      ******         ******      *****
******     *****           *****     ******
*******    ****             ****    *******
********   ***               ***   ********
*********  **                 **  *********
********** *                   * **********
Topic archived. No new replies allowed.