Multiplication table using while loop in C# vs Python

A while loop, just like a for loop, allows us to iterate over a block of code. Although the two loops are completely inter-changeable, there are scenarios where one is more elegant than the other.  Let us start by implementing the multiplication tables example from our nested for loop section, using nested while loops.

Multiplication table using while loop

C# code

Python code

This program has exactly the same output as the one with nested for loops. But it highlights some prominent differences between a for loop and a while loop. The while loop consists of only the keyword  while and a condition. The loop variables used in the condition must be declared before the while loop and if a loop variable needs to be incremented/decremented or modified, it has to be done in the body of the loop (code block following the loop). The for loop, on the other hand, can perform all these tasks in a single statement. This is the reason why for loops are considered more elegant for programs where we know exactly how many times a loop must iterate i.e. we know the initial value and the final value for our loop variable.

Let us now take an example where a while loop might be more elegant. In the above example, we knew beforehand that we had to print the multiplication tables for numbers from 5 to 10. Suppose now we don’t know the upper limit for our variable  num. We want to start by printing the multiplication table of 5 and then ask the user if they want to print the next table. We must then continue printing the next multiplication table until the user tells us to stop.

C# code

Python code

This example highlights the flexibility of a while loop over for loop. The loop condition in a while loop does not need to be a comparison statement with either  <= or  >= operator. It can be any Boolean expression i.e. any expression that results in a true or false value. This makes the while loop more suitable in situations where we don’t exactly know the number of times a loop needs to iterate.

As we have established that the two loops are interchangeable, there must be a way to implement the above example using for loop. Indeed, there is. It can be implemented using an infinite for loop and a break statement. An infinite for loop is simply a for loop with the termination condition left blank. Such a for loop will run forever unless a break statement is called. Similarly, an infinite while loop has a loop condition that is always true. The break statement can be used to immediately terminate any loop. The following C# code implements the example above using an infinite for loop. The implementation with a while loop is, however, more elegant.

 What we learnt

  1. While loop has a more flexible loop condition compared to for loop.
  2. There are cases where while loop might be preferable over for loop and vice versa.
  3. Infinite looping and break statement.

< Multiplication table using for loop               Defining and calling functions >

You may also like: