Skip to content

C_If_Else.c

ani9977 edited this page Sep 11, 2024 · 1 revision

Here's the updated code with comments and explanations:

#include <stdio.h>  // Includes the standard input-output library for input-output functions

int main() {  // Main function where the program execution starts
    int number = 10;  // Declares an integer variable 'number' and assigns it the value 10

    // Checks if the 'number' is greater than 0
    if (number > 0) {
        // If the condition is true (number is greater than 0), this line is executed
        printf("Number is positive.\n");  // Prints "Number is positive." to the console
    } else {
        // If the condition is false (number is not greater than 0), this line is executed
        printf("Number is not positive.\n");  // Prints "Number is not positive." to the console
    }

    return 0;  // Returns 0 to indicate that the program executed successfully
}

Explanation:

  • int number = 10;: This line declares an integer variable number and initializes it with the value 10.
  • if (number > 0): This if statement checks if number is greater than 0. If true, the code inside the if block is executed.
  • printf("Number is positive.\n");: If number > 0, this line is executed, printing "Number is positive." to the console.
  • else: The else block is executed if the condition in the if statement is false (i.e., if number <= 0).
  • printf("Number is not positive.\n");: If the condition is false, this line is executed, printing "Number is not positive." to the console.
  • return 0;: This statement returns 0 to indicate that the program ran successfully.

This program checks whether the value of the variable number is positive or not using a simple if-else condition. It prints an appropriate message based on the result of the condition.

Clone this wiki locally