Conditional statements are like the mind of your program. Every program that you write is likely to have at least one conditional statement. Since this topic is so important, we must learn this before we move forward.
If you want to perform a certain action only when a certain condition is met you can use conditional statements to achieve this. Take a look at the following example.
You can use an if statement to ensure that the message is printed only if a is positive. And if it is negative the message will not be printed. But what if we want to print a different message if a is negative?
In that case we can use an else statement. Please note how the code is formatted. It is necessary to indent the body (code that is executed if a condition is met) otherwise the code will not be run.
Surely now you covered all of the possible cases? But what if a equals to zero? A simple way to solve that would be to nest an if statement within another if statement.
This approach gives the correct result. But what if you had more than three possible conditions? The code will become a lot harder to follow as the complexity grows. To avoid that where appropriate you can use an elif statement. Think of it as else and if combined so that if the last condition was not met another one will be tested.
Take your time to understand the ideas behind each statement. It is not always obvious which statement to use especially in very complex algorithms which might have multiple nested conditional statements.
Lastly there is syntax that is not widely used that allows to write simple if statements in one line.
This technique is quite limiting because it only allows a simple one line expression to be in the body. But there are a few situations where this might prove useful by saving some space and improving readability.