Control Flow: Conditional Statements (if, else, switch)
Using `if`, `else`, and `switch` statements to control the flow of execution based on conditions.
Control Flow: Conditional Statements (if) in C
Understanding Conditional Statements
In programming, control flow refers to the order in which statements in a program are executed. Conditional statements are essential tools for controlling this flow, allowing a program to make decisions and execute different blocks of code based on certain conditions.
The if
Statement in C
The if
statement is the most basic conditional statement in C. It allows you to execute a block of code only if a specified condition is true. The syntax of the if
statement is as follows:
if (condition) {
// Code to be executed if the condition is true
}
Let's break down the components:
if
: This keyword signifies the start of theif
statement.(condition)
: Thecondition
is an expression that evaluates to eithertrue
(non-zero value) orfalse
(zero value). It is enclosed in parentheses.{ ... }
: The code enclosed within the curly braces{ }
is the block of code that will be executed if thecondition
istrue
. If the block contains only one statement, the curly braces are optional, but it is highly recommended to always use them for better readability and maintainability.
How the if
Statement Works
The if
statement works as follows:
- The program evaluates the
condition
inside the parentheses. - If the
condition
istrue
(evaluates to a non-zero value), the code block enclosed within the curly braces is executed. - If the
condition
isfalse
(evaluates to zero), the code block is skipped, and the program continues with the next statement after theif
statement.
Example of Using the if
Statement
Here's a simple example that demonstrates the use of the if
statement in C:
#include <stdio.h>
int main() {
int age = 20;
if (age >= 18) {
printf("You are eligible to vote.\n");
}
printf("Program continues...\n");
return 0;
}
In this example:
- The variable
age
is initialized to 20. - The
if
statement checks ifage
is greater than or equal to 18. Since 20 >= 18 istrue
, the code block within theif
statement is executed, and "You are eligible to vote." is printed to the console. - The program then continues to execute the next statement, which prints "Program continues...".
If we changed age
to 15, the condition age >= 18
would be false
, and only "Program continues..." would be printed.
Important Considerations
- Truthiness and Falsiness in C: In C, any non-zero value is considered
true
, and zero is consideredfalse
. This is different from languages like Python or JavaScript, which have explicit boolean types. - Comparison Operators: Common comparison operators used in
if
statements include:==
(equal to)!=
(not equal to)>
(greater than)<
(less than)>=
(greater than or equal to)<=
(less than or equal to)
Conclusion
The if
statement is a fundamental building block in C programming, enabling you to create programs that can make decisions based on conditions. Mastering the if
statement is essential for creating dynamic and responsive applications.