It’s 6 pm in the evening, and some guests are at your house. Your mother asks you to go to the market and buy some biscuits. She asks you to buy the salted ones. you may buy another only if you don’t find the salted ones. Simple logic, correct? IF salted ones are available, buy them, ELSE buy anything else.
In this scenario, you have a choice. There are certain conditions and outcomes. Decisions in such cases require logics. You can also use such logics in your code. We often use the IF-ELSE logic
IF-ELSE logic
Understanding the blocks used
Now let us understand with some simple examples.
Example 1: checking if the number is odd or even
Logic: If – divisible by 2= even, else odd
Blocks used
⦁ If-else block
⦁ Condition block
⦁ Remainder block = this math operation returns the remainder. For an even number divided by 2 give 0 as the remainder and for an odd number give 1.
You can build the same program and use it to check different numbers. But as you move forward, you will realize it tells 1.2 or 2.4 as odd. It is because numbers with decimals are neither even nor odd. So let us fix it. Here we will use the If-Else if-else logic
The program gives the following results:-