Control Statements in C Language
1. What are Control Statements?
Control statements are statements used to change the normal flow of execution of a program.
Normal Flow of a Program
Normally, a C program executes statements:
- One after another
- From top to bottom
- Each statement is executed exactly once, unless the flow is changed
#include <stdio.h>
int main()
{
printf("First\n");
printf("Second\n");
printf("Third\n");
return 0;
}
Output:
First
Second
Third
The statements are executed sequentially from top to bottom.
2. Types of Control Statements
There are three major types of control statements in C:
| Type | Purpose | Examples |
|---|---|---|
| Selection / Decision | Chooses which block of code should execute. | if, if-else, switch |
| Loops / Iterations | Repeats a block of code. | for, while, do-while |
| Jumps | Transfers control from one part of the program to another. | break, continue, goto, return |
3. Selection Statements in C
Selection statements are used when a program has to make a decision based on a condition.
- Simple
if - Multiple
if if...else- Nested
if else...ifladderswitch...case
4. Simple if Statement
A simple if statement is used to execute a group of statements only when a given condition is true.
Syntax
if (condition)
{
statements;
}
How it Works
- The condition is checked.
- If the condition is true, the statements inside the
ifblock execute. - If the condition is false, the
ifblock is skipped. - The program continues with the next statement.
{ } form a block. Braces are
recommended even when there is only one statement.
5. Example: Check Whether a Number is Natural
Problem
Write an algorithm and draw a flowchart to determine whether the value of a variable is a natural number.
Logic
For this example, we will consider positive integers greater than 0 as natural numbers.
Therefore, the condition is:
x > 0
Algorithm
- Start
- Declare an integer variable
x - Read the value of
x - Check whether
x > 0 - If true, display “x is a natural number”
- Stop
Flowchart
“Natural Number”
C Program
#include <stdio.h>
int main()
{
int x = 12;
if (x > 0)
{
printf("%d is a natural number", x);
}
printf("\nRest of the program");
return 0;
}
Output
12 is a natural number
Rest of the program
x is 0 or negative, the message inside the
if block will not be displayed. The rest of the program
will still execute.
6. Example 1: Voting Eligibility
Question
Write a C program to accept age from the user and display “Eligible for voting” when the age entered is greater than 18.
Hint
age > 18
Program
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age > 18)
{
printf("Eligible for voting");
}
return 0;
}
Sample Output
Enter your age: 25
Eligible for voting
age > 18. In an actual voting system, the legal minimum
voting age depends on the country.
7. Example 2: Even or Odd
Question
Write a C program to determine whether the number entered by the user is even or odd.
Hint
Use the modulus operator %.
x % 2 == 0 → Even
x % 2 != 0 → Odd
Simple if Approach
#include <stdio.h>
int main()
{
int x;
printf("Enter a number: ");
scanf("%d", &x);
if (x % 2 == 0)
{
printf("%d is even", x);
}
if (x % 2 != 0)
{
printf("%d is odd", x);
}
return 0;
}
Sample Output
Enter a number: 24
24 is even
% operator gives the remainder after division.
If a number divided by 2 leaves remainder 0, it is even.
8. Example 3: Positive, Negative or Zero
Question
Write C code to determine whether the number entered by the user is greater than, less than, or equal to zero.
Conditions
| Condition | Meaning |
|---|---|
a > 0 |
Positive number |
a < 0 |
Negative number |
a == 0 |
Zero |
Program Using Multiple if Statements
#include <stdio.h>
int main()
{
int a;
printf("Enter a number: ");
scanf("%d", &a);
if (a > 0)
{
printf("Positive number");
}
if (a < 0)
{
printf("Negative number");
}
if (a == 0)
{
printf("Zero");
}
return 0;
}
Sample Outputs
Enter a number: 15
Positive number
Enter a number: -8
Negative number
Enter a number: 0
Zero
== means “is equal to”.
Do not confuse it with =, which is the assignment operator.
9. Example 4: Number in Words
Question
Write a C program to accept any number out of 2, 4, 6, 8 from the user and display the input number in words.
Example
Enter any number from 2, 4, 6, 8: 4
FOUR
Approach
Since the program has to check several possible values, we can use
if...else if.
Program
#include <stdio.h>
int main()
{
int num;
printf("Enter any number from 2, 4, 6, 8: ");
scanf("%d", &num);
if (num == 2)
{
printf("TWO");
}
else if (num == 4)
{
printf("FOUR");
}
else if (num == 6)
{
printf("SIX");
}
else if (num == 8)
{
printf("EIGHT");
}
else
{
printf("Invalid number");
}
return 0;
}
Sample Output
Enter any number from 2, 4, 6, 8: 4
FOUR
else if ladder suitable for
selecting exactly one option from several choices.
10. Important Operators Used in Conditions
| Operator | Meaning | Example |
|---|---|---|
> |
Greater than | x > 10 |
< |
Less than | x < 10 |
== |
Equal to | x == 10 |
!= |
Not equal to | x != 10 |
>= |
Greater than or equal to | x >= 10 |
<= |
Less than or equal to | x <= 10 |
% |
Remainder / Modulus | x % 2 |
x = 5 assigns 5 to x, while
x == 5 checks whether x is equal to 5.
11. Practice Questions
Practice 1 — Positive Number
Write a C program to accept a number and display “Positive” if the number is greater than zero.
Hint: num > 0
Practice 2 — Divisible by 5
Write a C program to check whether a number is divisible by 5.
Hint: num % 5 == 0
Practice 3 — Greater Than 100
Accept a number from the user and display “Greater than 100” if the number is greater than 100.
Hint: num > 100
Practice 4 — Multiple of 10
Write a program to determine whether a given number is a multiple of 10.
Hint: num % 10 == 0
Practice 5 — Check Zero
Write a C program to check whether the number entered by the user is zero.
Hint: num == 0
Practice 6 — Even Number
Accept a number and display “Even Number” if the number is even.
Hint: num % 2 == 0
Practice 7 — Number Comparison
Accept two numbers and determine whether the first number is greater than the second number.
Hint: a > b
Practice 8 — Number in Words
Accept a number from 1 to 5 and display it in words.
Example:
1 → ONE,
2 → TWO,
3 → THREE,
4 → FOUR,
5 → FIVE
Practice 9 — Temperature
Accept temperature and display “Hot” if the temperature is greater than 30.
Practice 10 — Natural Number
Accept an integer and display “Natural Number” if it is greater than zero.
Also draw: Algorithm + Flowchart + C Program.
12. Quick Revision
- Control statements change the normal flow of execution.
- Selection statements are used for decision-making.
- Simple if executes a block when a condition is true.
- Multiple if uses more than one independent condition.
- if…else selects between two alternatives.
- Nested if means an if statement inside another if statement.
- else…if is useful when there are multiple alternatives.
- switch…case is useful for selecting one option from multiple fixed choices.
==is used for comparison.=is used for assignment.%gives the remainder.
C Programming — Control Statements
Learn the logic → Write the algorithm → Draw the flowchart → Write the C program → Test the output.