A traffic signal lets vehicles go when the light is green and makes them wait otherwise. Programs constantly make such choices: pass or fail, valid or invalid, positive or negative. The structure that makes a choice is called selection.
The Idea of Selection
Definition: Selection. A control structure that chooses which instructions to carry out according to whether a condition is true or false.
A condition is an expression whose value is either true or false. Conditions are usually formed with the relational operators shown in Table 2.2.
Table 2.2: Relational operators in C
| Operator | Meaning | Example | Value when a is 7 |
|---|---|---|---|
< |
Less than | a < 5 |
false |
<= |
Less than or equal to | a <= 7 |
true |
> |
Greater than | a > 5 |
true |
>= |
Greater than or equal to | a >= 10 |
false |
== |
Equal to | a == 7 |
true |
!= |
Not equal to | a != 7 |
false |
The if Statement
The if statement carries out a block of statements only when its condition is true. When the condition is false, the block is skipped and execution continues with the statement after it.
if (condition)
{
statements;
}
The if-else Statement
The if-else statement provides two paths. When the condition is true, the first block runs. When it is false, the second block runs. Exactly one of the two blocks is carried out, never both and never neither.
Writing Good Conditions
A condition should express the decision directly. To decide whether a student has passed with a pass mark of 40, the condition is mark >= 40. Two points deserve care.
First, the operator == tests equality, while = assigns a value. Writing if (a = 5) assigns 5 to a and is then treated as true, which is almost never what was intended.
Second, braces should enclose the block even when it contains one statement. Without braces, only the first statement after if belongs to it, and a statement added later will run unconditionally.
Example 2.2: Pass or fail
Problem: Read a mark out of 100 and display "Pass" if it is 40 or more, and "Fail" otherwise.
Solution:
- Read the mark.
- Test the condition mark >= 40.
- Display the message for the branch selected.
#include <stdio.h>
int main(void)
{
int mark;
printf("Enter mark: ");
scanf("%d", &mark);
if (mark >= 40)
{
printf("\nPass\n");
}
else
{
printf("\nFail\n");
}
return 0;
}
Input:
40
Output:
Enter mark:
Pass
Result: The mark 40 is a boundary case. Because the condition uses >=, it is correctly reported as a pass. With > it would wrongly be reported as a fail.
Common mistakes
- Writing
=in a condition where==is intended. - Placing a semicolon immediately after
if (condition), which ends the statement there. - Choosing
>when the task requires>=, so that the boundary value is handled wrongly.
Key takeaways
- Selection chooses a path according to a true-or-false condition.
ifruns a block only when the condition is true.if-elseruns exactly one of two blocks.- Boundary values must be tested to confirm that the correct relational operator was used.
Practice questions
- How many blocks of an if-else statement are carried out in one execution?
- What is the difference between = and == in C?
- Write a condition that is true when n is an even number.
Answers to the practice questions
- Exactly one: the if block when the condition is true, otherwise the else block.
- = assigns a value to a variable; == tests whether two values are equal.
- n % 2 == 0
Sources
- Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit II
