Welcome to the lesson on Nested Conditionals! In this session, we'll delve into a powerful C programming concept that allows you to write complex conditional logic with multiple levels of if
, else if
, and else
statements. This skill will enable you to create more sophisticated programs with intricate decision-making capabilities. By the end of this lesson, you'll be able to write code that efficiently handles multiple conditions in a nested structure.
Nested conditionals are sequences of if
, else if
, and else
statements placed within other conditional blocks (i.e., inside the curly braces {}
). By nesting them, you can create complex decision trees for your program to evaluate multiple conditions in a structured manner.
Here's an example to illustrate nested conditionals:
#include <stdio.h>
int main() {
int day = 6;
if (day == 1) { // Main condition checks for Sunday
printf("Today is Sunday.\n");
} else if (day == 2) { // Nested condition checks for Monday
printf("Today is Monday.\n");
} else if (day == 3) { // Another nested condition checks for Tuesday
printf("Today is Tuesday.\n");
} else { // Default case for all other days
printf("The day of the week is not recognized.\n");
}
return 0;
}
In this example, we have a main condition that checks if the day
variable equals 1 (Sunday). If it does, we print out "Today is Sunday.". However, if the main condition fails, we proceed to nested conditions to check for Monday and Tuesday. Notice how each else if
statement is indented within the curly braces of its preceding conditional block.
Let's consider a more complex example where we need to determine the grade of a student based on their test score:
#include <stdio.h>
int main() {
int score;
printf("Enter your test score: ");
scanf("%d", &score);
if (score >= 90) { // A Grade
printf("You received an A.\n");
} else if (score >= 80) { // B Grade
printf("You received a B.\n");
} else if (score >= 70) { // C Grade
printf("You received a C.\n");
} else if (score >= 60) { // D Grade
printf("You received a D.\n");
} else { // F Grade
printf("You received an F.\n");
}
return 0;
}
In this example, we use nested conditionals to determine the grade of a student based on their test score. The main condition checks if the score
variable is greater than or equal to 90 (A Grade). If it does, we print out "You received an A.". However, if the main condition fails, we proceed to nested conditions to check for B, C, D, and F grades.
What causes it:
# Bad code example that triggers the error
int main() {
int day = 6;
if (day == 1) printf("Today is Sunday.\n"); // Missing curly braces
else if (day == 2) printf("Today is Monday.\n"); // Missing curly braces
else if (day == 3) printf("Today is Tuesday.\n"); // Missing curly braces
else printf("The day of the week is not recognized.\n"); // No indentation for default case
}
Error message:
Syntax error near token `else'
Solution:
Add missing curly braces {}
to each conditional block and properly indented the default case.
Why it happens: Improper syntax, including missing curly braces and incorrect indentation, can lead to compile errors.
How to prevent it: Ensure that you include curly braces for each conditional block and follow proper indentation conventions.
What causes it:
# Bad code example that triggers the error
int main() {
int day = 6;
if (day == "Monday") printf("Today is Monday.\n"); // Typo in condition
}
Error message:
Traceback (most recent call last):
File "example.c", line 5, in <module>
if (day == "Monday") printf("Today is Monday.\n");
NameError: name 'Monday' is not defined
Solution:
Correct the typo by using day == 2
instead of day == "Monday"
.
Why it happens: Using a string value (e.g., "Monday"
) in a comparison operation with an integer variable will cause a NameError because the string is not defined as a constant or variable in the program.
How to prevent it: Be mindful of using proper data types for comparisons and ensure that you're comparing values consistently (either as strings or integers).
{}
.Next steps for learning: Learn about loops in C programming to further enhance your program's decision-making and repetition capabilities.