In C, both if statements and while loops rely on the idea of Boolean expressions.
This program accepts a number from the user. It then tests the number using an if statement to see if it is less than 0. If it is, the program prints a message. Otherwise, the program is silent. The (b < 0) portion of the program is the Boolean expression. C evaluates this expression to decide whether or not to print the message. If the Boolean expression evaluates to True, then C executes the single line immediately following the if statement. If the Boolean expression is False, then C skips the line or block of lines immediately following the if statement.
Short example:
Here are all of the Boolean operators in C:
This program accepts a number from the user. It then tests the number using an if statement to see if it is less than 0. If it is, the program prints a message. Otherwise, the program is silent. The (b < 0) portion of the program is the Boolean expression. C evaluates this expression to decide whether or not to print the message. If the Boolean expression evaluates to True, then C executes the single line immediately following the if statement. If the Boolean expression is False, then C skips the line or block of lines immediately following the if statement.
Short example:
#include <stdio.h>
#include<conio.h>
void main()
{
int b;
printf("Enter a value:");
scanf("%d", &b);
if (b < 0)
printf("The value is negative\n");
getch();
}
Here are all of the Boolean operators in C:
equality == less than < Greater than > <= <= >= >= inequality != and && or || not !
No comments:
Post a Comment