CS 116 Computing Fundamentals: Boolean Operators Selection Dr. Christina Class
CS 116 Computing Fundamentals: Boolean Operators Selection Dr. Christina Class
Fundamentals
Boolean Operators
Selection
Dr. Christina Class
Two important basics:
part 1
13.5 true
-27.86 true
0 false
15 - 3 * 5 false
‘x‘ true
Attention!
int x;
printf("please enter a value for x: ");
scanf("%d",&x);
if (x < 0) {
x = x * -1;
printf("the absolute value of x is: %d", x);
} else {
printf("the absolute value of x is: %d", x);
}
we can move the common part to after the selection empty else
int x;
printf("please enter a value for x: ");
scanf("%d",&x);
if (x < 0) {
x = x * -1;
} else {
}
printf("the absolute value of x is: %d", x);
int x;
printf("please enter a value for x: ");
scanf("%d",&x);
if (x < 0) {
x = x * -1;
} else
printf("the absolute value of x is: %d", x);
int x;
printf("please enter a value for x: ");
scanf("%d",&x);
if (x < 0) {
x = x * -1;
} else
;
printf("the absolute value of x is: %d", x);
int x;
printf("please enter a value for x: ");
scanf("%d",&x);
if (x < 0) {
x = x * -1;
}
printf("the absolute value of x is: %d", x);
int x;
printf("please enter a value for x: ");
scanf("%d",&x);
if (x < 0) {
printf("the absolute value of x is: %d", x*-1);
} else {
printf("the absolute value of x is: %d", x);
}
int x;
printf("please enter a value for x: ");
scanf("%d",&x);
if (x < 0)
printf("the absolute value of x is: %d", x*-1);
else
printf("the absolute value of x is: %d", x);
a>b
a > b and a ≤ c
a≤b
a ≤ b and b > c
a ≤ b and b ≤ c
if (a==5) {
printf("a is 5\n");
}
if (a==10) {
printf("a is 10\n");
}
else {
printf("else case");
}
return 0; it belongs to the second if
}
Write a program.
read an integer value from the user
output “the value is between 1 and
10” if it is > 1 and < 10
output “the value is not between 1
and 10” if it is ≤ 1 or ≥ 10
int main()
{ code duplication
int val;
scanf("%i", &val);
if(val > 1) {
if(val < 10) {
printf("the value is between 1 and 10\n");
} else {
printf("the value is not between 1 and 10\n");
}
} else {
printf("the value is not between 1 and 10\n");
}
return 0;
}
13.5 true
-27.86 true
0 false
15 - 3 * 5 false
‘x‘ true
on handout