Hi there,

I have only just began the proccess of learning C# Programming (this week)returning to education as a mature student, one of the assessments I have been given is to find out how to, and then write a program using 'nested for loops' to display the 1,2,3 and 4 times tables. I do not want someone just to write the code for me to copy as this would be of little value to the learning proccess, but would greatly apprieciate any member of the forum being able to point me in the right direction of how I can begin to solve this problem.

I imagine it will require two loops the first being: for (int i=1; i<5; i++) this to give the 1 to 4 times tables ??
The second loop is where I am at a total loss (providing it does just require two loops and the first is correct).

I apprieciate for many this this will be very basic, and apologise if I have posted this to a forum it does not belong.

6 Years
Discussion Span
Last Post by kplcjl

nested loops are loops within loops

for(outer=1; outer<4; outer++){
    for(inner=1; inner<5; inner++){
    //do something here ... 

in the sample:
first iteration: outer = 1, inner = 1
second iteration: outer = 1, inner = 2
third iteration: outer = 1, inner = 3
fourth iteration: outer = 1, inner = 4
fifth iteration: outer = 2, inner = 1

as you can see, the outer loop increments only when the inner loop is done
and when the outer loop increments by 1, the inner loops starts again at 1

Edited by daniel955: n/a

Votes + Comments
Well explained!

Your outer loop goes from 1 to 3, the OP wants it from 1 to 4.
The inner loop should probably go from 0 to 9

Console.WriteLine("This displays the one to four times tables");
            for (int a = 1; a <=4; a++)
                for (int j = 0; j <= 12; j++)
                    Console.WriteLine(a * j);

many thanks for taking the time to help, the code above is what I have come up with so far and it gives out a display of the tables in a single column; what i was wondering is, can it be displayed to show a format like :


Yes, experiment with Console.Write and Console.WriteLine and some formatstrings.
Example: Console.Write("{0} x {1} = {3}", a, j, a*j);
This "{0} x {1} = {3}" is called a format-string the {0} etc. in it, are placeholders for the arguments a,j and a*j.
Have fun with your experiments!


Your loop is entirely readable, understandable and isn't wrong. Just a warning about standard C# coding practices. Maybe this comes from arrays, where you declare the array [n] where n is the actual size of the array. Then you loop "for (i = 0; i < n ; i++) {...}".

I don't know why the community likes that style, but you see it all the time. One of the reasons I like it on 0 based initial loop values is that it exactly matches the number of loops being made, yet it still looks odd when you see j <= 12.

I don't know why, but some people really don't like to see "++j" instead of "j++" for the third parameter. Maybe they don't like to see ++ in front because it implies that that operation is done first and in a loop, that isn't the case.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.