Accurate interpretation and evaluation of this "or condition": while ( x < 1 || x > 8 )
-
My doubt is about the basic theory of "or logical operator". Especifically, logical OR returns true only if either one operand is true.
For instance, in this OR expression (x 8) using x=5 when I evalute the 2 operands, I interpret it as both of them are false.
But I have an example that does not fit wiht it rule. On the contrary the expression works as range between 0 and 8, both included. Following the code:
#include
int main(void)
{
int x ; //This is the variable for being evaluateddo { printf("Imput a figure between 1 y 8: "); scanf("%i", &x); } while ( x < 1 || x > 8 ); // Why this expression write in this way determinate the range??? { printf("Your imput was ::: %d ",x); printf("\\n"); } printf("\\n");
}
I really appreciate any helpo in order to clarify my doubt
In advance, thank you very much. Otto
-
My doubt is about the basic theory of "or logical operator". Especifically, logical OR returns true only if either one operand is true.
For instance, in this OR expression (x 8) using x=5 when I evalute the 2 operands, I interpret it as both of them are false.
But I have an example that does not fit wiht it rule. On the contrary the expression works as range between 0 and 8, both included. Following the code:
#include
int main(void)
{
int x ; //This is the variable for being evaluateddo { printf("Imput a figure between 1 y 8: "); scanf("%i", &x); } while ( x < 1 || x > 8 ); // Why this expression write in this way determinate the range??? { printf("Your imput was ::: %d ",x); printf("\\n"); } printf("\\n");
}
I really appreciate any helpo in order to clarify my doubt
In advance, thank you very much. Otto
You might be confused by a pair of spurious braces :)
#include int main(void)
{
int x ; //This is the variable for being evaluateddo { printf("Imput a figure between 1 y 8: "); scanf("%i", &x); } while ( x < 1 || x > 8 ); //This is a do...while statement. //It repeats until x is between 1 AND 8 { //this brace is not needed and might make you believe there is a while loop printf("Your imput was ::: %d ",x); printf("\\n"); } //closing brace that should also be removed printf("\\n");
}
What the program does is to repeat the
scanf
statement until x is between 1 and 8. Antoher way to explain it: the loop repeats until the condition is false.!( (x<1) || (x>8) )
is equivalent to!(x<1) && !(x>8)
(see De Morgan's laws[^]), or(x>=1) && (x<=8)
.Mircea
-
My doubt is about the basic theory of "or logical operator". Especifically, logical OR returns true only if either one operand is true.
For instance, in this OR expression (x 8) using x=5 when I evalute the 2 operands, I interpret it as both of them are false.
But I have an example that does not fit wiht it rule. On the contrary the expression works as range between 0 and 8, both included. Following the code:
#include
int main(void)
{
int x ; //This is the variable for being evaluateddo { printf("Imput a figure between 1 y 8: "); scanf("%i", &x); } while ( x < 1 || x > 8 ); // Why this expression write in this way determinate the range??? { printf("Your imput was ::: %d ",x); printf("\\n"); } printf("\\n");
}
I really appreciate any helpo in order to clarify my doubt
In advance, thank you very much. Otto
# How does logical OR work? "Logical or," not to be confused with "bitwise or," is a short-circuiting operator that evaluates to an integer, either 0 or 1. Because it is short-circuiting, the second operand will not be evaluated if the first operand determines the result. In the case of OR, that means if the first operand is non-zero. (In the case of logical AND, the evaluation short-circuits if the first operand IS zero, since that determines the result.) Finally, if the second operand is evaluated, there is guaranteed to be a sequence point before evaluation of the second operand. This means it is "safe" to use things that require sequence points, like the operators that have side-effects (++, --). Consider this code: ``` int first(int x) { puts("first"); return x; } int second(int x) { puts("second"); return x; } void test(int x) { if (first(x) || second(x)) { puts("logical or returns true"); } else { puts("logical or returns false"); } } ``` If I call ``test(0)`` what will happen? The ``test`` function will call ``first(0)`` which will print its message and return 0. Because 0 is "false", the result of the logical or is *not* determined, so the second operand is evaluated. This calls ``second(0)`` which will print its message and return 0. Since both sides evaluate to 0 (false), the OR also evaluates to 0 and the ``if`` fails, so the ``else`` clause is invoked. The output is something like: ``` first second logical or returns false ``` Now, if I call ``test(1)`` what happens? The first operand is ``first(1)`` which prints its message and returns 1. 1 is truthy, so the result of the logical OR is determined and there is no need to evaluate the second operand. The call to ``second`` is skipped and the ``if`` succeeds and prints its message. The output looks like: ``` first logical or returns true ``` # How does the looping code work? The code in question is: ``` do { printf("Imput a figure between 1 y 8: "); scanf("%i", &x); } while ( x < 1 || x > 8 ); // Why this expression write in this way determinate the range??? ``` A ``do/while`` statement first invokes the body, then evaluates the condition. If the condition is true, the body is repeated and the condition re-evaluated until such time as the condition becomes untrue. This statement is designed for tasks like input validation, where t
-
You might be confused by a pair of spurious braces :)
#include int main(void)
{
int x ; //This is the variable for being evaluateddo { printf("Imput a figure between 1 y 8: "); scanf("%i", &x); } while ( x < 1 || x > 8 ); //This is a do...while statement. //It repeats until x is between 1 AND 8 { //this brace is not needed and might make you believe there is a while loop printf("Your imput was ::: %d ",x); printf("\\n"); } //closing brace that should also be removed printf("\\n");
}
What the program does is to repeat the
scanf
statement until x is between 1 and 8. Antoher way to explain it: the loop repeats until the condition is false.!( (x<1) || (x>8) )
is equivalent to!(x<1) && !(x>8)
(see De Morgan's laws[^]), or(x>=1) && (x<=8)
.Mircea
Hi Mrice, very interesting your contibutions. I am just beginning and exploring basics of programming. And understand it is like a puzzle for me. Also Morgan's law is good reference. Something abstract, but for sure I am going to review it deeply. I really apreciate all your time you took for answer me- Otto Medina
-
# How does logical OR work? "Logical or," not to be confused with "bitwise or," is a short-circuiting operator that evaluates to an integer, either 0 or 1. Because it is short-circuiting, the second operand will not be evaluated if the first operand determines the result. In the case of OR, that means if the first operand is non-zero. (In the case of logical AND, the evaluation short-circuits if the first operand IS zero, since that determines the result.) Finally, if the second operand is evaluated, there is guaranteed to be a sequence point before evaluation of the second operand. This means it is "safe" to use things that require sequence points, like the operators that have side-effects (++, --). Consider this code: ``` int first(int x) { puts("first"); return x; } int second(int x) { puts("second"); return x; } void test(int x) { if (first(x) || second(x)) { puts("logical or returns true"); } else { puts("logical or returns false"); } } ``` If I call ``test(0)`` what will happen? The ``test`` function will call ``first(0)`` which will print its message and return 0. Because 0 is "false", the result of the logical or is *not* determined, so the second operand is evaluated. This calls ``second(0)`` which will print its message and return 0. Since both sides evaluate to 0 (false), the OR also evaluates to 0 and the ``if`` fails, so the ``else`` clause is invoked. The output is something like: ``` first second logical or returns false ``` Now, if I call ``test(1)`` what happens? The first operand is ``first(1)`` which prints its message and returns 1. 1 is truthy, so the result of the logical OR is determined and there is no need to evaluate the second operand. The call to ``second`` is skipped and the ``if`` succeeds and prints its message. The output looks like: ``` first logical or returns true ``` # How does the looping code work? The code in question is: ``` do { printf("Imput a figure between 1 y 8: "); scanf("%i", &x); } while ( x < 1 || x > 8 ); // Why this expression write in this way determinate the range??? ``` A ``do/while`` statement first invokes the body, then evaluates the condition. If the condition is true, the body is repeated and the condition re-evaluated until such time as the condition becomes untrue. This statement is designed for tasks like input validation, where t
Hi Aghast Thanks for your time. I have red your answer at least about 10 times, and I feel that I am close to understand the logic of the determined range in this do while expression. I really appreciate all your pacient in this excellent explanation. Otto