Print multiplication table using for loop in C# vs Python

For loop enables us to execute a block of code repeatedly for a given number of times. Lets start with a very simple example. Suppose we want to write a program that prints all even integers from 0 to 100. We can achieve that either by manually writing each even integer in a print function, which would be tedious and inefficient; or we can use a for loop as follows.

C# code

Python code

For loop syntax

The components of a for loop are highlighted in the figure below. The first thing we require is a loop variable, in our case the variable i, and an initial value for the loop variable. We then provide the condition or the value of loop variable at which the loop should terminate. And finally, we provide the increment for our loop variable at each iteration of the loop, called step size. We have a step size of 2 in our program.


Components of a for loop

The loop begins with the loop variable  i equal to the initial value. The block of code inside the for loop runs for each value of i as long as the termination condition/final value is not reached. After each iteration, the value of i is incremented/decremented based on the step size. The step size can be a positive, negative or even fractional number. Obviously, the loop variable must be of type float for fractional step size. Once the variable  i reaches the final value, the block of code runs for the last time and the for loop is exited.

Notice that the final value in our Python code is 101. This is because the range function in Python, range(start value, end value, step size) generates numbers from start value to end value – 1 i.e. range(0,101,2): generates integers from 0 to 100 with a step size of 2.

Multiplication table using for loop

 For any given integer, the following program displays it’s multiplication table using for loop.

Yes, it is that simple, thanks to loops. The loop variable i takes values from 1 to 10 and for each value of i, the block of code simply multiplies the variable num with i and displays the result. The increment in C# using the statement i++ is equivalent to saying i = i + 1. This kind of increment statements are available in the C family of languages. To add 2 to the value of i, we can either write i = i + 2 or simply i += 2.

Notice how the code block must be indented by 4 spaces in Python as we have seen previously with if-statement. The string “\n” that we used in the print() function in Python, is an escape sequence called line feed. It tells the compiler to go to the next line. There are many escape sequences available in both C# and Python and we will see more of them as required. Another example of an escape sequence is “\t” that adds a horizontal tab in our string. Since the function Console.WriteLine() has built-in functionality to go to the new line after printing the string, we did not need to use an escape sequence in C#. If a line feed is not required in C#, the Console.Write() function can be used instead.

Nested for loops

Just like if-statements, for loops can also be nested, with each loop having its own loop variable. Building on our multiplication tables example; suppose we want to print the multiplication tables of all integers from 5 to 10, one after the other. We can easily develop such a program using nested for loops.

The outer for loop iterates over the values for variable num, while the inner for loop iterates over i. The outer loop starts with the variable num equal to 5. The inner for loop then goes through all the values of i, multiplies them with 5 and displays the result. Once the inner loop is finished, the variable num is incremented to 6 by the outer loop and the inner loop goes through all the values of i once again, this time multiplying them with 6 and displaying the result.

What we learnt

  1. Loops allow us to execute a block of code repeatedly for as many times as we require.
  2. For loops have four components; a loop variable, initial value, termination condition and step size.
  3. For loops can be nested where each loop iterates over its own loop variable. Nested for loops can be very useful when implementing complex algorithms.
  4. The escape sequence “\n” can be used in a string to go to a new line and “\t” can be used to add a horizontal tab.

< Programming a basic calculator               Multiplication table using while loop >

You may also like: