Chapter 14 — Conditional Statements

Easy-language Notes · Part 1 of 2 (intro · if · if-else)
REVISION NOTES
Class XI · Computer Science (083)
Sheet 1 (Front)

1. What is a Conditional Statement?

A conditional statement lets the program decide what to do based on a condition. A condition is any expression that evaluates to True or False.

It is the same "if" we use in daily life. If it rains, take an umbrella, else wear sunglasses. The program tests the condition first and then chooses an action.

Where conditionals appear

  • Login: if username + password match, allow; else show error.
  • ATM: if balance is enough, give cash; else show "insufficient".
  • Result: if marks ≥ 33, print "Pass"; else "Fail".
  • Games: if score reaches 100, give bonus life.

Operators inside conditions

OperatorMeaning
==   !=Equal · Not equal
> < >= <=Standard comparisons
andTrue only if both sides True
orTrue if any side True
notReverses True ↔ False

Truthy and Falsy values

A condition need not be a comparison — any value can be tested. Python treats the following as False and almost everything else as True:

FalsyTruthy
0, 0.0any non-zero number
"" (empty string)any non-empty string
None, FalseTrue
Read a condition out loud as a question. if marks >= 33: reads as "is marks at least 33?" — if the answer is yes, run the block.
A common mistake: = instead of ==. Single = assigns; double == compares. x = 5 stores 5; x == 5 asks "is x equal to 5?".

2. The if Statement

The simplest conditional. Run a block only if a condition is True. If False, skip the block.

Syntax

if condition:
    statement1
    statement2
next_line          # always runs

Three things to notice:

  • The line ends with a colon. Forgetting it is a syntax error.
  • The next lines are indented (4 spaces). Indentation marks "inside the if".
  • The first line back at the original margin is outside the if and runs always.
Voting eligibility:
age = int(input("Age: "))
if age >= 18:
    print("You can vote.")
print("Thank you.")
If age = 20 → both lines print. If age = 15 → only "Thank you." prints.
Discount on big bill:
amount = 1500
if amount > 1000:
    discount = amount * 0.10
    print("Discount:", discount)
Output: Discount: 150.0

What if the condition is False?

The whole indented block is skipped. Execution jumps to the next line at the outer indent. The program does not stop; it just continues without running the if-block.

Free shipping over Rs 500:
total = 320
shipping = 40
if total > 500:
    shipping = 0
print("Ship:", shipping)
For total = 320 the if-block is skipped; shipping stays 40. Output: Ship: 40
A condition can use and / or / not to combine more checks: if marks >= 33 and attendance >= 75: — passes only if both are true.

3. The if-else Statement

Use if-else when you have two paths — one for True, one for False. Exactly one block runs, never both.

Syntax

if condition:
    # True path
    statement_T
else:
    # False path
    statement_F
Even or Odd:
n = int(input("N: "))
if n % 2 == 0:
    print(n, "is Even")
else:
    print(n, "is Odd")
% is the remainder operator. If n / 2 remainder is 0, n is even.
Largest of two numbers:
a, b = 12, 30
if a > b:
    print(a, "is larger")
else:
    print(b, "is larger")
Output: 30 is larger

Why use else instead of two ifs?

Writing two opposite ifs tests the condition twice and risks mistakes if you change one. A single if-else tests once and guarantees exactly one path runs.

Eligibility — using else is cleaner:
age = 17
if age >= 18:
    print("Eligible")
else:
    print("Wait", 18 - age, "yr")
Output: Wait 1 yr
else never has a condition. Don't write else condition: — it is just else:. The else covers all the cases not matched by the if.

4. The if-elif-else Statement

Use this when you have more than two paths. Python checks each condition top-to-bottom, runs the first True block, and skips all the rest. If none match, the final else runs.

Syntax

if condition_1:
    block_1
elif condition_2:
    block_2
elif condition_3:
    block_3
else:
    block_default

elif is short for "else if". Chain as many as you need.

