0

I noticed sometimes developers do these in their project;

while(int x = 0) {//run code}
int y = 0;
if (y = someFunction())
{//run code}

I want ask;

  1. why c++ allow to do these in loops like while,(maybe for as well?) and if-else statements, what is the usage?
  2. when someone should do this in his project?
  3. and when conditions like these give result true?
  • I don't remember to see it used in `while` and your `while(int x = 0)` will execute the loop `0` times, hence unlikely that you saw it like this in code. Do you have a more "real" example? If you really saw `while(int x=0)` then that could be a typo in the code you were reading – 463035818_is_not_an_ai Nov 18 '20 at 11:35
  • If they had written `while(int x = 0) {` then they are pulling your leg. Actually this is one reason why I prefer `while(!x)`, rather than an explicit comparison to zero. The yoda type expressions `while (0 == x)` make me feel like I've just drunk two bottles of a cheap Merlot. – Bathsheba Nov 18 '20 at 11:35
  • Voted for reopening. It is not an duplicate of the proposed duplicate, or at least not an exact duplicate. In this question the assignment is an assignment to zero. – user1810087 Nov 18 '20 at 11:35
  • @user1810087: You have an answer prepared? If so I'll reopen. – Bathsheba Nov 18 '20 at 11:36
  • 2
    I'm happy to reopen as well, if you don't think it's a duplicate. I certainly wouldn't mind seeing an explanation of `while(int x = 0) {`. – cigien Nov 18 '20 at 11:36
  • 1
    @Bathsheba You can always ping the user who dupe-hammers a post if you disagree with the closure. They are accountable for voting to close, and in most cases will be happy to reevaluate. – cigien Nov 18 '20 at 11:38
  • 1
    @cigien: Ping!!!!! – Bathsheba Nov 18 '20 at 11:39
  • I think most people with the hammer will at least read the comments, and be most curious to see the answer. – paddy Nov 18 '20 at 11:39
  • 2
    @Batsheba you are an easy guest then. Showing you `while(0 != x)` for dinner is even cheaper than the merlot – 463035818_is_not_an_ai Nov 18 '20 at 11:41
  • fwiw, I agree that the dupe is not the right one, but then still the question needs clarification. "I have seen sometimes" should come with a real example.... or maybe not, the question can be answered as it stands now – 463035818_is_not_an_ai Nov 18 '20 at 11:43
  • 3
    Note that the 2 snippets are not equivalent. The first one evaluates to `false`, but the second one depends on `someFunction`. Could you also add the source where you saw this? The surrounding context might help understand the use case. – cigien Nov 18 '20 at 11:44
  • I've actually seen once `while(int x = 0) {` in code made by a college. It was a very odd way used for debugging purpose. He would literally change the value `0` to some number to stress another function. He also used some defines like `#if 0` which is not uncommon https://stackoverflow.com/questions/2232737/if-0-as-a-define . But this is not a definitive answer, to the question... – user1810087 Nov 18 '20 at 11:47
  • @user1810087 That's a preprocessor conditional. OP is asking about a run-time conditional. I think those are different things. – cigien Nov 18 '20 at 11:49
  • 1
    @user1810087 I have to resort to such madness when coding Java sometimes, because our settings don't allow me to have unreachable code, maybe thats similar – 463035818_is_not_an_ai Nov 18 '20 at 11:49
  • @user1810087: I prefer a `goto` for that purpose as I have a static analyser set up to fail a build of any code containing a `goto`. If you leave an `#if 0` or `while(int x = 0)` by accident all hell could be let loose. – Bathsheba Nov 18 '20 at 11:53
  • @cigien you are right, and I don't want to give the impression to have the answer for this or that it is my preferred way do go. I'm only trying to say I witnessed such code myself, but had the luck to ask the programmer for the reason. :-) – user1810087 Nov 18 '20 at 11:56
  • @user1810087 I understand, it certainly sounds like fun :) I'm not entirely sure that such a discussion is suitable though. It seems to invite opinions, rather than facts. – cigien Nov 18 '20 at 11:59

1 Answers1

2

what is the usage?

Declaration inside if/for/while allows to reduce scope of the variable.

so, instead of

int y = somevalue();
if (y) {// y != 0
// ...
}
// y still usable :/

or

{ // extra block :/
    int y = somevalue();
    if (y) {// y != 0
        // ...
    }
}
// y no longer usable :)

you might directly do:

if (int y = somevalue()) // y != 0
    // ...
}
// y no longer usable :)

with syntax which might indeed surprise.

For more controversial:

int y;
// ...
if (y = somevalue()) // y != 0
    // ...
}
// y still usable :)

it is allowed as that assignation is an expression convertible to bool (value of y = x is y (which has been assigned to x)).

it is more controversial as error prone as unclear if = or == is expected. some convention use extra parents to more clearly express assignation

if ((y = somevalue())) // really = intended, not ==
    // ...
}

when someone should do this in his project?

For existing projects, try to use existing convention.

For new project, you have to do the trade of:

scope versus syntax not shared with other language which might be surprising

and when conditions like these give result true?

when given expression convert to true, so non zero integral, non null pointer.

c++17 introduces another syntax to allow to split declaration and condition for if/while. (for already allow that split)

if (int y = somevalue(); y != 42) // y != 42
    // ...
}
// y no longer usable :)
Jarod42
  • 203,559
  • 14
  • 181
  • 302