Control Flow: Conditional Statements (if, else, switch)
Using `if`, `else`, and `switch` statements to control the flow of execution based on conditions.
Conditional Statements in C
Explanation: Conditional Statements
Conditional statements are fundamental control flow structures in C programming that allow you to execute different blocks of code based on whether a specified condition is true or false. They enable your programs to make decisions and behave dynamically according to various inputs and circumstances. The main conditional statements are if
, else
, and switch
.
if
statement: Executes a block of code if a specified condition is true.else
statement: Executes a block of code if the condition in the precedingif
statement is false.else if
statement: Allows you to chain multiple conditions together, providing a more complex decision-making structure.switch
statement: Selects one of several code blocks to execute based on the value of a single expression. It's particularly useful when dealing with a fixed set of possible values.
Understanding and effectively using conditional statements is crucial for writing robust and adaptable C programs.
Practical Examples and Case Studies
Example 1: Determining if a number is positive, negative, or zero using if
and else if
This example demonstrates how to use if
and else if
statements to classify a given number as positive, negative, or zero.
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if (number > 0) {
printf("%d is positive.\n", number);
} else if (number < 0) {
printf("%d is negative.\n", number);
} else {
printf("%d is zero.\n", number);
}
return 0;
}
Case Study 1: Simple Grading System using if
, else if
, and else
This case study implements a simple grading system that assigns letter grades (A, B, C, D, F) based on a student's score.
#include <stdio.h>
int main() {
int score;
printf("Enter the student's score: ");
scanf("%d", &score);
if (score >= 90) {
printf("Grade: A\n");
} else if (score >= 80) {
printf("Grade: B\n");
} else if (score >= 70) {
printf("Grade: C\n");
} else if (score >= 60) {
printf("Grade: D\n");
} else {
printf("Grade: F\n");
}
return 0;
}
Example 2: Determining the Day of the Week using switch
This example uses a switch
statement to display the corresponding day of the week based on a numerical input (1-7).
#include <stdio.h>
int main() {
int day;
printf("Enter a number (1-7) representing the day of the week: ");
scanf("%d", &day);
switch (day) {
case 1:
printf("Sunday\n");
break;
case 2:
printf("Monday\n");
break;
case 3:
printf("Tuesday\n");
break;
case 4:
printf("Wednesday\n");
break;
case 5:
printf("Thursday\n");
break;
case 6:
printf("Friday\n");
break;
case 7:
printf("Saturday\n");
break;
default:
printf("Invalid input. Please enter a number between 1 and 7.\n");
}
return 0;
}
Case Study 2: A Simple Calculator using switch
This case study implements a simple calculator that performs addition, subtraction, multiplication, or division based on user input.
#include <stdio.h>
int main() {
char operator;
double num1, num2;
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator); // Note the space before %c to consume any leading whitespace
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
switch (operator) {
case '+':
printf("%.2lf + %.2lf = %.2lf\n", num1, num2, num1 + num2);
break;
case '-':
printf("%.2lf - %.2lf = %.2lf\n", num1, num2, num1 - num2);
break;
case '*':
printf("%.2lf * %.2lf = %.2lf\n", num1, num2, num1 * num2);
break;
case '/':
if (num2 == 0) {
printf("Error: Division by zero!\n");
} else {
printf("%.2lf / %.2lf = %.2lf\n", num1, num2, num1 / num2);
}
break;
default:
printf("Error: Invalid operator!\n");
}
return 0;
}
Nested `if` Statements: Determining Eligibility for a Loan
This example demonstrates nested `if` statements to determine loan eligibility based on age and income.
#include <stdio.h>
int main() {
int age;
double income;
printf("Enter your age: ");
scanf("%d", &age);
printf("Enter your annual income: ");
scanf("%lf", &income);
if (age >= 18) {
if (income >= 30000.0) {
printf("Congratulations! You are eligible for a loan.\n");
} else {
printf("Sorry, your income is not sufficient for a loan.\n");
}
} else {
printf("Sorry, you must be at least 18 years old to apply for a loan.\n");
}
return 0;
}