# Logic in programming

Logic is not the first thing you think about when you start programming. It is spontaneous to start from the study of syntax or vocabulary. Because programming is usually associated with writing code.

But try to think of a writer of novels. Surely his style of writing is what distinguishes him. The way it writes down the plot he invented, the intertwining of the events.

All this happens only on one condition: that the writer has thought of a plot and weave in his mind, that are coherent with each other and possibly intriguing.

Before starting to program I usually take at least 5 minutes to understand the problem well. Even when it seems seemingly simple, a question always hides hidden variables not immediately obvious and not always the most immediate solution to demonstrate the easiest to implement.

There are problems with complex logic that require hours or days of analysis even before you start to turn on the computer. These are problems involving complex logic, highly articulated data sets and requiring knowledge of mathematics and expert computation. In these cases, there are real documents that summarize the logic through flowcharts and UML. At this level we talk about the analysis of software engineering, a real specialization for those who want to try their hand in this world.

## Logical schemas and flowcharts

Even for simple problems and especially for logic puzzles it can be useful to lay out a flowchart.

You can represent the ideas that come to mind on how to get from the beginning to the end of your reasoning, passing through the different possible roads, marking in what points the logical flow takes different paths according to the dynamic conditions or loops in some segments.

## The mathematics

The riddles you will find in this advanced programming course need a good mathematical basis. For each problem I will show you what is the theorem or the mathematical principle to which it refers. In case you do not know it already you can deepen it before you choose how to proceed.

There are always many different ways to deal with a problem. Depending on your temperament, experience and personal point of view you can choose several roads. Although they can all be correct, there’s no guarantee that they are overlapping: there are other elements that are at stake, beyond the correctness of the final result, which are:

• security, i.e. the possibility that the program is used in unexpected configurations
• efficiency, i.e. the use of the physical resources that the program needs to function.
• complexity. In this case the problem is not perceived by the user but it hinders the life cycle of a program, in particular the testing and debugging phases.

Efficiency is one of the most clear parameters when using a program, because that is what determines the running speed and battery life. Mathematics and logic are a great help at this stage because they allow you to do the same thing but with less effort by the calculator.