Conditional statements are fundamental in programming as they allow you to execute different code blocks based on certain conditions. In C, the primary conditional statements are if
, else if
, else
, and switch
. This section will cover each of these in detail, providing examples and exercises to solidify your understanding.
- The
if
Statement
if
StatementThe if
statement is used to execute a block of code if a specified condition is true.
Syntax
Example
#include <stdio.h> int main() { int number = 10; if (number > 0) { printf("The number is positive.\n"); } return 0; }
Explanation
- The condition
number > 0
is checked. - If the condition is true, the code inside the
if
block is executed, printing "The number is positive."
- The
else
Statement
else
StatementThe else
statement is used to execute a block of code if the condition in the if
statement is false.
Syntax
if (condition) { // code to be executed if condition is true } else { // code to be executed if condition is false }
Example
#include <stdio.h> int main() { int number = -5; if (number > 0) { printf("The number is positive.\n"); } else { printf("The number is not positive.\n"); } return 0; }
Explanation
- The condition
number > 0
is checked. - If the condition is false, the code inside the
else
block is executed, printing "The number is not positive."
- The
else if
Statement
else if
StatementThe else if
statement allows you to check multiple conditions.
Syntax
if (condition1) { // code to be executed if condition1 is true } else if (condition2) { // code to be executed if condition2 is true } else { // code to be executed if both conditions are false }
Example
#include <stdio.h> int main() { int number = 0; if (number > 0) { printf("The number is positive.\n"); } else if (number < 0) { printf("The number is negative.\n"); } else { printf("The number is zero.\n"); } return 0; }
Explanation
- The conditions
number > 0
andnumber < 0
are checked in sequence. - If neither condition is true, the code inside the
else
block is executed, printing "The number is zero."
- The
switch
Statement
switch
StatementThe switch
statement is used to execute one block of code among many options.
Syntax
switch (expression) { case constant1: // code to be executed if expression equals constant1 break; case constant2: // code to be executed if expression equals constant2 break; // you can have any number of case statements default: // code to be executed if expression doesn't match any case }
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
- The value of
day
is compared against eachcase
. - When a match is found, the corresponding block of code is executed.
- The
break
statement prevents the execution from falling through to the next case.
Practical Exercises
Exercise 1: Positive, Negative, or Zero
Write a program that reads an integer from the user and prints whether the number is positive, negative, or zero.
Solution
#include <stdio.h> int main() { int number; printf("Enter an integer: "); scanf("%d", &number); if (number > 0) { printf("The number is positive.\n"); } else if (number < 0) { printf("The number is negative.\n"); } else { printf("The number is zero.\n"); } return 0; }
Exercise 2: Grade Evaluation
Write a program that reads a grade (A, B, C, D, F) from the user and prints the corresponding description (Excellent, Good, Average, Poor, Fail).
Solution
#include <stdio.h> int main() { char grade; printf("Enter a grade (A, B, C, D, F): "); scanf(" %c", &grade); switch (grade) { case 'A': printf("Excellent\n"); break; case 'B': printf("Good\n"); break; case 'C': printf("Average\n"); break; case 'D': printf("Poor\n"); break; case 'F': printf("Fail\n"); break; default: printf("Invalid grade\n"); } return 0; }
Common Mistakes and Tips
- Forgetting the
break
statement inswitch
cases: This can lead to fall-through behavior, where multiple cases are executed. - Using
=
instead of==
in conditions:=
is the assignment operator, while==
is the equality operator. - Not handling all possible cases: Always include a
default
case inswitch
statements to handle unexpected values.
Conclusion
In this section, you learned about conditional statements in C, including if
, else if
, else
, and switch
. These constructs allow you to control the flow of your program based on different conditions. Practice the exercises provided to reinforce your understanding, and remember to watch out for common mistakes. Next, we will explore loops in C, which will further enhance your ability to control program flow.
C Programming Course
Module 1: Introduction to C
- Introduction to Programming
- Setting Up the Development Environment
- Hello World Program
- Basic Syntax and Structure
Module 2: Data Types and Variables
Module 3: Control Flow
Module 4: Functions
- Introduction to Functions
- Function Arguments and Return Values
- Scope and Lifetime of Variables
- Recursive Functions
Module 5: Arrays and Strings
Module 6: Pointers
Module 7: Structures and Unions
Module 8: Dynamic Memory Allocation
Module 9: File Handling
- Introduction to File Handling
- Reading and Writing Files
- File Positioning
- Error Handling in File Operations
Module 10: Advanced Topics
Module 11: Best Practices and Optimization
- Code Readability and Documentation
- Debugging Techniques
- Performance Optimization
- Security Considerations