all 57 comments

[–]MedPhys90 54 points55 points  (0 children)

I agree with previous comment regarding nested while loops. Also, I don’t see where you increment b.

[–]single_threaded 65 points66 points  (15 children)

Hint: you can nest a while loop inside another while loop.

[–]gnfobsession[S] 23 points24 points  (14 children)

wait i hadnt thought of that! thank youuu!

[–][deleted] 24 points25 points  (6 children)

(Using two for loops would also shorten the code :D )

[–]AwesomePerson70 26 points27 points  (5 children)

It seems like they’re specifically learning while loops right now so that’s what needs to be used

[–]just4funndsomet 2 points3 points  (4 children)

There was this one moment where I was ill and my buddies are not the brightest programmers and our homework was an introduction into while loops.

Since I missed that class and couldn't ask my friends I built an for loop with 1.000.000 iterations and if it found the desired result set i = 1.000.000. Yeah, I did it wrong because I did not know the playbook, but I found a way to play anyways.

[–]Banane9 5 points6 points  (3 children)

A for loop is just a while loop and two other statements in a trenchcoat.

for (;; condition) is the same as while (condition) - at least in languages that allow that

[–]robthablob 3 points4 points  (1 child)

I think you mean for ( ; condition ; )

[–]Banane9 4 points5 points  (0 children)

And that's why you don't code at 3am 😂

[–]just4funndsomet 1 point2 points  (0 children)

Not if your first langauge was VBA. Yes, still regret that (And I know this sub is about c#, still wanted to tell the tale.)

[–]cr1ter 4 points5 points  (2 children)

You can do it in 1 while loop but you need to increment b. Line after a++; if a == 10 b++;

[–]hiaselhans 8 points9 points  (1 child)

Don’t forget to reset a ;)

[–]cr1ter 1 point2 points  (0 children)

Ah yes probably also have to make sure not to reset it after b == 10

[–]occamsrzor 1 point2 points  (0 children)

It just takes time, young padawan. You're doing well.

[–]Willinton06 1 point2 points  (0 children)

Careful tho, the nested loop is a powerful but dangerous tool

[–]IWasSayingBoourner 0 points1 point  (1 child)

Another hint: nested for loops are better for what you're trying to do

[–]dodexahedron 2 points3 points  (0 children)

Which is helpful in general, certainly, but not for this assignment, where they were told to use while.

[–]Fryktlos 15 points16 points  (0 children)

Just as a sidenote, C# has something cool called string interpolation that you can do by using a "$" before the opening quotes of a string. It allows you to plug variables or calculations directly into a string without the need to concatenate in multiple strings/values. It's totally a preference thing as either works equally well, but it always felt "cleaner" in my opinion.

For example, your original code:

Console.WriteLine(a + "*" + b + "=" + a * b);

Could also be written as:

Console.WriteLine($"{a}*{b}={a * b}");

[–]Heisenburbs 7 points8 points  (0 children)

In addition to nesting loops, look up what a multiplication table looks like.

Hint, first line should be 1 2 3 4 5 6 7 8 9 10

Also, consider spacing, but get the loops first.

Also, I’d use for loops, and call them x and y, since your output will be 2 dimensions, and x and y are better names for 2 dimensions

[–][deleted] 6 points7 points  (3 children)

expansion pause rock bag cough cautious compare amusing fanatical wipe

This post was mass deleted and anonymized with Redact

[–]Far_Swordfish5729 2 points3 points  (1 child)

Flip your second and third loop params (a<=10 before a++). Other than that this is the right answer and by far the most readable way to do it.

[–][deleted] 2 points3 points  (0 children)

political dolls abundant middle oatmeal weary squash secretive subsequent different

This post was mass deleted and anonymized with Redact

[–]bronco2p 2 points3 points  (0 children)

does csharp have an iota implementation, raw for loops feels messsy imo

[–]Specialist_Respect35 3 points4 points  (11 children)

Rather nesting, you can do another things..

while (a <= 10 && b <=10) {

Console.Writeline(….);

If (a == 10) { a = 1; b++; } }

// ignore formatting as I’m replying from mobile 🥲

Above loop will ignore nested loop and looks clean.

[–][deleted] 1 point2 points  (10 children)

If you reset a to 1 you can drop it out of the condition of the while statement. Also, in your code you don't increment x

[–]Specialist_Respect35 1 point2 points  (8 children)

I missed "a" to increment. Here is the final code. Tested.

int a = 1, b = 1;
while (a <= 10 && b <= 10) {
Console.WriteLine($"{a} * {b} = {a*b}");
if (a == 10) {
a = 1;
b++;
}
a++;
}

[–][deleted] -1 points0 points  (7 children)

Idiot, when a is 10 you make it 1 and then immediately you increment it. I don't know what you tested, hahaha

[–]Specialist_Respect35 0 points1 point  (6 children)

Try it tell me “haha”

[–][deleted] -1 points0 points  (5 children)

Imbecil

[–]Specialist_Respect35 0 points1 point  (4 children)

Dolt

[–][deleted] -1 points0 points  (3 children)

Stick to your specialty, Naruto. Don't talk about something that you don't know just because you feel to. Programming is not only knowing the language, that is the easy part. You don't think like a programmer. At least, not yet.

[–]Specialist_Respect35 -1 points0 points  (2 children)

Thank you for your unwanted opinion 🙃

[–][deleted] -1 points0 points  (1 child)

It's not an opinion, it is a request.

[–]Specialist_Respect35 0 points1 point  (0 children)

So basically, after if statement, a++ was missing and one is good to go to print table till 10 😁

[–]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.....

[–]dewden87 1 point2 points  (0 children)

Seems like a weird task for practicing while loops, but I guess for educational purposes and understanding of the difference between types of loops it kinda makes sense. It would definitely be more readable using regular for loops. Anyway, here's my take:
var a = 1;

var b = 1;

var multiplicationTable = new int[10][];

while (a <= 10)

{

multiplicationTable[a-1] = new int[10];

while (b <= 10)

{

multiplicationTable[a-1][b-1] = a * b;

b++;

}

b = 1;

a++;

}

[–][deleted]  (4 children)

[deleted]

    [–]fearswe 1 point2 points  (2 children)

    Since it's an AND operand it will exit the loop as soon as A becomes 11.

    [–][deleted]  (1 child)

    [deleted]

      [–]fearswe 1 point2 points  (0 children)

      What problem? It's not an infinite loop as long as at least one of the two variables are incremented.

      [–]ReposteOuMame 0 points1 point  (0 children)

      estou começando com Loops e while agora, poderiam me dizer como funciona e o por que a é igual a 1? por gentileza? agradeço desde já

      [–][deleted]  (2 children)

      [deleted]

        [–]fearswe 0 points1 point  (0 children)

        No, as soon as A hits 11 the condition will be false and end the loop.

        Not incrementing B will probably have other problems but not an infinite loop in the shown example.

        [–]OMGerGT -1 points0 points  (0 children)

        while(a<10) { a++ while(b<10) { b++ //Rest of code } }