Programming Made Easy

Part 1: Introduction

Beginners in the field of programming often face difficulty digesting a few concepts and paradigms in this domain. Typically, the journey of a beginner starts with taking up a programming language, writing a hello world program, understanding the use of variables, understanding if statements, loops, functions and finally moving on to more intermediate things like classes, objects and so on.

The Difficulty in Visualizing Logic

The truth about programming is that it is logical and people who are new to this field often have difficulties in visualizing this logic. By difficulty in visualizing, what I mean is that one would largely be able to follow if a tutor was to take them through the logic, but will not be able to reproduce the same if asked. Another way of looking at this is when you don't understand the logic of a program but don't know what question to ask either. If you are one who can relate to this, then this series is for you.

Moving to the Difficult Parts

One must recognize that not every part of programming is difficult. Variables, mathematical operations (like addition, subtraction and division) and to some extent conditional branching (basically if - else statements) are the easy part. The main difficulty lies in understanding loops (and their use cases), objects, classes and threads. In this article, I will attempt to break down the idea of a loop.

Loops

A computer program is basically a set of instructions the computer must perform. Computer programmers must define those instructions such that when the computer performs them, the result is the output that is required by the user. It so happens, that most outputs can be achieved by performing the same task multiple times (or performing a similar task multiple times with a new tweak each time). This is where loops come in.

Say the user wants the program to determine whether a particular number is prime or not. A prime number is one that is only divisible by itself and 1. To check whether a particular number is prime, one will have to check if it is divisible by any natural number greater than 1 but less than the number itself. If the user provides an input of 5, one must check divisibility for 2, 3 and 4. In this case we are performing the same operation of checking the divisibility 3 times, each time incrementing the divisor by 1. This is a classic example of how you are performing the same operation multiple times with a tweak.

For a beginner, the above example might be a little complicated in the first read. That is not because it is difficult, but because it is new. Clarity of the visuals in your mind (when you go through the above example) will increase as you visualize it multiple times. With time and practice, you will have visualized the logic behind a lot of other programs, making your future visualization more clear. Logical visualizations of such kind require one to follow a sequence of thoughts.

Other Exercises

The use case for loops is too wide for it to be covered in one article. Loops are so fundamental that one will almost never come across a problem that doesn't require a loop to solve. The best way to master loops is to practice them. The following exercises can help build iterative logic.

  1. Write a program to find the factorial of a given number.
  2. Write a program to count the number of factors of a give number.
  3. Write a program to check if a number is palindrome. The number 121 is palindrome because its reverse is also the same number. (requires understanding of modulus operator)
  4. Write a program to calculate the number of words in given sentence (requires basic understanding of string functions).

The above mentioned exercises require you to understand the what are variables, if-else statements and have syntactical knowledge of a programming language. Other pre-requisites to solve the exercises are mentioned adjacent to them. These a standard problems and the solutions to them are simply a Google search away.

As mentioned earlier, this seems difficult because it is new. Visualizing unfamiliar logic is similar to taking an unfamiliar route while commuting. With time, as you travel the route multiple times, it becomes clear in your mind and you start discovering its intricacies.