# Thread: Hello, and a question...

1. ## Hello, and a question...

Hello, everyone. Typically when I join a forum, I post in the "Intro" board, but seeing as this board is missing one, I'd like to introduce myself before asking any questions.

My name is Max, I'm 21, and I've recently taken up learning programming. Although C might not be what I'll be delving into, it is the language used by the course I'm studying (cs50 by Harvard on edx). I've been stuck on a problem for quite some time now, and decided to look at someone else's code in order to better understand what I was missing.

In order to not just copy and paste someone else's answer, I wanted to modify some blocks, but all I could do was change the initialization names. Anything else I tried (that made sense to me) didn't bring forth the desired outcome. So, before proceeding I want to make sure I understand WHY the code is the way it is. Otherwise I'm not learning anything. Here is the code.

Code:
```{
int height = 0;

// asks for input, rejects everything besides 1-23
do
{
printf ("Height of the pyramid: ");
height = GetInt();
}
while (height <= 0 || height > 23);

// checks height to decide how many rows
for (int row = 0; row < height; row++)
{
// creates height-1 spaces
for (int space = height - 2; space > row - 1; space--)
{
printf("%c", ' ');
}

// creates height+2 hashtags
for (int pound = 0; pound < row + 2; pound++)
{
printf("#");
}

printf("\n");
}

return 0;
}```
My question is in relation to int "pound," and the way it interacts the way it does with int "row."

In line
Code:
```for (int pound = 0; pound < height + 2; pound++)
{
printf("#");
}```
I tried replacing the red text with int "height," but the results were adding far too many "#."

ACTUAL QUESTION: Is the reaction the result of this block being a nested loop, rather than a stand alone block? If so, can someone give me basic insight as to how this all works? I'm having a lot of trouble having this make sense to me logically.

P.S. If someone feels up to it, can you please translate this code to plain English? Just like I said above, I'm having a lot of trouble saying it out loud so it makes sense logically. Really new to even TRYING to think like a programmer, let alone understanding all these concepts.

I apologize if I went off on tangents, or rambled a bit (or a lot). Really grateful for any help at this point.

2. Welcome Max. A pencil and paper can go along way to helping you understand what the code is doing.

I would suggest you get a piece of paper and make a list of all variables in the program, along with their initial values. Step through each line of code "by hand" and update the value of the variables on the paper as needed. It would also be a good idea to hand-write the output as you encounter print statements.

It may seem a little tedious, but this is a great way to understand how the code works.

3. ## Explanation of Part 1

I think in the future you should do what Matticus said. In fact, do it any way even if you understand this after reading what I wrote. Personally, I bought myself a whiteboard and I use it ALL THE TIME when I am learning a new programming concept. This helps to save paper, is more fun (IMO), and allows me to quickly and easily focus on specific subjects. I recommend going to a store and getting a decent sized whiteboard. You dont even have to mount it on a wall if you dont want. Plus, the added benefit to this is a lot of employers ask you to "whiteboard" stuff out now adays which will be 2nd nature to you.

So.....

Code:
```int height = 0;
// asks for input, rejects everything besides 1-23
do
{
printf ("Height of the pyramid: ");
height = GetInt();
}
while (height <= 0 || height > 23);```
int height = 0; is creating a new integer variable and initializing it to the value of 0.

After this is a do while loop that is saying "Continuously prompt the user for the height of the pyramid UNTIL the user enters a height which is between 0 and 23."

As soon as a user enters a number between those two numbers, the do while loop will exit and continue to the next set of instructions. It is possible that the do while loop will never actually loop because if the user enters a height between 0 and 23 the first time, it will not satisfy the conditions needed to loop over again.

Code:
```for (int row = 0; row < height; row++)    {
// creates height-1 spaces
for (int space = height - 2; space > row - 1; space--)
{
printf("%c", ' ');
}

// creates height+2 hashtags
for (int pound = 0; pound < row + 2; pound++)
{
printf("#");
}

printf("\n");
}```
I don't feel like me walking you through all of this is necessary and will even help you understand it any better. In fact, if I literally translate it to English like you wanted, this part will sound quite confusing. So this is what you need to know:

• The moral of the story here is that this logic is taking whatever number the user entered above and it's obviously drawing some characters to the screen based upon that number.
• I suggest running the program, entering a number, and looking at the output side-by-side with the code and paying very close attention to the terms in the for loops.
For example:

Code:
`for (int space = height - 2; space > row - 1; space--)`

Stare at this code and then ask yourself "What is space equal to right now? 0. Okay, what is row equal to? Now is space greater than row -1? Ok, if so, then the loop will progress through its next cycle, then it will subtract 1 from space and check the condition again... It will keep repeating the code between the {}(looping) until space is no longer greater than row. In reality, this could mean that space EQUALS row - 1, or it could mean that space is LESS than row - 1. If space is either equal to OR less than row - 1, this means that space is NOT greater than row - 1, thus the loop stops executing. Btw I would write it like (row - 1) to avoid confusion in the code.
The bigger picture here is that this program uses nested for loops, and what that means is that for every single iteration (0 - n times, n being the number entered by the user earlier) of the external (first) for loop, the inner loops will also execute all of their cycles. Keep in mind that in programming in general, it is possible for the inner loops to NOT execute if their conditions are not met.

So on cycle one of the outer loop, the inner loop will cycle a bunch of times, then the 2nd inner loop will cycle a bunch more times, now the outer loop will move to the next cycle and the inner loops will cycle a bunch of times again. If you are still totally lost after doing both suggestions, then get a debugger and put some breakpoints on the loops, then step through the entire program and watch where the execution goes. This will literally show you step-by-step what is going on in a way that will be almost impossible to not understand. CodeBlocks can do this, or you could use the gdb debugger. If you dont know anything about this, get on YouTube and find some C debugging tutorials.

4. Thank you very much to the both of you.

In regards to the first reply, I was about to go out and buy a journal for this problem (and other problems thereafter).
However, on my way out, I told the guys at the cowork space I frequent I'd be back, one of them asked where I was going and I told him. He gave me a spare composition notebook he had.
It all started to make more sense as I wrote, and rewrote the code. I also wrote some parts block-by-block, and I'm beginning to get a better grasp on these concepts.

Thank you eternally for the second reply, it really helped tie everything together for me when I was writing everything.

Per the suggestion of someone at the office, I am currently writing random for loops, and nested for loops to be more comfortable with them in the future.

Thanks again, to both of you.