Karlo Kampić
Karlo Kampić

Reputation: 11

Explanation to a simple C++ code using two while loops

I am in need of help with this simple program, can't quite wrap my head around it. If someone could please explain the steps it takes to get the output it does. (Found it on a website, not mine)

The code is:

#include <iostream>

// Loop between 5 and 1
int main()
{
    int outer = 5;
    while (outer >= 1)
    {
        // loop between inner and 1
        int inner = outer;
        while (inner >= 1)
            std::cout << inner-- << ' ';

        // print a newline at the end of each row
        std::cout << '\n';
        --outer;
    }

    return 0;
}

The output looks like this:

5 4 3 2 1
4 3 2 1
3 2 1 
2 1
1

Upvotes: 0

Views: 684

Answers (5)

BigBrownBear
BigBrownBear

Reputation: 25

In start Outer = 5 then Inner =5 ** Now inner loop executes 5 times and every time it decrements the **inner. After inner loop executes 5 time the outer decrement statement is executed:

--outer;

now outer=4 and again comes to inner loop so inner=4 and again the upper procedure is executed.

Upvotes: 0

typewriter
typewriter

Reputation: 368

#include <iostream>

// Loop between 5 and 1
// The main function is mandatory in c++ it is called when the program is opened.
int main()
{
    int outer = 5;                      // outer is a count for the outer loop
    while (outer >= 1)                  // while this count is greater than one
    {
        // loop between inner and 1
        int inner = outer;              // initiate a loop in the outer loop
        while (inner >= 1)              // this loop prints the numbers per row
            std::cout << inner-- << ' ';// in every iteration the inner is decreased this creates the 5 4 3 2 1 per line

        // print a newline at the end of each row
        std::cout << '\n';              // Near the end of the outer loop there is a newline
        --outer;                        // Outer is decreased by 1.
    }

    return 0;
}

Upvotes: 3

theWiseBro
theWiseBro

Reputation: 1529

  1. Outer while loop runs in order outer = 5,4,3,2,1. --outer decrements the value of outer in each iteration.
  2. The inner loop starts with inner set to outer. That is, say, in the second iterator, value of outer is 4. Then inner loop goes over the values 4,3,2,1.
  3. One thing to understand here is the post-decrement operator. std::cout << inner-- << ' '; this line will first use the value of inner and then decrement it. That is, inner will first be printed and then decremented. For eg. if inner is say 4, it will first print 4 and then the decrement comes into effect. Thus, for the second iterator, the inner loop will print 4 3 2 1.

Upvotes: 0

user
user

Reputation: 7604

You want to print out 5 lines. outer keeps track of how many more lines you have left to print (it starts at 5 and is decremented at the end of the outer while loop.

Then, on each line, you want to print out a decreasing sequence of numbers. This sequence contains 'n' numbers and also starts with n, then goes to n-1, and so on until it reaches 1. Luckily, we have outer already defined, which is basically n. So to generate that sequence, you make a variable inner which starts at n (or outer) and goes down by one (and also gets printed every time in that loop). Once input gets below 1, we stop because we don't want to print 0.

Upvotes: 0

Jacob
Jacob

Reputation: 223

Basically the second while loop subtracts one from inner and prints it out until inner = 1. Then it goes into the other while loop and subtracts one from outer. It loops through the outer loop until outer = 1. Also it would help to put in breakpoints and run through the program with the debugger to see how the variables change, like @t.niese said.

Upvotes: 3

Related Questions