/* * this shows the lazy evaluation of logic expressions in C * * Matt Bishop, ECS36A * April 9, 2024 original version */ #include <stdio.h> int main(void) { int x, y, z, a; /* set the values of the variables in the logic expressions */ x = 12; y = 29; z = -1; printf("x = %d, y = %d, z = %d\n", x, y, z); /* lazy evaluation: false || false && ... is false */ a = (x > y || y < z && x < z); printf("(x > y || y < z && x < z) = %d\n", a); /* lazy evaluation: false || true && true is true */ a = (x > y || y > z && x > z); printf("(x > y || y > z && x > z) = %d\n", a); /* lazy evaluation: false && ... is false */ a = (x > y && y > z); printf("(x > y && y > z) = %d\n", a); /* lazy evaluation: false && ... is false */ /* note the second term is not evaluated */ /* so z is not incremented */ printf("z = %d\n", z); a = (x > y && y > ++z); printf("(x > y && y > ++z) = %d\n", a); printf("z = %d\n", z); /* lazy evaluation: true && true is true */ /* note the second term is evaluated so */ /* z is incremented */ printf("z = %d\n", z); a = (x < y && y > ++z); printf("(x < y && y > ++z) = %d\n", a); printf("z = %d\n", z); /* * hasta la vista! */ return(0); }
|
ECS 36A, Programming & Problem Solving Version of April 2, 2024 at 12:13PM
|
You can get the raw source code here. |