C Programming Control Statements
C Programming Control Statements
C Programming
Control Statements
BY Aniruddha Manmode
Index
- What is Control Statements
- Conditional Control Statements
- If statement
- If-else statement
- If-else-if statement
- Nested If statement
- Switch statement
- Looping Control Statements
- while loop
- do while loop
- for loop
- Branching (Jump) Control Statements
- break statement
- continue statement
2
What is Control Statements ?
Control statements in C are used to manage the flow of execution of a program. They
allow you to make decisions, repeat actions, and jump to different parts of your code
based on certain conditions.
These statements allow you to execute a block of code only when certain conditions are met.
Examples:
- if statement
- if-else statement
- if-else-if statement
- Nested If
- Switch-case
If statement
These statements execute specific blocks of code based on a condition. The condition is a logical
expression that evaluates to either True or False. Based on this evaluation, the program decides which
code block to execute.
Example -
#include <stdio.h>
int main()
{
int x = 10;
if (x > 5)
{
printf("x is greater than 5\n");
}
return 0;
}
If-else statement
Executes one block of code if the condition is True, and another block if it is False.
Example -
#include <stdio.h>
int main() {
int x = 3;
if (x > 5) {
printf("x is greater than 5\n");
} else {
printf("x is less than or equal to 5\n");
}
return 0;
}
If-else-if statement
Example -
#include <stdio.h>
int main() {
int num = 10;
// Outer if statement
if (num > 0) {
printf("The number is positive.\n");
// Nested if statement
if (num % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}
} else {
printf("The number is not positive.\n");
}
return 0;
}
Nested If statement
Example -
#include <stdio.h>
int main() {
int num = 10;
// Outer if statement
if (num > 0) {
printf("The number is positive.\n");
// Nested if statement
if (num % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}
} else {
printf("The number is not positive.\n");
}
return 0;
}
Switch Statement
Example -
#include <stdio.h>
A switch statement is a
int main() {
control structure in programming int num;
that evaluates an expression and
// Get a number from the user
executes one of several possible printf("Enter a number (1-3): ");
blocks of code based on the value scanf("%d", &num);
of the expression. // Switch statement to check the number
switch (num) {
case 1:
It is often used to replace printf("You entered One.\n");
multiple if-else-if conditions. break;
case 2:
printf("You entered Two.\n");
Each possible value of the break;
expression is defined by a case, case 3:
printf("You entered Three.\n");
and the default block executes if break;
none of the cases match. default:
printf("Invalid number!\n");
break;
}
return 0;
}
2. Looping Control Statements
Looping control statements allow you to repeat a block of code multiple times.
Loops are control structures that allow you to execute a block of code repeatedly as long as a specified
condition is true.
Types:
- while loop
- do-while loop
- for loop
while loop
Example -
do-while loop
Example -
for loop
Example -
Nested for loop
Example -
3. Branching (Jump) Control Statements
These allow you to change the normal flow of execution by jumping out of loops or skipping certain
code of block.
Examples:
Example -
#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
continue; // Skip the rest of the loop when i is 5
}
printf("%d\n", i);
}
return 0;
}