Control Statements in C
1. Conditional Statements!
In generally, a C program runs line by line. But sometimes you want your program to make decisions. That’s where conditional statements (also called decision-making statements) come in.
Example, suppose you are going outside, there is two conditions:
If it’s raining → take an umbrella.
If it’s sunny → wear sunglasses.
So, you are deciding based on a condition. In the same way, C uses conditional statements to decide what to do.
⭐ Now start with types of Conditional Statements.
1️⃣ if statement
Used when you want to do something only if the condition is true.
// Structure
if (condition) {
// code runs only if condition is true
}
🍭Example
// code<>
int age = 20;
if (age >= 18) {
printf("You can vote.\n");
}
Output
You can vote.
2️⃣ if-else statement
Used when you want two possible outcomes (true or false).
// Structure
if (condition) {
// code if true
} else {
// code if false
}
🍭Example
// code<>
int age = 16;
if (age >= 18) {
printf("You can vote.\n");
} else {
printf("You cannot vote.\n");
}
Output:
You cannot vote.
3️⃣ nested if statement
An if inside another if. Used when you want to check multiple conditions.
🍭Example:
// code<>
int marks = 85;
if (marks >= 60) {
if (marks >= 90) {
printf("Grade: A+\n");
} else {
printf("Grade: A\n");
}
}
Output:
Grade: A
4️⃣ if-else-if ladder
Used when you have many conditions.
🍭Example:
// code<>
int marks = 72;
if (marks >= 90) {
printf("Grade: A\n");
} else if (marks >= 75) {
printf("Grade: B\n");
} else if (marks >= 50) {
printf("Grade: C\n");
} else {
printf("Fail\n");
}
Output:
Grade: C
5️⃣ switch statement
A cleaner way to handle multiple choices instead of many if-else.
🍭Example:
// code<>
int day = 3;
switch(day) {
case 1: printf("Monday\n"); break;
case 2: printf("Tuesday\n"); break;
case 3: printf("Wednesday\n"); break;
default: printf("Invalid Day\n");
}
Output:
Wednesday
At a glance: –
Conditional statements = decision makers of C.
Types:
if(one condition)if-else(two outcomes)nested if(condition inside condition)if-else-if ladder(multiple conditions)switch(multiple fixed choices)
An important analogy: –
Just like you decide daily whether to take an umbrella, wear sunglasses, or stay home, a C program uses conditional statements to make decisions.
2. Looping Statements!
Looping Statements in C (Iteration):
By default, a program runs line by line and then ends. But what if you want to repeat a task multiple times? That’s where loops (looping statements) come in.
🍭Example:
Think about brushing your teeth:
You move the brush left → right → left → right repeatedly.
Instead of doing it manually one by one, you repeat the same action in a loop.
In the same way, loops in C allow you to run code again and again until a condition is false.
Types of Looping Statements in C: –
1️⃣ while loop
Repeats code as long as condition is true.
// Structure
while (condition) {
// code to execute
}
🍭Example:
// code<>
int i = 1;
while (i <= 5) {
printf("%d\n", i);
i++;
}
Output:
1
2
3
4
5
2️⃣ do…while loop
Similar to while, but runs at least once, even if condition is false.
// Structure
do {
// code
} while (condition);
🍭Example:
// code<>
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 5);
Output:
1
2
3
4
5
3️⃣ for loop
Best when you know exactly how many times to repeat.
// Structure
for (initialization; condition; update) {
// code
}
🍭Example:
// code<>
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
Output:
1
2
3
4
5
⭐Teacher says “Write this sentence 5 times” → you know the count.
4️⃣ Nested Loops
A loop inside another loop.
🍭Example: Printing a star pattern ‘⭐’
// code
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
*
* *
* * *
while(Repeat until condition fails (unknown count)).do…while(Run at least once, then check).for(Best for fixed repetitions).nested loops(Loop inside a loop (used for patterns, tables, etc.)).
An important analogy:
while(Keep eating until you are full).do…while(Taste food at least once).for(Counted repetition (do exercise 10 times)).
3. Jumping statements!
In C, jump statements are used to change the normal flow of a program. Instead of going line by line, they allow you to:
Exit from a loop early
Skip some part of code
Jump to another location in the program
🍭Example:
Imagine you are watching a movie :
If it’s boring → you exit the theatre early (
break).If a scene looks unimportant → you skip to the next scene (
continue).If you want to jump directly to a specific scene → you jump to that part (
goto).
Types of Jump Statements in C: –
1️⃣ break statement
Used to exit a loop or switch immediately.
// Structure
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // loop stops here
}
printf("%d\n", i);
}
Output:
1
2
3
4
🍭 Leaving the cinema hall, when the movie becomes boring.
2️⃣ continue statement
Skips the current iteration and moves to the next iteration.
// Structure
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // skip 3
}
printf("%d\n", i);
}
1
2
4
5
Not recommended in modern coding (makes code messy), but still part of C.
// Structure
int i = 1;
start:
printf("%d\n", i);
i++;
if (i <= 5) {
goto start; // jump back to label
}
Output:
1
2
3
4
5
Using a shortcut road to reach a place instead of following the regular route.
At a glance:
break → exits loop/switch immediately.
continue → skips the current step and moves to the next.
goto → jumps to a label (used rarely).
Analogy:
break= quitting the game early.continue= skipping a level, but keep playing.goto= teleporting to another level.
4. Flowcharts & Dry runs!
When learning C, just writing code is not enough. To understand how a program works, programmers use two powerful tools:
Flowcharts → Pictorial representation of logic.
Dry Run → Step-by-step manual execution of the code.
1. Flowcharts in C:
A flowchart is a diagram that shows the flow of a program using different shapes like rectangles, diamonds, and arrows.
🔗Symbols in Flowchart:
Oval (Ellipse) → Start / End
Rectangle → Process (like calculation)
Diamond → Decision (like if/else)
Parallelogram → Input/Output
🍭Example: Think about making tea.
Start
Boil water
Add tea leaves
If milk available?
Yes → add milk
No → continue without milk
Serve
End
This can be shown in a flowchart 👇
(START) → [Boil Water] → [Add Tea Leaves] → ◇(Milk available?)
YES → [Add Milk]
NO → [Skip]
→ [Serve Tea] → (END)
Same way, before coding in C, programmers often draw a flowchart to plan logic.
2. Dry Run in C:
A dry run means manually executing the program line by line on paper, without actually running it on the computer.
It helps in debugging and checking logic.
🍭Example: Imagine your teacher gives you math homework :
Before solving in your notebook, you think step by step in your mind → this is a dry run.
Dry Run: –
// code<>
#include <stdio.h>
int main() {
int i, sum = 0;
for (i = 1; i <= 5; i++) {
sum = sum + i;
}
printf("%d", sum);
return 0;
}
Dry Run Table:
| Step | i | sum (before) | sum (after) |
|---|---|---|---|
| 1 | 1 | 0 | 1 (0+1) |
| 2 | 2 | 1 | 3 (1+2) |
| 3 | 3 | 3 | 6 (3+3) |
| 4 | 4 | 6 | 10 (6+4) |
| 5 | 5 | 10 | 15 (10+5) |
At a glance: –
Flowchart = Diagram to plan program logic.
Dry Run = Manually checking how variables change step by step.
Analogy:
Flowchart = Google Maps route before travel.
Dry Run = Walking through the path mentally before going.