ЁЯТ╗
тМия╕П
ЁЯЦ▒я╕П
ЁЯЦея╕П
ЁЯТ╛
тЖР Back to Dashboard
Font Size:

1. Introduction

Normally, a Python program runs from top to bottom, executing each line in order. But many problems need the program to make decisions and repeat actions. Control structures are the parts of a programming language that change the flow of the program. There are three main types: sequence, selection and repetition. Sequence means running the statements in order, selection means choosing between different paths with conditions, and repetition means repeating a block of code, which is covered by loops.

The most important selection statements in Python are the if, elif and else statements. With these, a program can check a condition and decide which block of code to run. For example, a program can check whether a number is even or odd, or whether a student has passed or failed. This chapter explains conditions, the if statement, the if-else statement, the if-elif-else chain, nested if statements, and how to use indentation correctly in Python.

2. Conditions and Boolean Expressions

A condition is a comparison or logical expression that evaluates to either True or False. Conditions are written using comparison operators such as ==, !=, >, <, >= and <=, and they can be joined using the logical operators and, or and not. For example, marks >= 40 is a condition, and marks >= 40 and attendance >= 75 is a combined condition. The result of every condition is a boolean value.

Conditions are the heart of decision-making. They use the values stored in variables and compare them to make the program behave differently in different situations. For example, if age >= 18, a person can vote; otherwise, they cannot. Python evaluates the condition, and if it is True, one set of statements runs, and if it is False, a different set runs. Writing clear and correct conditions is one of the most important skills in programming.

3. The if Statement

The if statement is the simplest decision-making statement. It executes a block of code only if a condition is True. The syntax is simple: we write the keyword if, followed by the condition, followed by a colon, and then the block of statements indented below it. For example:

if marks >= 40: print("Pass")

In this example, the message "Pass" is printed only if the value of marks is 40 or more. If the condition is False, Python skips the block and continues with the next statement after the if block. The indentation is very important in Python: the block of code that belongs to the if statement must be indented, usually by four spaces, and all statements in the block must have the same indentation.

4. The if-else and if-elif-else Statements

The if-else statement handles both cases: one block runs when the condition is True, and a different block runs when it is False. For example:

if marks >= 40: print("Pass") else: print("Fail")

Here, if the condition is True, "Pass" is printed; otherwise, "Fail" is printed. This ensures the program always does one of the two actions.

When there are more than two possibilities, we use the if-elif-else chain. The elif keyword stands for "else if", and it lets us check several conditions one after another. For example, a program can give grades: if marks >= 90 print "A", elif marks >= 75 print "B", elif marks >= 60 print "C", else print "D". Only the block of the first condition that is True runs, and the rest are skipped.

5. Nested if Statements and Good Practice

An if statement can be placed inside another if statement, which is called a nested if. This is used when a decision depends on more than one condition. For example, a program may first check if a student has paid the fees, and if yes, then check if the attendance is enough to be allowed into the exam. Nested if statements must be indented carefully so that the program knows which block belongs to which condition.

While writing decision-making code, we should keep a few good practices in mind. The conditions should be clear and simple, and the indentation must be consistent. We should use elif instead of many nested ifs whenever possible, because it makes the code easier to read. We should test our program with different inputs, including the boundary values such as exactly 40 in a pass-fail program. Well-written control structures make programs correct, clear and easy to maintain.

Quick Revision Tables

Table 1: Selection Statements

Statement Purpose
if Runs a block when the condition is True
if-else Runs one block if True, another if False
if-elif-else Checks several conditions in order
Nested if An if inside another if

Table 2: Comparison and Logical Operators

Operator Meaning
== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
and True only if both are true
or True if at least one is true
not Reverses the value

Mind Map

flowchart TD A["Python Control Structures"] --> B["Sequence"] A --> C["Selection"] A --> D["Repetition (Loops)"] C --> C1["if"] C --> C2["if-else"] C --> C3["if-elif-else"] C --> C4["Nested if"] C1 --> C1a["Condition True"] C2 --> C2a["Two paths"] C3 --> C3a["Multiple checks"] C4 --> C4a["If inside if"]

Important Diagrams (SVG)

Diagram 1: Flow of the if-else Statement

Flow of the if-else Statement START CONDITION? marks >= 40 True print("Pass") if block runs False print("Fail") else block runs END Golden Rule: if-else runs one block when the condition is True and another when it is False. Only one of the two blocks ever runs in an if-else statement.

Diagram 2: if-elif-else Chain for Grades

The if-elif-else Chain for Grades MARKS if marks >= 90 : Grade A elif marks >= 75 : Grade B elif marks >= 60 : Grade C else : Grade D Golden Rule: In an if-elif-else chain, only the first true condition's block runs. The else block runs only when none of the conditions is true.

Common Mistakes

  1. Using a single equals sign in a condition. Conditions use ==, while = is for assignment.
  2. Forgetting the colon at the end of the if line. Every if, elif and else line must end with a colon.
  3. Not indenting the block of the if statement. The statements inside the block must be indented.
  4. Using different indentation for statements in the same block. All statements in one block must be indented equally.
  5. Writing the else block before the if block. The if comes first and the else comes after.
  6. Using multiple ifs when an if-elif chain is better. elif stops checking after a true condition, saving time.
  7. Confusing and with or. and needs both conditions true, while or needs only one.
  8. Forgetting that comparison with == compares values, and writing float and int comparisons incorrectly.
  9. Nesting too many if statements, making the code confusing. Using elif is often clearer.
  10. Forgetting to test the boundary values, such as marks exactly equal to 40.

Exam Tips

  1. Learn the three control structures: sequence, selection and repetition.
  2. Remember the syntax of if, if-else and if-elif-else, including the colon and indentation.
  3. Know the difference between = (assignment) and == (comparison).
  4. Be able to write a pass-fail program using if-else.
  5. Be able to write a grading program using if-elif-else.
  6. Understand when to use a nested if and when to use elif.
  7. Practise writing conditions with comparison and logical operators.

Conclusion

Control structures give our programs the power to make decisions. With the if, if-else and if-elif-else statements, a program can check conditions and choose the right action, from printing pass or fail to giving the correct grade. Careful use of colons, indentation and conditions keeps our decision-making code correct and readable. In the next chapter, we will learn how to repeat actions using loops, and how to organise our code using functions, to write programs that can handle much bigger tasks.