Card 5: Conditional Logic (If/Else)

Programming is not just about giving a fixed list of instructions. Sometimes, you need the computer to make decisions. This is called conditional logic. It's like deciding what to do based on the weather in your village in Andhra Pradesh.

1. The `if` Statement: Making a Choice

Story: The `if` statement checks if a certain condition is true. If it is, the computer will run a specific piece of code.

Analogy: If it is raining, then you take an umbrella.

2. The `else` Statement: The Alternative

Story: The `else` statement gives the computer an alternative action to take if the `if` condition is false.

Analogy: If it is raining, take an umbrella. Else (otherwise), wear your sunglasses.

3. Comparison Operators

Story: To create conditions, we use comparison operators. `==` (is equal to), `!=` (is not equal to), `>` (greater than), and `<` (less than).

Example: `if age > 18` checks if the value in the `age` variable is greater than 18.

Python Code with If/Else:

age = 20

if age >= 18:
  print("You are old enough to vote.")
else:
  print("You are not old enough to vote yet.")

# Another example
temperature = 35 # in Celsius
if temperature > 30:
  print("It's a hot day in Andhra!")
else:
  print("The weather is pleasant.")
            

Key Technical Terms

Knowledge Check

In the code `if temperature > 30:`, what happens if the `temperature` is 25?