C If … Else
1. What is if...else?
-
The
if...elsestatement allows your program to execute certain code only if a condition is true, and optionally another block if the condition is false. -
Syntax:
2. Simple if Statement
Output:
The code inside
{}executes only if the condition is true.
3. if...else Statement
Output:
4. if...else if...else Statement
-
Used when you have multiple conditions.
Output:
5. Nested if Statements
-
You can place one
ifinside another to check multiple conditions.
Output:
6. Key Points
-
Conditions must evaluate to true (non-zero) or false (0).
-
Use
else iffor multiple alternative conditions. -
Nested
ifallows checking complex scenarios. -
Curly braces
{}are optional for single-line statements, but recommended for readability.
7. Combined Example
Output:
