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

1

I would like to save these 9 lines repeated 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 is a simple for loop that generates the multiplication table complete, the code is repeated 10x, each Console.Write writes a column excerpt on the same line per repetition. I thought of nesting a for within another, such as:

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 emerge from Expected ...

I found this article C # - Nested Loops , but none resolved. Any solution?

    
asked by anonymous 27.09.2017 / 03:39

2 answers

0

Last version, I was able to leave the columns output 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();
}}}

View on GitHub .

    
06.03.2018 / 00:56
1

This code does not make any sense, it has basic syntax errors. It would look 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();
        }
    }
}

See running on .NET Fiddle . And no Coding Ground . Also I placed GitHub for future reference .

    
27.09.2017 / 03:48