Conditional Control Statements


The if statement in C is used to conditionally execute a block of code based on a specified condition. It allows you to control the flow of execution by determining whether the code inside the if block should be executed or skipped.

  • If statement: The if statement allows you to execute a block of code if a certain condition is true. If the condition is false, the code block is skipped.
  • If-else statement: The if-else statement extends the functionality of the if statement by providing an alternative block of code to execute when the condition is false.
  • If-else-if statement: The if-else-if statement allows you to check multiple conditions and execute different code blocks based on the conditions.
  • Nested if-else statement: You can also nest if-else statements within each other to handle multiple conditions.

if Statement

The if statement in C is used to conditionally execute a block of code based on a specified condition.

The syntax of the if statement is as follows:


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

Here's an example to illustrate the usage of the if statement:


#include <stdio.h>

int main() {
    int num;

    printf("Enter a number: ");
    scanf("%d", &num);

    if (num > 0) {
        printf("The number is positive.\n");
    }

    printf("The program has ended.\n");
    return 0;
}

In this example, the user is prompted to enter a number. The if statement checks if the number is greater than 0. If the condition is true, the statement "The number is positive." is printed. Regardless of the condition's outcome, the statement "The program has ended." is always printed at the end.

if-else Conditional Statement

The if-else statement in C allows you to conditionally execute different blocks of code based on a specified condition.

The syntax of the if-else statement is as follows:


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

Here's an example to illustrate the usage of the if-else statement:


#include <stdio.h>

int main() {
    int num;

    printf("Enter a number: ");
    scanf("%d", &num);

    if (num > 0) {
        printf("The number is positive.\n");
    }
    else {
        printf("The number is non-positive.\n");
    }

    printf("The program has ended.\n");
    return 0;
}

In this example, the user is prompted to enter a number. The if-else statement checks if the number is greater than 0. If the condition is true, the statement "The number is positive." is printed. If the condition is false, the statement "The number is non-positive." is printed. Regardless of the condition's outcome, the statement "The program has ended." is always printed at the end.

if-else-if Conditional Statement

The if-else-if statement in C allows you to conditionally execute different blocks of code based on multiple specified conditions.

The syntax of the if-else-if statement is as follows:


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

Here's an example to illustrate the usage of the if-else-if statement:


#include <stdio.h>

int main() {
    int num;

    printf("Enter a number: ");
    scanf("%d", &num);

    if (num > 0) {
        printf("The number is positive.\n");
    }
    else if (num < 0) {
        printf("The number is negative.\n");
    }
    else {
        printf("The number is zero.\n");
    }

    printf("The program has ended.\n");
    return 0;
}

In this example, the user is prompted to enter a number. The if-else-if statement checks multiple conditions to determine whether the number is positive, negative, or zero. The appropriate message is printed based on the condition that evaluates to true. Regardless of the condition's outcome, the statement "The program has ended." is always printed at the end.

Nested if-else Statement

In C programming, a nested if-else statement allows you to have an if-else statement inside another if or else block. This allows for more complex conditional logic and multiple levels of decision-making.

The syntax of the nested if-else statement is as follows:


if (condition1)
{
    // Code to be executed if condition1 is true

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

Here's an example to illustrate the usage of the nested if-else statement:


#include <stdio.h>

int main() {
    int num;

    printf("Enter a number: ");
    scanf("%d", &num);

    if (num > 0) {
        printf("The number is positive.\n");

        if (num % 2 == 0) {
            printf("The number is even.\n");
        }
        else {
            printf("The number is odd.\n");
        }
    }
    else {
        printf("The number is non-positive.\n");
    }

    printf("The program has ended.\n");
    return 0;
}

In this example, the user is prompted to enter a number. The nested if-else statement checks if the number is positive. If it is, it further checks whether the number is even or odd. The appropriate messages are printed based on the conditions that evaluate to true. If the number is non-positive, the statement "The number is non-positive." is printed. Regardless of the condition's outcome, the statement "The program has ended." is always printed at the end.

Loading...

Search