0

Sorry, I couldn't find a better fitting title, feel free to edit the title if you find a more suthing one. Here is my question, I know I can create a small version of IF/ELSE statement like this:

(condiction) ? numFound = true : numFound = false;

but, is there a way of having two statements inside the first condition, ex.

(condition) ? numFound = true, break: numFound = false;

normal version of if/else statement I want to write

if (condition)
{
    numFound = true;
    break;
}
else
    numFound = false;

Sorry for the noob question, and thanks for the help!

Felipe Lopez
  • 396
  • 3
  • 11
  • 2
    `break` is not an expression, so you simply can't use it that way. What's so bad about writing the full `if/else` anyway? – SirGuy Apr 13 '17 at 18:26
  • 2
    I'd probably do something like `numFound = condition; if (numFound) break;` – Fred Larson Apr 13 '17 at 18:27
  • 2
    if I was to read the code I wanted the `break` to be clearly visible and not hidden in a line full of other statements – 463035818_is_not_an_ai Apr 13 '17 at 18:27
  • 5
    especially on a more complex use I wouldnt want to see everything in a single line :P – 463035818_is_not_an_ai Apr 13 '17 at 18:29
  • Thank you all for the help, I learned to just stop being lazy and use the 2-3 extra lines of code :) – Felipe Lopez Apr 13 '17 at 18:43
  • That's not how you're supposed to use the ternary operator. To respect the purpose and properties of the operator you should write `numFound = condition ? value1 : value2;`. (Which if `value1` and `values2` are `true` and `false`, amounts to `numFound = condition;`.) – n.caillou Apr 13 '17 at 19:20

5 Answers5

3

In short, it isn't possible. The Ternary operator requires the operands to evaluate to a value, and code blocks don't. Comma expressions don't work, because they will set the value to the last one executed- break doesn't evaluate to a value. Additionally, break isn't a function to be evaluated, it's a statement.


The advice is to not use conditionals/ternary operators when you are doing things more complex than their base use- they're harder to parse by humans and any decent compiler will compile x = condition ? this:that the same as if(condition){x=this}else{x=that} (or optimize both to the same assembly).

Delioth
  • 1,564
  • 10
  • 16
2

No you cannot do that. The "small version" of the if/else is called the conditional operator. It is the only operator in c++ taking three operands and commonly also simply called "the ternary operator". From here:

Exp1 ? Exp2 : Exp3;

where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon. The value of a ? expression is determined like this: Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire ? expression. If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression.

And for some clarification what is an expression see this question. break is not an expression, but a statement, hence you cannot use it inside a ternary.

Anyhow I would advise you not to hide the break within more stuff in a single line. In a loop a break is something extremely important and it makes sense to make it stand out from the "normal" buissness that happens inside the loop. If I dont immediately see a break (or a return) in a loop then I assume that it does its full iteration. Overlooking a break can cause mayor confusion and misunderstanding.

Community
  • 1
  • 1
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
1

You could use comma expressions. A comma expression of the form (expr1, expr2) evaluates expr1 (and ignores the value to which it evaluates) and then expr2, which's value is then the result of the overall comma expression.

int main(int argc, char* argv[]) {

    bool r = (argc==1) ? (printf("no parameters"),true) : (printf("parameters"),false);
    return 0;
}
Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
0

My friend you're in luck. Use comma operator:

(condition) ? numFound=true, x=1, y=2: numFound = false;

Of course if you're specific to break; it won't work.

GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
0

Your break doesn't work here but the task itself works. But be careful! Strange things can happen. See these examples:

0?printf("One"),printf("One"):printf("Two"),printf("Two");  /* TwoTwo */

0?(printf("One"),printf("One")):(printf("Two"),printf("Two")); /* TwoTwo */

1?printf("One"),printf("One"):printf("Two"),printf("Two");  /* OneOneTwo (!) */

1?(printf("One"),printf("One")):(printf("Two"),printf("Two")); /* OneOne */

This happens because of the comma operator which evaluates an expression fomr left to right and returns the value of the second statement for the whole expression.

The next step is to check the binding precedence of (), ?: and ,. We find here that the opertators are ordered from strong to weak like

  1. ()
  2. ?:
  3. ,

Therefore the third example I posted is evaluated as follows:

   1?printf("One"),printf("One"):printf("Two"),printf("Two")
-> (1?printf("One"),printf("One"):printf("Two")),printf("Two")
-> (printf("One"),printf("One")),printf("Two")
-> OneOneTwo

I do really not recommend using the so called ternary operator in more complicated cases. Stick to clear code. You know "Code is read more than it is written."

dtell
  • 2,488
  • 1
  • 14
  • 29