C if Statement

#include <stdio.h>

int main() {
    int num = 10;

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

    return 0;
}
  1. Include Header File:
  2. #include <stdio.h> includes the standard input-output library for using functions like printf.

  3. Main Function:

  4. int main() { marks the beginning of the main function, which is the entry point of a C program.

  5. Variable Declaration:

  6. int num = 10; declares an integer variable num and initializes it with the value 10.

  7. If Statement:

  8. if (num > 0) { checks if the value of num is greater than 0.

  9. Print Statement (If True):

  10. printf("The number is positive.\n"); prints a message to the console if the condition in the if statement is true.

  11. Else If Statement:

  12. } else if (num == 0) { provides an alternative condition to check if the value of num is equal to 0, in case the first condition is false.

  13. Print Statement (Else If True):

  14. printf("The number is zero.\n"); prints a message to the console if the condition in the else if statement is true.

  15. Else Statement:

  16. } else { handles the case when both the if and else if conditions are false.

  17. Print Statement (Else True):

  18. printf("The number is negative.\n"); prints a message to the console if none of the previous conditions is true.

  19. Return Statement:

    • return 0; indicates that the program has executed successfully and exits the main function.
  20. Closing Brace:

    • } marks the end of the main function.