Control Structure
By Notes Vandar
Control Structure
Control structures in C programming determine the flow of control in a program. They allow for decision-making, looping, and altering the normal sequential execution of statements.
3.1 Selective Structure
3.1.1 If statement
The if
statement is the simplest form of selection. It executes a block of code only if a specified condition is true. If the condition is false, the code block is skipped.
Syntax:
// Code to execute if the condition is true
}
Example:#include <stdio.h>
int main() {
int age = 20;
if (age >= 18) {
printf(“You are eligible to vote.\n”);
}
return 0;
}Explanation: If the condition
age >= 18
is true, the message “You are eligible to vote.” is printed.
3.1.2 If-else statement
The if-else
statement adds an alternative code block to execute if the condition is false. So, if the condition is true, one block of code executes; otherwise, another block of code is executed.
- Syntax:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
- Example:
#include <stdio.h>
int main() {
int age = 16;
if (age >= 18) {
printf(“You are eligible to vote.\n”);
} else {
printf(“You are not eligible to vote.\n”);
}
return 0;
}
3.1.3 Nested if-else statement
if-else
statement refers to an if-else
structure where one or more if-else
statements are placed inside another if-else
statement. This allows for more complex decision-making by evaluating multiple conditions within conditions.
if-else
:// Code block to execute if condition1 is true
if (condition2) {
// Code block to execute if condition2 is also true
} else {
// Code block to execute if condition2 is false
}
} else {
// Code block to execute if condition1 is false
if (condition3) {
// Code block to execute if condition3 is true
} else {
// Code block to execute if condition3 is false
}
}
Example of Nested if-else
:
Consider a scenario where we need to determine the grade of a student based on their marks, with additional checks to provide specific messages for different ranges.
#include <stdio.h>
int main() {
int marks;
// Input marks from the user
printf(“Enter your marks: “);
scanf(“%d”, &marks);
// Nested if-else to determine grade
if (marks >= 90) {
printf(“Grade: A\n”);
} else {
if (marks >= 75) {
printf(“Grade: B\n”);
} else {
if (marks >= 50) {
printf(“Grade: C\n”);
} else {
printf(“Grade: F\n”);
}
}
}
return 0;
}
Explanation:
- If the student’s marks are 90 or above, they receive an “A”.
- If the marks are less than 90 but 75 or above, they receive a “B”.
- If the marks are less than 75 but 50 or above, they receive a “C”.
- If the marks are below 50, they receive a “F”
3.1.4 Switch statement
The switch
statement provides a way to select one of many code blocks based on the value of an expression. It is often used as an alternative to multiple else if
statements when there are many possible discrete values for the expression being tested.
- Syntax:
switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
…
default:
// Code to execute if no case matches
}
- Example:
#include <stdio.h>
int main() {
int day = 3;
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;
case 5:
printf(“Friday\n”);
break;
case 6:
printf(“Saturday\n”);
break;
case 7:
printf(“Sunday\n”);
break;
default:
printf(“Invalid day\n”);
}
return 0;
}
Explanation: Depending on the value of day
, the corresponding case is executed. If day = 3
, the message “Wednesday” is printed.