Help with while loops

I'm creating a simple program to output characters in a certain way using while loops. Im very new to programing so this is supposed to teach me how to use loops. Ive got the first set of loops working but i don't know how to make the secondd part of the output work. Whenever i make another set of loops it just goes below my other set of parentheses. Any help would be appreciated thank you! Ive got this

(
((
(((
((((
(((((
((((((
(((((((
((((((((
(((((((((
((((((((((

However I need:
()
(())
((()))
(((())))
((((()))))
(((((())))))
((((((()))))))
(((((((())))))))
((((((((()))))))))
(((((((((())))))))))

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main() {
        int i = 0;
        while(i <= 10 ) {    // controls columns
            int n = 0;
            while(n < i) {    //controls rows
                cout << "(";
                n++;
            }
        cout << '\n';
        i++;
        }



}
Last edited on
I think this needs to be done as two separate while loops within a larger loop, rather than nested while-loops.

Reason is that the inner loop always executes in full, before resuming where the outer-loop left off. The kind of structure you want is difficult (probably impossible) to do with nested loops.

By the way, in your code, you are never even printing a closing parenthesis.

See if you can understand why the code below works:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main() 
{
    int n = 1;
    do
    {
        int i = 0, j = 0; // i and j always reset to 0 after each iteration of do-while
        while(i<n)  // keep printing '(' until i is equal to n
        {
            cout << "(";
            i++;
        }
        while(j<n) // keep printing ')' until j is equal to n
        {
            cout << ")";
            j++;
        }
        cout << "\n";  // move to newline for next iteration
        n++;          // increment n so the next iteration will print more parenthesis
    }while(n <= 10); // stop when n is greater than 10
}

Last edited on
Ohh okay i see. My assignment told me to write something using nested while loops but i wasn't sure if i was allowed to use do while loops. This helps though i understand it now thank you @Arslan7041
My assignment told me to write something using nested while loops but i wasn't sure if i was allowed to use do while loops.


You can actually replace the do-while with a while loop and it will give the same output. See here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;

int main() 
{
    int n = 1;
    while(n<=10)
    {
        int i = 0, j = 0; // i and j always reset to 0 after each iteration of do-while
        while(i<n)  // keep printing '(' until i is equal to n
        {
            cout << "(";
            i++;
        }
        while(j<n) // keep printing ')' until j is equal to n
        {
            cout << ")";
            j++;
        }
        cout << "\n";  // move to newline for next iteration
        n++;          // increment n so the next iteration will print more parenthesis
    }
}
Last edited on
You can then compact the code as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main() 
{
    int n = 0; // notice 'n' starts at 0 now, can you see why?
    while(n++<10)
    {
        int i = 0, j = 0; 
        
        while(i++<n) cout << "(";
        while(j++<n) cout << ")";
 
        cout << "\n";  
    }
}


You see, this achieves the same thing but with less lines of code.
okay so the compacted code just uses a shortcut so you don't have to increment the variables each time? Like i++<n is saying while incremented i is less than n print "(" ? or does it increment it in that statement and thats why n can be 0?
The "shortcut" is that instead of doing the incrementing in the body of the loops, I moved them to inside the loop conditions. But exactly the same thing is happening as in the longer code. The speed of the code is not improved in any way, it just looks more compact and clean.

In line 7, post-increment is being used. So n is being compared to 10, and only after the comparison is it being incremented. So when its being compared to 10, n=0, and once it enters the loop, it is incremented to 1. This is why I set n to 0. It achieves the same affect as n starting off as 1 in the loop, then being incremented at the end of the loop (as in the longer code).

Line 11 and 12 are the same. Variables i and j are incremented after the comparison to n. It achieves the same affect as comparing, and then incrementing in the body of the loop.
Last edited on
Topic archived. No new replies allowed.