Grade by percentage:
p = 67
if p >= 90: g = "A"
elif p >= 75: g = "B"
elif p >= 60: g = "C"
elif p >= 33: g = "D"
else: g = "F"
print(g)
For p = 67: 90 fails → 75 fails → 60 passes → g = "C". The remaining branches are skipped.
Order matters. Write the highest condition first. If you put p >= 33 at the top, every passing student would get "D".

elif vs separate ifs

Three separate ifs test their conditions independently — multiple blocks may run. An if-elif-else ladder runs at most one block. Pick elif when the conditions are mutually exclusive.

PatternBranches that may run
Three separate ifs0, 1, 2, or all 3
if-elif-elif-elseExactly one
Once any branch matches, the rest are not checked. So overlapping conditions are fine — only the first match runs.

Chapter 14 — Conditional Statements

Easy-language Notes · Part 2 of 2 (elif · nested · one-line if)
REVISION NOTES
Class XI · Computer Science (083)
Sheet 1 (Back)

5. Nested if

An if statement inside another if (or inside else / elif). Used when a second decision depends on the first.

Syntax

if outer:
    if inner:
        block_A          # both True
    else:
        block_B          # outer True, inner False
else:
    block_C              # outer False
Login (need username AND correct password):
user = "vivek"
pwd  = "abc123"
if user == "vivek":
    if pwd == "abc123":
        print("Login successful")
    else:
        print("Wrong password")
else:
    print("User not found")
Three different messages depending on the two conditions.

Flattening with and

Many nested ifs can be replaced by a single if using and. The flat form is shorter and easier to read.

Both forms behave the same:
# nested
if x > 0:
    if x < 100:
        print("valid")
# flat (preferred)
if 0 < x < 100:
    print("valid")
Python supports chained comparisons like 0 < x < 100, which is a built-in shortcut for x > 0 and x < 100.
Deeply nested ifs become hard to read. Try to keep nesting at most 2 levels deep — flatten with and/or or use an elif ladder when possible.

6. Conditional Expression (One-line if)

Python lets you put a small if-else on a single line. Useful when assigning one of two values based on a condition.

Syntax

value = result_T if condition else result_F

Read it left-to-right: take result_T if condition is True, otherwise result_F.

Largest of two:
a, b = 12, 30
big = a if a > b else b
print(big)        # 30
Pass / Fail in one line:
marks = 28
print("Pass" if marks >= 33 else "Fail")
Output: Fail
Print a sign symbol in one line:
n = -3
print("sign:", "+" if n >= 0 else "-")
Output: sign: -
Use one-line if only for short value choices. Don't squeeze big multi-line logic into one line — it becomes unreadable.

Quick Summary — The Three Forms

FormUse when…
ifOne thing to do, only when condition holds
if-elseExactly two paths, choose one
if-elif-elseMany paths, only one runs
Nested ifSecond decision depends on the first
One-line ifPick one of two values quickly
Common errors: missing colon after if/elif/else; using = instead of ==; wrong indentation; else with a condition; two else blocks for one if.

Chapter 14 Worksheet — Conditional Statements

Total: 50 marks  |  Time: 75 min
Name: Class/Sec: Roll No.: Date:

Section A — Multiple Choice (1 × 10 = 10)

  1. Which keyword introduces the alternative branch when an if condition is False?
    (a) elif(b) else(c) other(d) when
  2. What is the output of the snippet below?
    x = 5
    if x % 2 == 0:
        print("A")
    else:
        print("B")
    (a) A(b) B(c) AB(d) Error
  3. Which operator returns True only when both sides are True?
    (a) or(b) not(c) and(d) &
  4. How many else blocks can one if have?
    (a) 0(b) 1(c) 2(d) Any
  5. Identify the valid one-line conditional:
    (a) x = 5 if a>b else 10(b) x = if a>b: 5 else 10(c) x = 5 ? a>b : 10(d) x = a>b then 5 else 10
  6. if marks >= 33 and attendance >= 75: prints "Pass" only when:
    (a) Either holds(b) Both hold(c) Neither holds(d) Always
  7. Which is NOT a comparison operator?
    (a) ==(b) !=(c) =<(d) >=
  8. What ends the header of every if / elif / else?
    (a) Semicolon(b) Colon(c) Comma(d) Period
  9. If a = 4 and b = 4, the value of not (a == b) is:
    (a) True(b) False(c) None(d) Error
  10. In an if-elif-else ladder, how many of its blocks run during one execution?
    (a) Zero(b) Exactly one(c) All matching(d) The first two

