Control Statements in C (2026) – if, if-else, switch with Examples for Beginners


Control Statements in C are one of the most important topics for beginners learning C programming. They help a program make decisions, repeat actions, and control execution flow. In this complete beginner-friendly tutorial (2026), you will learn everything about control statements with simple explanations and examples.

In the previous article, we learned about Operators in C, Variables and Datatypes and introduction to c and how to perform calculations and comparisons. Before learning control statements, make sure you understand Variables and Datatypes in C.

 Check this The Knowledge Space Hub

In real life, we make decisions every day. For example:

  • If it is raining, we take an umbrella.

  • If we are thirsty, we drink water.

  • If marks are above 30, the student passes.

In all these situations, an action happens only when a condition is true.

Programming works in the same way.

A C program does not automatically know what to do in different situations. We must tell

the program:

  • When to execute a particular statement

  • When to skip a statement

  • When to choose between different options

  • When to repeat something again and again

For example:

  • Execute code only if a condition is true

  • Choose one option out of many

  • Repeat actions based on a condition

To control these decisions and actions, we use Control Statements in C. Control statements help the program decide the flow of execution. Without control statements, a program would simply run from top to bottom without thinking or making decisions. So, control statements are the foundation of logical programming in C.

What Are Control Statements in C?

Control statements are special statements in C that control how a program runs. when we write a C program, it normally executes line by line from top to bottom. but in real programs, we don’t always want every line to execute. sometimes we want:

  • A statement to run only if a condition is true

  • A block of code to run again and again

  • The program to choose one option from many

  • The program to skip certain lines

To control all these situations, we use control statements. in simple words, control statements control the flow of a program. they help the program decide:

  • Which statement should execute

  • When it should execute

  • How many times it should execute

For example: If a student’s marks are greater than 30

If above 30 → print “Pass”
If not → print “Fail”

Here, the program must decide what to print. That decision is made using control statements.

Without control statements, a program would:

  • Execute every line one by one

  • Never make decisions

  • Never repeat actions

  • Never skip unwanted code

That means it would not be useful in real-world applications. So, control statements are very important because they give intelligence and logical thinking ability to a C program.

Types of Control Statements in C

There are three main types:

  1. Decision-Making Statements

  2. Looping Statements

  3. Jump Statements

In this article, we will focus on Decision-Making Control Statements.

1️⃣ Decision-Making Statements in C

Decision-making statements allow the program to execute certain blocks of code based on conditions.

They include:

  • if statement

  • if-else statement

  • else-if ladder

  • nested if

  • switch statement

Let’s understand each one clearly.

1. if Statement in C

The if statement executes code only when the condition is true.

Syntax:

if(condition) {
// code
}

Example:

#include <stdio.h>

int main() {
int age = 18;

if(age >= 18) {
printf("You are eligible to vote.");
}

return 0;
}

Output:

You are eligible to vote.

If the condition is false, nothing happens.

2. if-else Statement in C

Used when we want to execute one block if condition is true and another block if false.

Syntax:

if(condition) {
// true block
}
else {
// false block
}

Example:

#include <stdio.h>

int main() {
int number = 5;

if(number % 2 == 0)
printf("Even Number");
else
printf("Odd Number");

return 0;
}

3. else-if Ladder in C

Used when we have multiple conditions.

Syntax:

if(condition1) {
}
else if(condition2) {
}
else {
}

Example: Largest of Three Numbers

#include <stdio.h>

int main() {
int a = 10, b = 20, c = 15;

if(a > b && a > c)
printf("A is largest");
else if(b > a && b > c)
printf("B is largest");
else
printf("C is largest");

return 0;
}

4.Nested if Statement

An if statement inside another if statement.

Example:

#include <stdio.h>

int main() {
int age = 20;
int citizen = 1;

if(age >= 18) {
if(citizen == 1) {
printf("Eligible to vote");
}
}

return 0;
}

Used in:

  • Login systems

  • Eligibility checks

  • Banking applications

5.switch Statement in C

The switch statement is used when we want to select one option from many.

