Loop "for" inside another "for" (nested repetition)

Asked

Viewed 2,890 times

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?

2 answers

2

This code doesn’t make any sense, it has basic syntax errors. It would be like this:

using static System.Console;

public class Program {
    public static void Main() {
        for (int n1 = 1; n1 < 11; n1++) {
            for (int n2 = 1; n2 < 11; n2++) WriteLine($"{n1:d2} x {n2:d2} = {n1 * n2:d2}");
            WriteLine();
        }
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • Thank you very much, your code is much better optimized, I will research a little more about the $ and {} within the WriteLine. I managed to rotate replacing the second for by a if. Some way to output print columns side by side?

  • 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.

1


Last release, I managed to leave the output of the columns side by side:

using System;
namespace ConsoleApp1{
    class Program{
        static void Main(string[] args){
            Console.WriteLine("\nTABUADA MULTIPLICAÇÃO\n");
            for (int n1 = 1, n2 = 1; n2 < 11;){
                Console.Write($"{n1}x{n2}={n1*n2}\t"); n1++;//9x
                if (n1 == 10){
                    Console.WriteLine($"{n1}x{n2}={n1*n2}"); n2++;//1x
                    n1 = n1 - 9;
                }
            }
Console.WriteLine("\nDigite qualquer tecla para sair.");
Console.ReadKey();
}}}

See on Github.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.