Control Statements in C: Sequence, Selection & Repetition Explained

In programming, we often need to control the way statements are executed. If every program only executed line by line, it would be very limited. Thankfully, C provides **Control Statements** — instructions that allow the programmer to decide the order in which statements are executed, whether to skip certain parts, repeat blocks of code, or make decisions based on conditions.
In this detailed guide, we will explore the three main types of control statements in C: **Sequence, Selection, and Repetition**. Along with definitions, we’ll look at practical examples, step-by-step explanations, and real-world applications. This lecture is designed to help students deeply understand these concepts and apply them confidently in programming.
Control Statement
The statement this is used to control the flow of the execution of the program.
Types of Control Statements in C
C provides three fundamental types of control statements that form the backbone of structured programming:
1. Sequence (Sequential Control)
Definition: The sequence is the default execution mode in C. It means statements are executed one after the other in the order they appear.
How it works: No conditions, no loops, no jumps. Each line executes once in order.
#include <stdio.h>
int main() {
int num;
printf("Enter number: ");
scanf("%d", &num);
printf("You entered %d", num);
return 0;
}
Explanation: In this program, the statements run one after another:
Extra[You can skip this during writing on notebooks. It is just an extra knowledge.]- The program asks the user for a number.
- The number is read from input.
- The number is displayed back to the user.
This is a **sequential flow** without any branching or looping.
2. Selection (Decision-Making)
Definition: Selection statements allow the program to choose different paths based on conditions.
Purpose: To make decisions in the program.
Common statements: if
, if-else
, if-else if-else
, switch
.
#include <stdio.h>
int main() {
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
if (marks >= 50) {
printf("Pass");
} else {
printf("Fail");
}
return 0;
}
Explanation: This program checks the condition:
- If
marks >= 50
, it prints "Pass". - Otherwise, it prints "Fail".
Thus, the flow of execution depends on the **decision** made at runtime.
Extra[You can skip this during writing on notebooks. It is just an extra knowledge.]Example: Switch Statement
#include <stdio.h>
int main() {
int choice;
printf("Enter 1 for Tea, 2 for Coffee: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("You chose Tea");
break;
case 2:
printf("You chose Coffee");
break;
default:
printf("Invalid choice");
}
return 0;
}
Explanation: A switch
is useful when multiple possible conditions exist. Here:
- If the user enters 1 → "You chose Tea".
- If the user enters 2 → "You chose Coffee".
- For any other input → "Invalid choice".
3. Repetition (Looping)
Definition: Loops are used to execute a block of code repeatedly until a condition becomes false.
Purpose: To save effort and avoid writing the same statement multiple times.
Common loops: for
, while
, do-while
.
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}
Explanation: This loop prints numbers from 1 to 5. Instead of writing five printf
statements, the loop executes the statement repeatedly.
Example: While Loop
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}
Explanation: The while
loop keeps running as long as i <= 5
. Each time, it prints the value and increments i
.
Example: Do-While Loop
#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 5);
return 0;
}
Explanation: A do-while
loop guarantees that the block runs at least once, even if the condition is false.
Summary Table
Type | Purpose | Examples in C |
---|---|---|
Sequence | Runs statements in order | Normal statement execution |
Selection | Chooses path based on condition | if, if-else, switch |
Repetition | Repeats code until a condition is false | for, while, do-while |
Quick Notes – Control Statements in C
- Control Statements – Instructions that control the flow of execution in a C program.
- Three Main Types:
- Sequence: Default execution; runs statements one after another in order.
- Selection: Decision-making; chooses which code block to run based on conditions (if, if-else, switch).
- Repetition: Looping; repeats a block of code multiple times until a condition is false (for, while, do-while).
- Purpose: To make programs flexible, interactive, and efficient by adding decision-making and looping capabilities.
MCQs
1. Which control statement type runs statements one after another?
- a) Sequence
- b) Selection
- c) Repetition
- d) Function
Answer: a) Sequence
2. Which control statement allows decision-making?
- a) Sequence
- b) Selection
- c) Looping
- d) Recursion
Answer: b) Selection
3. Which control statement repeats a block of code?
- a) Looping
- b) Selection
- c) Sequence
- d) Condition
Answer: a) Looping
FAQs on Control Statements in C
Q1: What are control statements in C?
Control statements are instructions that direct the order of execution of statements in a C program. They allow conditional execution and looping.
Q2: Why are control statements important?
They make programs flexible and efficient by allowing decision-making and repetition instead of writing the same code multiple times.
Q3: What is the difference between sequence and selection?
Sequence follows a straight flow of execution, while selection makes decisions based on conditions.
Q4: What is the main difference between while and do-while loop?
In while, the condition is checked first. In do-while, the block runs at least once before checking the condition.
Q5: Can we use nested control statements in C?
Yes, control statements can be nested, such as using a loop inside an if-statement.
Q6: When should we use switch instead of if-else?
Switch is preferred when we need to compare a variable against multiple constant values, while if-else is better for range-based conditions.
Q7: What happens if we forget to use break in switch?
The control falls through to the next case, executing multiple cases unintentionally.
Q8: Can loops run infinitely?
Yes, if the terminating condition is never met. For example, while(1)
is an infinite loop.
Q9: Are goto statements also control statements?
Yes, but they are generally discouraged as they make the code unstructured.
Q10: Which is the most commonly used control statement?
The for
loop is one of the most commonly used because it’s compact and well-suited for counting iterations.