Recent - Posts

QBasic for Beginners – Post 5: IF…THEN…ELSE Statement

 

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:

IF condition THEN statements END IF

Example:

CLS age = 18 IF age >= 18 THEN PRINT "You are eligible to vote." END IF

✔ If the condition is true, the message is printed.
❌ If false, nothing happens.


IF…THEN…ELSE Statement

Used when we want two choices.

Syntax:

IF condition THEN statements ELSE statements END IF

Example:

CLS marks = 45 IF marks >= 40 THEN PRINT "You have passed." ELSE PRINT "You have failed." END IF

📌 The program checks the condition and chooses the correct block.


Using Relational Operators with IF

IF statements usually use relational operators:

OperatorMeaning
>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:

IF condition1 THEN statements ELSEIF condition2 THEN statements ELSE statements END IF

Example:

CLS marks = 75 IF marks >= 80 THEN PRINT "Grade A" ELSEIF marks >= 60 THEN PRINT "Grade B" ELSEIF marks >= 40 THEN PRINT "Grade C" ELSE PRINT "Fail" END IF

🧠 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:

CLS age = 20 citizen = 1 '1 means yes, 0 means no IF age >= 18 THEN IF citizen = 1 THEN PRINT "You can vote." ELSE PRINT "Citizenship required." END IF ELSE PRINT "You are underage." END IF

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