What is logic?
When I started learning programming 20 years back, I learnt different languages FORTRAN, COBOL, BASIC which were used for specific purposes and are now rarely used. And also learnt other languages, which are still used, like C, C++ and then Java. After learning so many languages, I understood that you can learn any language easily when you understood the concepts. But the main problem is how to implement those concepts. How to utilize those concepts to solve any problem?
So, logic is to divide your problem into small small steps and get your desired result. If you know, what a particular code or method is doing it will be easier to debug complex problems too. You will be able to understand code written by other developers too. And you can only learn it through practicing and developing logical and analytical skills. There are few strategies to analyze and solve your problem -
Suppose we have a small stationary shop and a student comes and buys some stationary items like pencils, erasers, notebook etc. -
- Make organized lists / chart / diagram (flow chart) -
- List all your information already available to you (Data) - We as shopper already know what is the price for each item.
- List what is the targeted result, what are the requirements (output) - We need to calculate total amount of money the student has to pay.
- List what do you require from user (input) - We have to ask student what item and how many he needs.
- List the steps how will you use input and data to get the output - Do all the calculations required
- Check if there are some conditions and which one is suitable (if, if else, switch case etc.) -
- Check the pattern in steps, how many steps are you repeating for different data - so do we need loops (for, foreach, while, do etc.) or creating methods for reuse - We have to repeat multiplication for each item - price * item quantity.
- List what can go wrong - data mismatch (wrong input), calculation (division by 0) etc.
So, break down a complex problem into small problems and write down steps for each problem. Start writing small small programs and practice for improving your logical and analytical skills.
NOTE: My main aim for this blog is to make people, who have just started programming, to make them to understand logic for each program. Nowadays, you can get solution for each problem, but programming is much more than cut, copy and paste. Logical thinking is must for each programmer.
1 comment:
Great example to illustrate the programming aspects
Post a Comment