C-Program
Control Statements in C: A Beginner's Guide to Decision-Making and Loops
Last Updated on: 9th Jul 2025 19:51:27 PM
Welcome to this exciting tutorial on Control Statements in C! If you're learning C programming, control statements are like the steering wheel of your code—they guide the flow of execution based on conditions or repeat tasks efficiently. In this guide, we'll break down each control statement with clear explanations, relatable examples, and their outputs to make learning fun and straightforward. Whether you're a beginner or brushing up your skills, let's dive into the world of decision-making and looping in C!
What Are Control Statements?
Control statements in C allow you to control the flow of your program. They help you make decisions (e.g., "Should I execute this code?") or repeat actions (e.g., "Keep doing this until a condition is met"). There are two main categories:
-
Decision-Making Statements: if, if-else, nested if, switch.
-
Looping Statements: for, while, do-while.
-
Jump Statements: break, continue.
Let’s explore each one with examples that are easy to grasp and fun to try!
1. Decision-Making Statements
Decision-making statements let your program choose different paths based on conditions. Think of them as crossroads where your code decides which way to go!
1.1 The if Statement
The if statement checks a condition. If the condition is true, the code inside the if block executes.
Syntax:
if (condition) {
// Code to execute if condition is true
}
Example: Check if a number is positive.
#include <stdio.h>
int main() {
int num = 10;
if (num > 0) {
printf("%d is a positive number.\n", num);
}
return 0;
}
Output: 10 is a positive number.
Explanation: The condition num > 0 is true (since 10 is greater than 0), so the printf statement inside the if block runs. If num was negative or zero, nothing would print.
1.2 The if-else Statement
The if-else statement provides an alternative path if the condition is false.
Syntax:
if (condition) {
// Code if condition is true
} else {
// Code if condition is false
}
Example: Check if a number is even or odd.
#include <stdio.h>
int main() {
int num = 7;
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}
Output:
7 is odd.
Explanation: The condition num % 2 == 0 checks if the number is divisible by 2. Since 7 % 2 equals 1 (not 0), the else block executes.
1.3 The nested if Statement
You can nest if statements inside other if statements to check multiple conditions.
Syntax:
if (condition1) {
if (condition2) {
// Code if both conditions are true
}
}
Example: Check if a student passed both subjects.
#include <stdio.h>
int main() {
int math = 75, science = 80;
if (math >= 50) {
if (science >= 50) {
printf("You passed both subjects!\n");
}
}
return 0;
}
Output:
You passed both subjects!
Explanation: The outer if checks if math >= 50. If true, the inner if checks science >= 50. Since both conditions are true, the message is printed.
1.4 The switch Statement
The switch statement is used when you have multiple conditions to check against a single variable. It’s like a menu where you pick one option.
Syntax:
switch (expression) {
case value1:
// Code
break;
case value2:
// Code
break;
default:
// Code if no case matches
}
Example: Display the day of the week based on a number.
#include <stdio.h>
int main() {
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");
}
return 0;
}
Output:
Wednesday
Explanation: The value of day (3) matches case 3, so "Wednesday" is printed. The break statement ensures the program exits the switch block. If no case matches, the default block runs.
2. Looping Statements
Loops let your program repeat a block of code multiple times. Imagine automating repetitive tasks like printing numbers or processing data!
2.1 The for Loop
The for loop is great when you know how many times to repeat a task.
Syntax:
for (initialization; condition; update) {
// Code to repeat
}
Example: Print numbers from 1 to 5.
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
return 0;
}
Output:
1 2 3 4 5
Explanation:
-
initialization: int i = 1 sets the counter.
-
condition: i <= 5 checks if the loop should continue.
-
update: i++ increments the counter. The loop prints i until the condition becomes false.
2.2 The while Loop
The while loop repeats as long as a condition is true. Use it when the number of iterations isn’t fixed.
Syntax:
while (condition) {
// Code to repeat
}
Example: Print numbers until a sum exceeds 10.
#include <stdio.h>
int main() {
int sum = 0, num = 1;
while (sum <= 10) {
sum += num;
printf("Number: %d, Sum: %d\n", num, sum);
num++;
}
return 0;
}
Output:
Number: 1, Sum: 1
Number: 2, Sum: 3
Number: 3, Sum: 6
Number: 4, Sum: 10
Number: 5, Sum: 15
Explanation: The loop adds num to sum and prints both until sum exceeds 10.
2.3 The do-while Loop
The do-while loop is similar to while, but it guarantees at least one execution because the condition is checked after the loop body.
Syntax:
do {
// Code to repeat
} while (condition);
Example: Ask for a positive number.
#include <stdio.h>
int main() {
int num;
do {
printf("Enter a positive number: ");
scanf("%d", &num);
} while (num <= 0);
printf("You entered: %d\n", num);
return 0;
}
Output (if user inputs -5, then 7):
Enter a positive number: -5
Enter a positive number: 7
You entered: 7
Explanation: The loop keeps asking for input until a positive number is entered. The do block runs at least once.
3. Jump Statements
Jump statements alter the flow of loops or switch cases.
3.1 The break Statement
The break statement exits a loop or switch case immediately.
Example: Stop a loop when a condition is met.
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
printf("%d ", i);
}
return 0;
}
Output:
1 2 3 4
Explanation: The loop stops when i equals 5 due to the break statement.
3.2 The continue Statement
The continue statement skips the rest of the current loop iteration and moves to the next one.
Example: Skip odd numbers in a loop.
#include <stdio.h>
int main() {
for (int i = 1; i <= 6; i++) {
if (i % 2 != 0) {
continue;
}
printf("%d ", i);
}
return 0;
}
Output:
2 4 6
Explanation: When i is odd, continue skips the printf statement, so only even numbers are printed.
Why Are Control Statements Awesome?
Control statements are the backbone of programming logic. They let you:
-
Make smart decisions based on user input or data.
-
Automate repetitive tasks efficiently.
-
Create dynamic, interactive programs (like games or calculators!).
Pro Tip: Practice these examples by tweaking the conditions or outputs. Try writing a program that uses a switch to create a simple menu or a for loop to calculate factorials!