2
I’d like to spare those 9 repeated lines in my code:
for (int n1 = 1, n2 = 1; n2 < 11;){
Console.Write(n1 + "x" + n2 + "=" + (n1 * n2) + "\t"); n1++;//1
Console.Write(n1 + "x" + n2 + "=" + (n1 * n2) + "\t"); n1++;//2
Console.Write(n1 + "x" + n2 + "=" + (n1 * n2) + "\t"); n1++;//3
Console.Write(n1 + "x" + n2 + "=" + (n1 * n2) + "\t"); n1++;//4
Console.Write(n1 + "x" + n2 + "=" + (n1 * n2) + "\t"); n1++;//5
Console.Write(n1 + "x" + n2 + "=" + (n1 * n2) + "\t"); n1++;//6
Console.Write(n1 + "x" + n2 + "=" + (n1 * n2) + "\t"); n1++;//7
Console.Write(n1 + "x" + n2 + "=" + (n1 * n2) + "\t"); n1++;//8
Console.Write(n1 + "x" + n2 + "=" + (n1 * n2) + "\t"); n1++;//9
Console.WriteLine(n1 + "x" + n2 + "=" + (n1 * n2)); n2++;//10
n1 = n1 - 9;
}
It’s a noose for
simple that generates the Multiplication Table complete, the code is repeated 10x, each Console.Write
writes a column snippet in the same row by repetition. I thought of nesting a for
inside another, as for example:
for (int n1 = 1, n2 = 1; n2 < 11;){
Console.Write(n1 + "x" + n2 + "=" + (n1 * n2) + "\t"); n1++;//9x
for (n1==10){
Console.WriteLine(n1 + "x" + n2 + "=" + (n1 * n2)); n2++;//1x
n1 = n1 - 9;
}
}
In this syntax, numerous errors of Expected...
I found this article C# - Nested Loops, but none solved. Some solution?
Thank you very much, your code is much better optimized, I will research a little more about the
$
and{}
within theWriteLine
. I managed to rotate replacing the secondfor
by aif
. Some way to output print columns side by side?– Lucas Bonafé
Right here you have everything about it. It is possible to do with a
if
, but I don’t like it, it’s counterintuitive. You can do it in columns, but it’s a little bit more work and you’d have to reverse the execution, not to mention the lack of space, then you’d have to break into blocks that’s too complicated.– Maniero