-
Notifications
You must be signed in to change notification settings - Fork 0
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
}-
int number = 10;: This line declares an integer variablenumberand initializes it with the value10. -
if (number > 0): Thisifstatement checks ifnumberis greater than 0. If true, the code inside theifblock is executed. -
printf("Number is positive.\n");: Ifnumber > 0, this line is executed, printing "Number is positive." to the console. -
else: Theelseblock is executed if the condition in theifstatement is false (i.e., ifnumber <= 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.