How to implement this while not from Python in C?

Asked

Viewed 183 times

1

I’m studying a Python code I need to turn to C, but the part that’s confusing me is this:

while not(f1 == 0 and fn == 1):
    ...

How would this same code be in C language?

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score, you will have so accept an answer).

2 answers

4

There is no while not, there is the operator not which is independent of while.

If you just want to apply the not you have to exchange this operator who uses the English word for the symbol ! which is what C uses. But you need to use it in the right place. and you trade for &&.

while (!(f1 == 0 && fn == 1)) {

Note that in C the parentheses of while are part of its construction and do not serve for grouping an expression. Since you need to group everything to apply the operator to the whole expression then you need to create other parentheses.

This occurs in Python because in this language parentheses are not part of the construction of while.

So even the most correct in Python would be like this:

while not (f1 == 0 and fn == 1):

It’s a blank space that seems unnecessary, but it makes all the difference to legibility, since the way it was written makes it look like it’s a function.

But the right thing to do is just reverse all the signals:

while (f1 != 0 || fn != 1) {

This saves one operation and can even facilitate the short circuit and save another. And for those who know it is even more readable.

If all will be denied then what is equal becomes different, and what is a and, becomes a or. Actually in Python it should be like this too:

while f1 != 0 or fn != 1:

Without the parentheses that can confuse when it converts from one language to another. So it becomes clearer that there is no while not, before the parentheses and lack of space gave the impression of the while not be one thing.

I’ll show you an example of how it keeps doing this. And that the inversion of all operators is the same as applying the !.

#include <stdio.h>

int main(void) {
    int f1 = 1;
    int fn = 1;
    printf("%d ", f1 == 0 && fn == 1);
    printf("%d ", !(f1 == 0 && fn == 1));
    printf("%d\n", f1 != 0 || fn != 1);
    f1 = 0;
    printf("%d ", f1 == 0 && fn == 1);
    printf("%d ", !(f1 == 0 && fn == 1));
    printf("%d\n", f1 != 0 || fn != 1);
    while (f1 != 0 || fn != 1);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Don’t forget that in C, unlike Python which uses indentation and only needs to open with :, have to close the keys when the block closes. Then I made a while without block so ended with ;. But more common is:

while (f1 != 0 || fn != 1) {
    ...
}

2

Direct translation to C would be as follows:

while (!(f1 == 0 && fn == 1)) { ... }

However, this can be simplified by using the de Morgan’s law for that reason:

while (f1 != 0 || fn != 1) { ... }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.