Click here to Skip to main content
15,887,175 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Native VS Debugger Question Pin
Richard MacCutchan29-Jun-21 10:16
mveRichard MacCutchan29-Jun-21 10:16 
QuestionMessage Closed Pin
27-Jun-21 5:05
Member 1496877127-Jun-21 5:05 
AnswerRe: How to verify apt install (application file ) executable installation ? Pin
Richard MacCutchan27-Jun-21 5:26
mveRichard MacCutchan27-Jun-21 5:26 
AnswerRe: How to verify apt install (application file ) executable installation ? Pin
k505427-Jun-21 5:56
mvek505427-Jun-21 5:56 
QuestionAccurate interpretation and evaluation of this "or condition": while ( x < 1 || x > 8 ) Pin
Otto Medina25-Jun-21 21:36
Otto Medina25-Jun-21 21:36 
AnswerRe: Accurate interpretation and evaluation of this "or condition": while ( x < 1 || x > 8 ) Pin
Mircea Neacsu26-Jun-21 1:25
Mircea Neacsu26-Jun-21 1:25 
GeneralRe: Accurate interpretation and evaluation of this "or condition": while ( x < 1 || x > 8 ) Pin
Otto Medina26-Jun-21 18:51
Otto Medina26-Jun-21 18:51 
AnswerRe: Accurate interpretation and evaluation of this "or condition": while ( x < 1 || x > 8 ) Pin
Aghast (nj)26-Jun-21 3:13
Aghast (nj)26-Jun-21 3:13 

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 the body is used to collect input, then the condition is used to determine if the input is invalid. Because the body is always executed, it forces the input to be collected at least once, as compared with the "while (condition) {body}" loop form, where a premature condition could prevent the body from executing.

So in the code example, we have a prompt for input, then a scanf statement for collecting input (Note: see here: http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html). This "prompt / input" body is surrounded by a do/while loop so the input will be collected at least once before the input variables are checked.

The input validation is to repeat the input while (x < 1 || x > 8). This means that either of those conditions is NOT acceptable. If x is less than 1, repeat the prompt and input. Or, if x is greater than 8, repeat the prompt and input. Only when the logical or fails and both conditions are false will the input loop be stopped and the input considered acceptable.

This means that the valid range for the input is [1, 8]. Anything eise just gets the prompt again.
GeneralRe: Accurate interpretation and evaluation of this "or condition": while ( x < 1 || x > 8 ) Pin
Otto Medina29-Jun-21 2:30
Otto Medina29-Jun-21 2:30 
QuestionMessage Closed Pin
16-Jun-21 3:19
Member 1496877116-Jun-21 3:19 
AnswerRe: How do I access private variable of an object ? Pin
Mircea Neacsu16-Jun-21 3:23
Mircea Neacsu16-Jun-21 3:23 
GeneralMessage Closed Pin
16-Jun-21 5:50
Member 1496877116-Jun-21 5:50 
AnswerRe: How do I access private variable of an object ? Pin
Mircea Neacsu16-Jun-21 6:12
Mircea Neacsu16-Jun-21 6:12 
GeneralRe: How do I access private variable of an object ? Pin
David Crow16-Jun-21 6:37
David Crow16-Jun-21 6:37 
AnswerRe: How do I access private variable of an object ? Pin
Richard MacCutchan16-Jun-21 6:58
mveRichard MacCutchan16-Jun-21 6:58 
AnswerRe: How do I access private variable of an object ? Pin
RedDk16-Jun-21 7:22
RedDk16-Jun-21 7:22 
AnswerRe: How do I access private variable of an object ? Pin
CPallini16-Jun-21 23:27
mveCPallini16-Jun-21 23:27 
GeneralRe: How do I access private variable of an object ? Pin
Richard MacCutchan17-Jun-21 0:35
mveRichard MacCutchan17-Jun-21 0:35 
GeneralRe: How do I access private variable of an object ? Pin
CPallini17-Jun-21 1:32
mveCPallini17-Jun-21 1:32 
GeneralMessage Closed Pin
17-Jun-21 6:36
Member 1496877117-Jun-21 6:36 
GeneralRe: How do I access private variable of an object ? Pin
Richard MacCutchan17-Jun-21 6:40
mveRichard MacCutchan17-Jun-21 6:40 
GeneralRe: How do I access private variable of an object ? Pin
CPallini17-Jun-21 7:43
mveCPallini17-Jun-21 7:43 
AnswerRe: How do I access private variable of an object ? Pin
Magnus Forslund15-Jul-21 2:19
Magnus Forslund15-Jul-21 2:19 
QuestionMessage Closed Pin
15-Jun-21 10:35
Member 1496877115-Jun-21 10:35 
AnswerRe: Pass a function with parameter Pin
Greg Utas15-Jun-21 11:38
professionalGreg Utas15-Jun-21 11:38 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.