C Language (Branching Statements)
C Language (Branching Statements)
if (condition)
{
# Code to execute if condition is true
}
Decision control Branching statements
>If-Else Statement: An "if-else" statement is an extension of the "if“ statement.
If the condition in the "if" part is true, it runs one block of code; if the
condition is false, it runs another block of code in the "else" part.
Syntax:
if (condition)
{
// Code to execute if condition is true
}
else
{
// Code to execute if condition is false
}
Decision control Branching statements
>Else-If Statement: An "else-if" statement allows you to check multiple
conditions in sequence. If the condition in the initial "if" statement is false, the
program checks subsequent "else-if" conditions until it finds a true one or
reaches the "else" part.
Syntax: if (condition1)
{
// Code to execute if condition1 is true
}
else if (condition2)
{
// Code to execute if condition2 is true
}
// ... (more else if conditions)
else
{
// Code to execute if no condition is true
}
Decision control Branching statements
>Nested If Statement: A nested "if" statement is an "if" statement inside
another "if" statement. This is used when you want to check additional
conditions based on the result of an outer condition.
Syntax:
if (outer_condition)
{
if (inner_condition)
{
// Code to execute if both conditions are true
}
}
Decision control Branching statements
>Switch Case Statements: A "switch" statement is used to select one of many code blocks to be executed.
It's like a multiple-choice menu. You provide a value, and the program matches it to a predefined list of
values (cases) and executes the corresponding code block.