C - If Condition

In C programming, the if statement, else statement, and else if statement are used for conditional execution, allowing you to control the flow of your program based on certain conditions. 

The if Statement:

The if statement allows you to execute a block of code if a certain condition is true. Its syntax is as follows:

if (condition)
{
    // Code to be executed if the condition is true
}

The condition is an expression that evaluates to either true or false. If the condition is true, the code within the curly braces is executed. If the condition is false, the code block is skipped, and the program continues to the next statement after the if block.

int x = 5;
if (x > 0)
{
    printf("x is positive\n");
}

The else Statement:

if (condition)
{
    // Code to be executed if the condition is true
}
else
{
    // Code to be executed if the condition is false
}

The else statement is used in conjunction with the if statement to specify an alternative block of code to execute when the if condition is false. Its syntax is as follows:

int x = -2;
if (x > 0)
{
    printf("x is positive\n");
}
else
{
    printf("x is non-positive\n");
}

The else if Statement:

if (condition1)
{
    // Code to be executed if condition1 is true
}
else if (condition2)
{
    // Code to be executed if condition2 is true
}
else
{
    // Code to be executed if all conditions are false
}

The else if statement allows you to specify additional conditions to check when the if condition is false. It provides an alternative path based on different conditions. Its syntax is as follows:

int x = 10;
if (x < 0)
{
    printf("x is negative\n");
}
else if (x == 0)
{
    printf("x is zero\n");
}
else
{
    printf("x is positive\n");
}

In this example, different conditions are checked sequentially, and the corresponding block of code is executed based on the first true condition encountered.