Hello students! 👋
Welcome to Post 5 of our QBasic learning series. In the previous lesson, we learned about operators. Now it’s time to use those operators to help the computer make decisions.
Just like humans decide “if it rains, I’ll carry an umbrella”, computers also decide using IF…THEN…ELSE statements.
What is IF…THEN…ELSE?
The IF…THEN…ELSE statement is a decision-making statement in QBasic.
It allows the program to choose one path when a condition is true and another path when it is false.
Simple Meaning
-
IF a condition is true → do something
-
ELSE → do something else
Simple IF…THEN Statement
This is used when there is only one condition.
Syntax:
Example:
✔ If the condition is true, the message is printed.
❌ If false, nothing happens.
IF…THEN…ELSE Statement
Used when we want two choices.
Syntax:
Example:
📌 The program checks the condition and chooses the correct block.
Using Relational Operators with IF
IF statements usually use relational operators:
| Operator | Meaning |
|---|---|
> | Greater than |
< | Less than |
= | Equal to |
>= | Greater than or equal |
<= | Less than or equal |
<> | Not equal |
IF…ELSEIF…ELSE Statement
Used when there are multiple conditions.
Syntax:
Example:
🧠 The computer checks conditions from top to bottom and executes the first true one.
Nested IF Statement
An IF inside another IF is called a Nested IF.
Example:
Common Mistakes Students Make
❌ Forgetting END IF
❌ Using = instead of >= or <=
❌ Wrong indentation (harder to read)
✔ Always align your IF blocks properly.
Why IF…THEN…ELSE is Important
-
Helps the computer think logically
-
Used in marksheet, age checking, login systems
-
Foundation for advanced programming concepts
Key Points to Remember
-
IF checks a condition
-
ELSE runs when condition is false
-
ELSEIF handles multiple conditions
-
END IF is compulsory
-
Conditions use relational and logical operators
What’s Coming Next?
In Post 6, we’ll learn about SELECT CASE — a cleaner and smarter way to handle multiple conditions in QBasic 😎
Keep practicing small programs and try changing conditions to see how the output changes.
Happy coding! 🚀
Comments