15,882,113 members
See more:
I am currently trying to draw a solid square with (*)'s that is N length by N height using while loops but it returns as just a long line of (*)'s. Any help would be very much appreciated.

(Note: Sorry if this is a very novice question, I have only just begun to learn coding so please don't be too harsh, thank you :)).

What I have tried:

```using System;

public class Program
{
public static void Main()
{
var N = 5;
var i = 0;

while (N*N > i)
{
while (N > i)
{
Console.Write("*");
i = i + 1;
}
Console.Write("\n");
}
}
}```
Posted
Updated 20-Apr-19 23:44pm
v2

## Solution 1

Think about it: can you really use the same variable in both loops?
You want two loops: one for rows, and one for columns:
```while (more rows to do)
set up for a row
while (row not finished)
print a star
print a new line```
N will remain the same for both loops, but you need separate X and Y counters.

QuantumNova 21-Apr-19 23:23pm
Thank you, this help was very much appreciated :)
OriginalGriff 22-Apr-19 1:44am
You're welcome!

## Solution 2

You need to use a different counter for the inner loop. After printing the first 5 asterisks `i` will be equal to 6, so that loop will no longer be performed, and the outer loop will run forever. Try the following:
C#
```while (N*N > i)
{
var j = 0;
while (N > j)
{
Console.Write("*");
j = j + 1;
}
i += j;
Console.Write("\n");
}```

QuantumNova 21-Apr-19 23:23pm
Thanks so much, this really helped a lot, you are a lifesaver :)

## Solution 3

Quote:
but it returns as just a long line of (*)'s.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.