/* * this shows the lazy evaluation of logic expressions in C * * Matt Bishop, ECS36A * April 9, 2024 original version */ #include 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); }