It works best when checking a single variable against multiple values.

Syntax:

switch(expression) {
case value1:
// code
break;

case value2:
// code
break;

default:
// code
}

Example:

#include <stdio.h>

int main() {
int day = 2;

switch(day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
default:
printf("Invalid day");
}

return 0;
}

Difference Between if-else and switch

Both if-else and switch are decision-making control statements in C. They help the program choose which block of code to execute. But they are used in different situations.

Let’s understand the difference clearly.



1.Type of Conditions Used

🔹 if-else

The if-else statement works with logical conditions.

You can use:

  • Relational operators (<, >, ==, !=)

  • Logical operators (&&, ||)

  • Complex expressions

Example:

if(age >= 18 && citizen == 1)

Here, we are checking multiple conditions together. So, if-else is suitable when conditions are complex.

🔹 switch

The switch statement works with constant values only. It compares one variable with fixed values.

Example:

switch(day)

Each case must be a constant like:

case 1:
case 2:

You cannot write:

case day > 5 (Not allowed)

So switch cannot handle logical expressions.

 2.Handling Ranges

🔹 if-else

if-else can handle ranges easily. Example:

if(marks >= 90)

Here we are checking a range (marks greater than or equal to 90). You can also write:

if(marks >= 50 && marks < 75)

This is very useful for grading systems.

🔹 switch

Switch cannot directly handle ranges. You cannot write:

case marks >= 90

Switch only checks exact matches. So switch is not suitable for range-based conditions.

3.Flexibility

🔹 if-else

  • Can handle multiple variables

  • Can use complex logic

  • Can combine many conditions

  • Very flexible

Used in:

  • Login validation

  • Eligibility checking

  • Result processing

🔹 switch

  • Works with one variable only

  • Best for menu-driven programs

  • Cleaner when many fixed options exist

Used in:

  • Calculator menu

  • Day of week selection

  • ATM menu system

4.Speed and Performance

In some cases, switch may be slightly faster than if-else when there are many constant options.

Why?

Because switch directly jumps to the matching case. But in small programs, the speed difference
is very small. So, beginners don’t need to worry much about performance.

Real-Life Applications of Control Statements

Control statements are used in:

✔ ATM systems
✔ Login authentication
✔ Menu-driven programs
✔ Result processing systems
✔ Banking software
✔ Online forms validation

Every real software uses decision-making statements.

 Program: Check Whether a Character is Vowel or Consonant

#include <stdio.h>

int main() {
char ch;

printf("Enter a character: ");
scanf("%c", &ch);

if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||
ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
printf("Vowel");
else
printf("Consonant");

return 0;
}

Sample Output:

Enter a character: e
Vowel

Program: Simple ATM Withdrawal Check

#include <stdio.h>

int main() {
int balance = 5000, withdraw;

printf("Enter withdrawal amount: ");
scanf("%d", &withdraw);

if(withdraw <= balance)
printf("Transaction Successful");
else
printf("Insufficient Balance");

return 0;
}

Sample Output:

Enter withdrawal amount: 2000
Transaction Successful

Practice Programs for Students

  1. Write a C program to check whether a number is positive, negative, or zero.

  2. Write a C program to calculate grade based on marks.

  3. Write a program to find the smallest of three numbers.

  4. Write a menu-driven calculator using switch.

  5. Write a program to check whether a year is a leap year.

FAQ Section

What are control statements in C?

Control statements are statements that control the execution flow of a program based on conditions.

What are the types of control statements?

Decision-making, looping, and jump statements.

What is the difference between if and switch?

if handles complex conditions. switch is used for fixed values.

Can we use logical operators inside if?

Yes. Logical operators like && and || are commonly used inside if conditions.

Why are break statements used in switch?

To stop execution after a matching case.

Conclusion

Control statements are the backbone of logical programming in C. They allow programs to:

✔ Make decisions
✔ Execute different blocks of code
✔ Build real-world applications

After mastering control statements, the next step is learning Loops in C, which allow repetition of code blocks.

Related Posts:

Comments

Post a Comment

Popular posts from this blog