Flow Control in C: Conditional Statements
Introduction
Flow control is a fundamental concept in any programming language, and C is no exception. It allows us to specify the flow of execution of the program. One of the most important flow control mechanisms in C is the use of conditional statements which allow us to execute different parts of our program depending on certain conditions. In this tutorial, we will explore the different conditional statements available in C.
If Statement
The most basic form of conditional execution in C is the if
statement. It has the following syntax:
if (condition) {
// code to be executed if condition is true
}
The condition
is a boolean expression, it can be either true or false. If the condition is true, the code inside the curly brackets {}
is executed. If the condition is false, the if
statement does nothing and the program continues with the code following the if
statement.
Let's see an example:
int age = 20;
if (age >= 18) {
printf("You are eligible to vote.\n");
}
In this example, if the age is 18 or more, the message "You are eligible to vote." is printed.
If-Else Statement
The if-else
statement provides a way to execute some code when the condition is false. Here is the syntax:
if (condition) {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
Let's modify the previous example to use an if-else
statement:
int age = 15;
if (age >= 18) {
printf("You are eligible to vote.\n");
} else {
printf("You are not eligible to vote.\n");
}
In this case, if the age is less than 18, the message "You are not eligible to vote." is printed.
Else If Statement
The else if
statement allows us to test multiple conditions and execute different code for each condition. Here is the syntax:
if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition1 is false and condition2 is true
} else {
// code to be executed if both condition1 and condition2 are false
}
For example:
int score = 85;
if (score >= 90) {
printf("Excellent!\n");
} else if (score >= 70) {
printf("Good job!\n");
} else {
printf("You need to work harder.\n");
}
In this example, different messages are printed depending on the value of the score.
Switch Statement
The switch
statement is another form of conditional execution in C. It is often used as a cleaner alternative to if-else if-else
statements when we need to test a variable against multiple values. Here is the syntax:
switch (expression) {
case value1:
// code to be executed if expression equals value1
break;
case value2:
// code to be executed if expression equals value2
break;
default:
// code to be executed if expression doesn't match any value
}
For example:
char grade = 'B';
switch (grade) {
case 'A':
printf("Excellent!\n");
break;
case 'B':
printf("Good job!\n");
break;
default:
printf("You need to work harder.\n");
}
In this example, different messages are printed depending on the value of the grade.
Conclusion
Understanding conditional statements is crucial for controlling the flow of execution in your C programs. Practice using if
, if-else
, else if
, and switch
statements until you feel comfortable with them. Remember, the key to learning programming is practice. Happy coding!