allocated
allocated

Reputation: 1355

Weird Condition Checks

Let's consider the following C code snippet.

while(!var_can_be_0_or_1 && foo_returns_0_or_1(arg1, arg2)) {
     body;
}

it's a simple while condition statement which does what I am failing to understand. But let's say I have two macros

#define TRUE 1
#define FALSE 0

Can someone please tell me(or rather explain to me) what are we checking under the condition in this while loop here? I mean in what condition/s the loop body will execute and in which condition/s it will skip?

Elaboration

I found it in a book about Data Structures in a program which converts an infix expression string into a postfix one. The exact code was something like this --->

int prcd(char char);
int und, outpos;
char symb, topsymb;

 while(!und && prcd(topsymb, symb)) { <----- Stuck on this one real bad
            postr[outpos++] = topsymb;
            popandtest(&opstk, &topsymb, &und);
 }

The elaboration is probably unnecessary but just to put things into context. ;)

EDIT :

I'm really sorry if the question was somewhat unclear to people who are trying to help, so I'll explain a little bit more about what I am really asking here

Let's forget the code I wrote in the elaborate portion of this text and go back to the first one and let's just say we have a while loop , plain and simple while loop

while(!condition1 && condition2) {
execute some codes;
}

Here, condition1 = und, und is an int whose value can be either 0 or 1(and NOTHING ELSE). 0 and 1 are macroed to FALSE and TRUE respectively. And condition2 = prcd(topsymb, symb) ; it's a function whose prototype is mentioned as int prcd(char char);. Again it returns either 0 or 1 and NOTHING ELSE.

Now what I want to know is how the condition inside the while loop brackets gets evaluated.

Hope this clears things up. :)

Upvotes: 0

Views: 165

Answers (4)

Milind Deore
Milind Deore

Reputation: 3063

I think this is some pseudo code with missing pre conditions, because it won't compile as is.

  1. if 'und' is global then it must be initialized to zero and the while loop will always be TRUE, if inside function this must be initialized with garbage because it is allocated on stack and hence the behavior is unpredictable.
  2. Similarly, missing definition of prcd(), it is hard to suggest what it returns.

I think your need to correct the question with proper inputs.

Upvotes: 0

tjcertified
tjcertified

Reputation: 704

I am unsure if this is what you are looking for, but here is what I believe you want:

while(!var_can_be_0_or_1 &&    // if this is '0', continue; else exit
      foo_returns_0_or_1(a, b) // if this is NOT '0', continue; else exit

Does this help?

Using your macros, this is similar to writing

while (var_can_be_0_or_1 == FALSE && foo_returns_0_or_1 == TRUE)

I say similar because, if the function foo_returns_0_or_1 does NOT return a 1, but instead returns some other number, then your TRUE macro will not match, and the condition test will fail, as written above.

C does not require you to write the equality (== TRUE), but rather can evaluate on the output of the function or the state of the variable, if it is an int, and is the better choice for this statement.

As @John Bode notes, if the first condition fails, then the second condition will never be tested. Since this is a function, then it is possible that the function will never be called.

Upvotes: 2

John Bode
John Bode

Reputation: 123458

The && operator forces left-to-right evaluation. !var_can_be_0_or_1 will be fully evaluated (and any side effects applied); if the result of the expression is non-zero (true), then foo_returns_0_or_1(arg1, arg2) will be evaluated. If the result of that expression is also non-zero (true), then the body of the loop will be executed, otherwise the loop will exit.

Upvotes: 1

Sani Huttunen
Sani Huttunen

Reputation: 24375

I mean in what condition/s the loop body will execute and in which condition/s it will skip

body will execute if var_can_be_0_or_1 is false (i.e. 0) AND the return value of the function foo_returns_0_or_1 is true (i.e. not 0).

If either criteria are not met then body will be skipped.

Upvotes: 4

Related Questions