Control Statements are used in C programming to control the flow of program execution. Normally, a program executes statements from top to bottom. Control statements allow a programmer to make decisions, repeat instructions, or change the normal order of execution.
Control statements are essential for developing programs that can make decisions, perform repeated tasks, and respond to different conditions.
C control statements can mainly be divided into three categories:
Used to make decisions based on conditions.
ifif-elseelse-ififswitch-caseUsed to execute a block of code repeatedly.
forwhiledo-whileUsed to change the normal flow of execution.
breakcontinuegotoreturnif StatementThe if statement executes a block of code when a specified condition is true.
if (condition)
{
// statements
}
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18)
{
printf("You are eligible to vote.");
}
return 0;
}
If age is 18 or greater, the message will be displayed.
if-else StatementThe if-else statement provides two possible paths.
if (condition)
{
// statements if condition is true
}
else
{
// statements if condition is false
}
#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number % 2 == 0)
{
printf("Even number");
}
else
{
printf("Odd number");
}
return 0;
}
else-if LadderAn else-if ladder is used when there are multiple conditions to check.
#include <stdio.h>
int main()
{
int marks;
printf("Enter marks: ");
scanf("%d", &marks);
if (marks >= 80)
{
printf("Grade A");
}
else if (marks >= 60)
{
printf("Grade B");
}
else if (marks >= 40)
{
printf("Grade C");
}
else
{
printf("Fail");
}
return 0;
}
The conditions are checked from top to bottom. Once a condition is satisfied, its block is executed.
if StatementAn if statement placed inside another if statement is called a nested if.
#include <stdio.h>
int main()
{
int age;
int citizen;
printf("Enter age: ");
scanf("%d", &age);
printf("Enter 1 if you are a citizen, otherwise 0: ");
scanf("%d", &citizen);
if (age >= 18)
{
if (citizen == 1)
{
printf("Eligible");
}
}
return 0;
}
Nested conditions are useful when one decision depends on another.
switch StatementThe switch statement is useful when one expression needs to be compared with several constant cases.
switch (expression)
{
case value1:
// statements
break;
case value2:
// statements
break;
default:
// statements
}
#include <stdio.h>
int main()
{
int choice;
printf("1. Addn");
printf("2. Subtractn");
printf("3. Multiplyn");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Addition selected");
break;
case 2:
printf("Subtraction selected");
break;
case 3:
printf("Multiplication selected");
break;
default:
printf("Invalid choice");
}
return 0;
}
break in SwitchThe break statement stops execution of the current switch statement.
Without break, execution can continue into the following cases. This behavior is called fall-through.
switch (choice)
{
case 1:
printf("One");
break;
case 2:
printf("Two");
break;
default:
printf("Invalid");
}
default CaseThe default block executes when none of the specified case values match.
default:
printf("Invalid choice");
It is optional, but it is useful for handling unexpected input.
Loops are used when a block of code needs to be executed repeatedly.
forwhiledo-whilefor LoopA for loop is commonly used when the number of repetitions is known or can be controlled using a counter.
for (initialization; condition; update)
{
// statements
}
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 5; i++)
{
printf("%dn", i);
}
return 0;
}
1
2
3
4
5
for Loop WorksConsider:
for (i = 1; i <= 5; i++)
i = 1;
i <= 5
The statements inside the loop are executed.
i++;
The process continues until the condition becomes false.
while LoopA while loop repeatedly executes a block while its condition remains true.
while (condition)
{
// statements
}
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 5)
{
printf("%dn", i);
i++;
}
return 0;
}
do-while LoopThe do-while loop executes its body at least once, because the condition is checked after the loop body.
do
{
// statements
}
while (condition);
#include <stdio.h>
int main()
{
int i = 1;
do
{
printf("%dn", i);
i++;
}
while (i <= 5);
return 0;
}
while and do-while| Feature | while |
do-while |
|---|---|---|
| Condition checked | Before execution | After execution |
| Minimum executions | 0 | 1 |
| Semicolon after condition | No | Yes |
| Best use | When execution depends on condition | When code must run at least once |
A loop inside another loop is called a nested loop.
#include <stdio.h>
int main()
{
int i, j;
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= 3; j++)
{
printf("* ");
}
printf("n");
}
return 0;
}
* * *
* * *
* * *
Nested loops are commonly used for:
break StatementThe break statement immediately terminates the nearest enclosing loop or switch.
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 10; i++)
{
if (i == 5)
{
break;
}
printf("%dn", i);
}
return 0;
}
1
2
3
4
continue StatementThe continue statement skips the remaining statements in the current loop iteration and moves to the next iteration.
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <= 5; i++)
{
if (i == 3)
{
continue;
}
printf("%dn", i);
}
return 0;
}
1
2
4
5
break vs continuebreak |
continue |
|---|---|
| Terminates the loop | Skips the current iteration |
| Control moves outside the loop | Control moves to the next iteration |
Can be used in loops and switch |
Primarily used with loops |
goto StatementThe goto statement transfers program control to a labeled statement within the same function.
#include <stdio.h>
int main()
{
int number;
printf("Enter a positive number: ");
scanf("%d", &number);
if (number < 0)
{
goto invalid;
}
printf("Valid number");
return 0;
invalid:
printf("Invalid number");
return 0;
}
Although goto is part of C, excessive use can make programs difficult to understand and maintain. Structured alternatives such as loops and functions are generally preferred.
return StatementThe return statement ends a function and can send a value back to the calling code.
#include <stdio.h>
int add()
{
return 10 + 20;
}
int main()
{
int result;
result = add();
printf("Result = %d", result);
return 0;
}
The conditional operator ?: provides a short way to write a simple conditional expression.
condition ? value_if_true : value_if_false;
#include <stdio.h>
int main()
{
int age = 20;
printf("%s", age >= 18 ? "Adult" : "Minor");
return 0;
}
The conditional operator is useful for simple decisions, but complex conditions are usually clearer with if-else.
#include <stdio.h>
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number > 0)
{
printf("Positive number");
}
else if (number < 0)
{
printf("Negative number");
}
else
{
printf("Zero");
}
return 0;
}
#include <stdio.h>
int main()
{
int number, i;
printf("Enter a number: ");
scanf("%d", &number);
for (i = 1; i <= 10; i++)
{
printf("%d x %d = %dn", number, i, number * i);
}
return 0;
}
#include <stdio.h>
int main()
{
int i, sum = 0;
for (i = 1; i <= 10; i++)
{
sum = sum + i;
}
printf("Sum = %d", sum);
return 0;
}
Sum = 55
#include <stdio.h>
int main()
{
int choice;
printf("===== MENU =====n");
printf("1. Computer Coursen");
printf("2. Digital Marketingn");
printf("3. Web Designingn");
printf("4. Exitn");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("Computer Course Selected");
break;
case 2:
printf("Digital Marketing Selected");
break;
case 3:
printf("Web Designing Selected");
break;
case 4:
printf("Program Exit");
break;
default:
printf("Invalid Choice");
}
return 0;
}
Create a C program that:
Create a program that accepts marks and displays:
Create a program that accepts a number and displays its multiplication table from 1 to 10.
Create a menu using switch:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Ask the user to enter a choice and perform the selected operation.
Write a program using a for loop to display:
1
2
3
4
5
6
7
8
9
10
Then modify the program to display only even numbers.
Leave a Reply