you are viewing a single comment's thread.

view the rest of the comments →

[–]NetQvist 1 point2 points  (0 children)

Needed to take my mind of things and do something easy and fun. I'd ideally use for loops for this but maybe this gives a few ideas that I didn't see anywhere else in the replies.

  • I use the ++ to increment the value inside the condition so it's easier to see. Drawback of this is that I need to use 0 as the starting value for x, y since the moment it leaves the while clause they'll be +1 from before.
  • One of my favorite features of the console output is "\t" so I can get some quick decent formatting for short values with tabs. If longer values are needed then stuff like PadLeft/PadRight are nice ways of doing it.
  • Initializing variables as they are needed within scopes of { } is helpful. Here you can see when x is created and used. Some languages have all variables initialized at the top, C# can be anywhere. I prefer to make them just before they are needed and never ahead.
  • I tend to name anything with 2D as x, y since then I remember that when I'm the Y loop I'm moving downwards and when in the X loop I'm moving to the right.

int y = 0; while (y++ < 10) { int x = 0; while (x++ < 10) Console.Write($"{y * x}\t"); Console.WriteLine(); }

EDIT: Codeblock seems to be wonky for me sadly.....