# 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
Aghast nj
Posts
-
Accurate interpretation and evaluation of this "or condition": while ( x < 1 || x > 8 ) -
Plz solve itQuote:
Count the digits of a given number c programming
You don't specify what kind of number it is (floating point, integer), nor how it is provided. Is the number coming in as a string, for example from ``argv`` passed to ``main``? Is the number passed in to a function as a parameter? Does the function need a particular name? Is the number allowed to be negative? Is there a maximum value for the number? Will there be decimal points? Is the number base 10, or base 2, or base 16, or is there a prefix at the front that specifies the radix? What should the result be if no number is given, or an invalid number is given? Are there any unit test cases that would provide a good example of how this is supposed to work?
-
C(17) Complete Tutorial ReferencesTry this video series: https://youtu.be/F3ntGDm6hOs This is a "pre-introduction" series of 5 videos, to the Handmade Hero video series. HH is an ongoing series and may/may not be valuable to you, but the prelim is a good, fast intro that explains a bunch of concepts on Windows.
-
C(17) Complete Tutorial ReferencesTry this video series: https://youtu.be/F3ntGDm6hOs This is a "pre-introduction" series of 5 videos, to the Handmade Hero video series. HH is an ongoing series and may/may not be valuable to you, but the prelim is a good, fast intro that explains a bunch of concepts on Windows.
-
Regex only want the first matchCan you explain more what you're trying to do? Also, what tools/language are you using?
-
regex pattern in order to get a list of numbers from a stringYou don't say what environment you are working in, so it's hard to give you useful code. However, you can probably get what you want by either doing a search & replace operation, possibly repeated, or by using some kind of "regex iterator" approach. For C++, it would be a regex iterator (iterate over all the matches of THIS pattern in THAT string). For something like Python it would be find all occurrences of THIS pattern in THAT string. In order to drop the "8;" from your example sentence, you'd want to provide a better pattern than just "some digits." Instead, use something like /(\d+):/ to match on one-or-more digits followed by a colon. You want to capture the digits, not the colon. If you can just iterate over all matches in the string, that will probably be enough. If you're doing sed or some other editor, you'll have to search and replace the line. If possible, see if you can do something like a non-greedy match (.*?) to match all the text between matches, and then replace it with a comma. (FYI: The ? operator is a non-greedy modifier in Perl-style regex engines. Other regex engines may not support this at all, or they may have a different syntax-- for example, Vim would use .\{-} for that.