Q-BASIC If, Then, Else

Every single day, you make decisions without even noticing. "If it's raining, I'll take an umbrella. If not, I'll leave it at home." Your brain checks a condition, and picks one of two paths.

Computers do exactly the same thing — and in QBasic, the tool for this is the IF…THEN…ELSE statement. This is one of the most important topics for your SEE Computer exam, so we'll go slowly, with plenty of examples — and a couple of things you can actually click and try.

1. What is IF…THEN…ELSE?

Definition: IF…THEN…ELSE is a decision-making statement. It checks whether a condition is TRUE or FALSE, and runs a different block of code depending on the answer.

A condition is really just a yes/no question. "Is the number greater than 10?" "Is the age 18 or more?" Every one of these has exactly two possible answers — and IF…THEN…ELSE lets your program react differently to each one.

2. The umbrella analogy

Analogy: you're leaving home in the morning and ask yourself: "Is it raining?" If YES, you take an umbrella. If NO, you leave without one. You never do both, and never do neither.

Try it: click the weather

This is exactly how the computer "thinks" through an IF…ELSE.

🧍
Click a button to see the decision happen.
Is it raining? Take umbrella Leave without it End YES NO

3. Basic syntax

syntax.bas
IF condition THEN
    ' code that runs if the condition is TRUE
ELSE
    ' code that runs if the condition is FALSE
END IF
PartWhat it means
IF condition THENAsks a yes/no question
First blockRuns only if the answer is YES (TRUE)
ELSEMarks the start of the "otherwise" path
Second blockRuns only if the answer is NO (FALSE)
END IFTells QBasic the decision block is finished
Important: every IF must be closed with END IF. Forgetting this is one of the most common errors beginners make.

4. Example 1 (Easy): Even or Odd

A number is even if it divides exactly by 2. In QBasic, we check the remainder using MOD.

even_odd.bas
CLS
INPUT "Enter a number: ", num

IF num MOD 2 = 0 THEN
    PRINT num; "is an Even number"
ELSE
    PRINT num; "is an Odd number"
END IF

END

Try tracing it yourself: if a student enters 7, then 7 MOD 2 gives 1, so the condition is FALSE, and QBasic prints "7 is an Odd number."

5. Example 2 (Medium): Grading System with ELSEIF

For a grading system, we have more than two outcomes — A+, A, B, C, Fail. ELSEIF lets you check several conditions, one after another.

grading.bas
CLS
INPUT "Enter your marks: ", marks

IF marks >= 90 THEN
    PRINT "Grade: A+"
ELSEIF marks >= 75 THEN
    PRINT "Grade: A"
ELSEIF marks >= 60 THEN
    PRINT "Grade: B"
ELSEIF marks >= 40 THEN
    PRINT "Grade: C"
ELSE
    PRINT "Grade: Fail"
END IF

END

Try it: run the logic yourself

Type marks below. It will highlight exactly which line of the code above "runs" — this is what your computer does in real time.

Result will appear here.
How QBasic reads this: it checks each condition top to bottom and stops at the first TRUE one. Enter 80 above and watch: it skips "≥90", lands on "≥75", and never even looks at the rest.

6. Example 3 (Harder): Combining Conditions with AND

Sometimes a decision depends on more than one condition at once. Checking voting eligibility in Nepal needs both citizenship and age ≥ 18 — both, not just one.

voting_check.bas
CLS
INPUT "Enter your age: ", age
INPUT "Are you a Nepali citizen (Y/N): ", citizen$

IF age >= 18 AND (citizen$ = "Y" OR citizen$ = "y") THEN
    PRINT "You are eligible to vote."
ELSE
    PRINT "You are not eligible to vote yet."
END IF

END

Here, AND means both sides must be TRUE for the whole condition to be TRUE.

7. Common mistakes to avoid

  1. Forgetting END IF — QBasic will throw a syntax error.
  2. Case-sensitive string comparisons — citizen$ = "Y" won't match a lowercase "y" unless you check for both.
  3. Wrong order of ELSEIF conditions — checking marks >= 40 before marks >= 90 would stop every high score at "Grade: C" first.

8. Practice tasks

  1. Write a QBasic program that asks for a number and prints whether it is positive, negative, or zero.
  2. Modify the grading program so it also prints "Excellent work!" alongside Grade A+ results.
  3. Write a program that checks if a year is a leap year (a year is a leap year if it divides exactly by 4, but not by 100, unless it also divides by 400).
  4. Challenge: write a program that asks for three numbers and prints the largest, using only IF…THEN…ELSEIF.

Once you're comfortable with IF…THEN…ELSE in QBasic, the exact same logic applies in every language you'll ever learn — including Python. If you want to see this same "yes/no decision" idea in Python and run real code in your browser, try the Python Playground (your theme automatically links it below this post).

Comments