The if
statement in C is used for decision making. It executes a block of code if a specified condition is true. Optionally, an else
statement can be used to execute a block of code if the condition is false. Here's the basic syntax:
Here's an example:
Output:
In this example, if the condition x > 5
is true, the statement inside the if
block will be executed. Otherwise, the statement inside the else
block will be executed.
You can also use nested if-else
statements, where an if
statement is placed inside another if
or else
block. This allows for more complex decision-making logic. Additionally, you can use else if
to specify additional conditions to check if the initial condition is false. Here's an example:
Output:
In this example, x is equal to 10, so the condition in the else if statement is false. Therefore, the code inside the else block is executed.