C if...else Statement
#include <stdio.h>
int main() {
int number = 10;
if (number > 0) {
printf("The number is positive.\n");
} else {
printf("The number is not positive.\n");
}
return 0;
}
Explanation:
#include <stdio.h>
: Includes the standard input-output header file.int main() {
: The starting point of the program execution.main()
is the function that gets executed when the program runs.int number = 10;
: Declares an integer variable namednumber
and assigns it the value10
.if (number > 0) { ... } else { ... }
: This is anif...else
statement. It checks the condition inside the parentheses (number > 0
). If the condition is true (in this case, ifnumber
is greater than0
), it executes the code inside the first set of curly braces{}
. If the condition is false, it executes the code inside the second set of curly braces{}
.printf("The number is positive.\n");
: Ifnumber
is greater than0
, this line prints "The number is positive." to the console.printf("The number is not positive.\n");
: Ifnumber
is not greater than0
, this line prints "The number is not positive." to the console.return 0;
: Indicates the end of themain()
function and returns0
, indicating successful program execution to the operating system.