Section B — Find the Output (1 × 8 = 8)

  1. a = 10
    if a > 5:
        print("Big")
    print("Done")
    Output:
  2. x = 0
    if x:
        print("Yes")
    else:
        print("No")
    Output:
  3. m = 72
    if m >= 90: g = "A"
    elif m >= 60: g = "B"
    elif m >= 33: g = "C"
    else: g = "F"
    print(g)
    Output:
  4. a, b = 5, 8
    print(a if a > b else b)
    Output:
  5. n = 7
    if n > 0 and n % 2 != 0:
        print("positive odd")
    else:
        print("other")
    Output:
  6. s = ""
    if s:
        print("non-empty")
    else:
        print("empty")
    Output:
  7. y = 15
    if y % 3 == 0:
        if y % 5 == 0:
            print("FizzBuzz")
        else:
            print("Fizz")
    else:
        print("-")
    Output:
  8. a = 4
    if a == 4:
        print("P", end="")
    if a > 0:
        print("Q")
    Output:

Section C — Find the Error (1 × 6 = 6)

Each snippet has exactly one error. Mark the line number and write the corrected line.

  1. 1  age = 18
    2  if age = 18:
    3      print("adult")
    Wrong line:   Correction:
  2. 1  marks = 50
    2  if marks > 40
    3      print("Pass")
    Wrong line:   Correction:
  3. 1  n = 10
    2  if n > 0:
    3  print("positive")
    Wrong line:   Correction:
  4. 1  x = 5
    2  if x > 0:
    3      print("+ve")
    4  elseif x < 0:
    5      print("-ve")
    Wrong line:   Correction:
  5. 1  a, b = 3, 7
    2  big = a if a > b b
    3  print(big)
    Wrong line:   Correction:
  6. 1  age = 20
    2  if age >= 18:
    3      print("vote")
    4  else age < 18:
    5      print("wait")
    Wrong line:   Correction:

Section D — Short Answer (2 × 5 = 10)

  1. Differentiate = and == with one example each.

Chapter 14 Worksheet — (continued)

Sheet 2 (Back)

Section D — Short Answer (continued) (2 × 5 = 10)

  1. What is the difference between if-elif-else and a sequence of separate if statements?
  2. Why is indentation important in Python conditionals? Give one example of an error caused by wrong indentation.
  3. Rewrite this nested if as a single if using and:
    if x > 0:
        if x < 100:
            print("in range")
  4. Convert the following if-else into a one-line conditional expression:
    if n >= 0:
        sign = "+"
    else:
        sign = "-"

Section E — Code Tasks (16 marks)

Each program should read input from the user and produce a clear output. Use a separate sheet if you need more space.

  1. (3) Write a Python program that reads a year and prints "Leap year" or "Not a leap year". A year is a leap year if it is divisible by 4, except century years (divisible by 100) which must also be divisible by 400.
  2. (3) Write a Python program to read three numbers and print the largest. Use only if / else — no built-in max().
  3. (3) A bank gives the following discount on the credit-card annual fee depending on the customer's age band:
    • below 21 → 50%   • 21-40 → 20%   • 41-60 → 10%   • above 60 → 30%
    Write a program that reads the age and the original fee, and prints the final fee after discount.
  4. (4) Write a Python program that reads a single character and prints whether it is an uppercase letter, lowercase letter, digit, or special character. Hint: compare ranges using 'A' <= ch <= 'Z'.
  5. (3) Write a Python program that reads a number and prints whether it is positive even, positive odd, negative even, negative odd, or zero.