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.
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.
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.
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.
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.
| 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 |
| 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 |
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.