Control Structure in C_Programming
Control Structure in C_Programming
Final Summary.............................................................................................................................................9
1. IF STATEMENT ....................................................................................................................................................9
1
1.DECISION CONTROL STRUCTURE
Decision control structures allow you to control the flow of your program based on certain conditions. The most
common decision control structures are if, else, else if, and switch statements.
2. IF STATEMENT
The if statement checks a condition and executes the code inside it if the condition is true.
Syntax:
if (condition) {
Example:
#include <stdio.h>
int main() {
if (number > 5) {
return 0;
Summary:
In this example, the if statement checks if number is greater than 5. Since 10 is greater than 5, the code inside
the if block executes, printing "Number is greater than 5."
3. ELSE STATEMENT
2
Syntax:
if (condition) {
} else {
Example:
#include <stdio.h>
int main() {
int number = 3;
if (number > 5) {
} else {
return 0;
Summary:
Here, since the number is 3, the condition in the if statement is false, so the code inside the else block is
executed, printing "Number is less than or equal to 5."
You can place one if or else statement inside another, creating a nested structure. This is useful for more
complex decision-making.
Syntax:
if (condition1) {
3
if (condition2) {
} else {
} else {
Example:
#include <stdio.h>
int main() {
int number = 8;
if (number > 5) {
} else {
} else {
printf("Number is 5 or less.\n");
return 0;
Summary:
This program checks if number is greater than 5 and then checks if it's less than 10. Since number is 8, the
inner if block prints "Number is between 5 and 10."
4
5. SWITCH CASE
A switch statement is an alternative to a long chain of if-else statements when comparing the same
variable to different values.
Syntax:
switch (variable) {
case value1:
break;
case value2:
break;
default:
Example:
#include <stdio.h>
int main() {
int day = 2;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
5
break;
default:
printf("Another day\n");
return 0;
Summary:
The switch statement compares day with predefined values. Since day is 2, it matches case 2 and prints
"Tuesday."
Example:
#include <stdio.h>
int main() {
int a = 5, b = 10;
if (a > 0 || b < 0) {
6
if (!(a > 10)) {
return 0;
Summary:
This example demonstrates logical operators. The first if uses && to check if both a and b are positive. The
second if uses || to check if at least one condition is true. The third if uses ! to negate the condition.
Syntax:
if (condition1) {
} else if (condition2) {
} else {
Example:
#include <stdio.h>
int main() {
7
printf("Number is equal to 10.\n");
} else {
return 0;
Summary:
This example uses else if to check if number is greater than, equal to, or less than 10. Since number is
equal to 10, it prints "Number is equal to 10."
8. THE ! OPERATOR
The ! operator inverts the logical state of its operand. If a condition is true, ! makes it false, and vice versa.
Example:
#include <stdio.h>
int main() {
if (!isRaining) {
return 0;
Summary:
In this example, !isRaining evaluates to true because isRaining is false (0). Hence, the program prints
"It's not raining."
8
Final Summary
• Decision Control Structure: Helps in controlling the flow of execution based on conditions.
• If Statement: Executes code when a specific condition is true.
• Else Statement: Executes code when the if condition is false.
• Nested If-Else: Allows more complex decision-making by embedding if-else inside other if-
else statements.
• Switch-Case: An alternative to if-else for checking a variable against multiple values.
• Logical Operators: Combine multiple conditions for more complex logic.
• Else-If Clause: Allows multiple sequential conditions to be checked.
• ! Operator: Inverts a condition’s truth value.
1. IF STATEMENT
Example 1 (Basic):
#include <stdio.h>
int main() {
int number = 5;
if (number == 5) {
printf("Number is 5.\n");
return 0;
#include <stdio.h>
int main() {
9
int a = 3, b = 5;
if (a < b) {
return 0;
Explanation: Here, we check if a is less than b. Since 3 is less than 5, it prints "a is less than b."
#include <stdio.h>
int main() {
int number = 4;
if (number % 2 == 0) {
printf("Number is even.\n");
return 0;
Explanation: We check if a number is even by using the modulus operator (%). If the remainder is 0, the
number is even.
#include <stdio.h>
10
int main() {
if (number % 3 == 0) {
return 0;
Explanation: This checks if the number is divisible by 3. If true, it prints the message.
#include <stdio.h>
int main() {
return 0;
Explanation: We combine two conditions with the && operator. The message is printed only if both conditions
are true.
2. ELSE STATEMENT
#include <stdio.h>
11
int main() {
int number = 3;
if (number == 5) {
printf("Number is 5.\n");
} else {
return 0;
Explanation: If the condition is false, the else part executes, printing "Number is not 5."
#include <stdio.h>
int main() {
int number = 7;
if (number % 2 == 0) {
printf("Number is even.\n");
} else {
printf("Number is odd.\n");
return 0;
12
Example 3 (Simple Voting Check):
#include <stdio.h>
int main() {
} else {
return 0;
Explanation: This checks if the user is old enough to vote. If not, it prints a different message.
#include <stdio.h>
int main() {
if (a > b) {
} else {
13
return 0;
#include <stdio.h>
int main() {
} else {
return 0;
#include <stdio.h>
int main() {
int number = 5;
if (number > 0) {
14
if (number < 10) {
return 0;
Explanation: If number is greater than 0, the nested condition checks if it's also less than 10.
#include <stdio.h>
int main() {
printf("Grade: A\n");
} else {
printf("Grade: B\n");
} else {
printf("Grade: F\n");
return 0;
Explanation: This nested structure provides a simple grading system based on the score.
15
Example 3 (Temperature Check):
#include <stdio.h>
int main() {
} else {
} else {
return 0;
#include <stdio.h>
int main() {
if (number > 0) {
if (number % 5 == 0) {
16
printf("Number is positive, less than 50, and divisible by
5.\n");
return 0;
#include <stdio.h>
int main() {
} else {
} else {
return 0;
17
4. SWITCH-CASE STATEMENT
The switch-case is used to handle multiple possible conditions of a single variable without using multiple
if-else statements. Each case represents a possible value.
Example 1 (Basic):
#include <stdio.h>
int main() {
int day = 2;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
default:
printf("Another day\n");
return 0;
Steps:
18
#include <stdio.h>
int main() {
int day = 4;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
default:
printf("Another day\n");
return 0;
Steps:
Example 3 (Calculator):
19
#include <stdio.h>
int main() {
int a = 5, b = 3;
switch (operator) {
case '+':
break;
case '-':
break;
default:
return 0;
Steps:
#include <stdio.h>
int main() {
int a = 5, b = 3;
20
switch (operator) {
case '+':
break;
case '-':
break;
case '*':
break;
default:
return 0;
Steps:
#include <stdio.h>
int main() {
switch (grade) {
case 'A':
printf("Excellent\n");
21
break;
case 'B':
printf("Good\n");
break;
case 'C':
printf("Fair\n");
break;
default:
printf("Invalid grade\n");
return 0;
Steps:
1. grade is checked.
2. If it's B, it prints "Good."
3. The default block handles any grade not listed.
5. LOGICAL OPERATORS
Logical operators are used to combine conditions. The main operators are && (AND), || (OR), and ! (NOT).
#include <stdio.h>
int main() {
22
}
return 0;
Steps:
#include <stdio.h>
int main() {
int a = 3, b = 20;
return 0;
Steps:
#include <stdio.h>
int main() {
23
int isRaining = 0;
if (!isRaining) {
return 0;
Steps:
#include <stdio.h>
int main() {
int a = 10, b = 5;
return 0;
Steps:
24
#include <stdio.h>
int main() {
return 0;
Steps:
6. ELSE-IF CLAUSE
Example 1 (Basic):
#include <stdio.h>
int main() {
} else {
25
printf("Number is less than 10.\n");
return 0;
Steps:
#include <stdio.h>
int main() {
} else {
return 0;
Steps:
26
Example 3 (Grade Evaluation):
#include <stdio.h>
int main() {
printf("Grade: A\n");
printf("Grade: B\n");
} else {
printf("Grade: C\n");
return 0;
Steps:
#include <stdio.h>
int main() {
printf("Number is large.\n");
printf("Number is medium.\n");
27
} else if (number > 10) {
printf("Number is small.\n");
} else {
return 0;
Steps:
#include <stdio.h>
int main() {
printf("Grade: A\n");
printf("Grade: B\n");
printf("Grade: C\n");
printf("Grade: D\n");
} else {
printf("Grade: F\n");
return 0;
28
}
Steps:
1. The if condition checks if the score is 90 or above, in which case it prints "Grade: A."
2. If not, it moves to the next condition to check if the score is 80 or above for a "Grade: B."
3. This continues until the last else block, which prints "Grade: F" if none of the previous conditions are
met.
Explanation: This program evaluates the score and assigns a letter grade. If the score is 90 or higher, the
student gets an A, and so on down the list until a failing grade of F is assigned if the score is below 60.
29