How do I change if/else if statements into a loop?

I cannot figure out how to change the following code into a loop. This is just a portion of a larger code where n is defined by user input. How do I create a loop that adds one "#" to each iteration of a loop?

``````for(int i = 0; i < n; i++)
{
if(i == 0)
{
printf("#\n");
}
else if(i == 1)
{
printf("##\n");
}
else if(i == 2)
{
printf("###\n");
}
else if(i == 3)
{
printf("####\n");
}
else
{
printf("#####\n");
}
}
``````

You are already half-way there. Your for loop does something for each iteration. Now, you want to do something inside that loop for each iteration. The answer is another for loop--a concept called "nested loops." The basic structure would be:

``````for(int i = 0; i < n; i++)
{
//code to execute before the nested loop, if any

//the nested loop
for(int j = 0; j < i; j++)
{
//code you want to execute in the nested loop
}

//code to execute after the nested loop, if any
}
``````

Because the nested loop's condition will only execute while `j < i`, you can use this structure to cause something to happen set number of time for each iteration of the outer loop that is also dependent on how far in the outer loops life (that is how far into its iterations it is). Nested loops are discussed early in the CS50 lectures and there are numerous YouTube videos and online tutorials/articles that go in depth on how they work. I also went over nested loops in an answer in this quesion to help someone else out, which might be of help.

Hopefully, this helps point you in the right direction. If it does, feel free to click the check mark next to my answer to accept it. But if not, let me know and I will try to help some more.