Why Can't I Use a "Break" Statement Inside a Ternary Conditional Statement in C++

Why can't I use a break statement inside a ternary conditional statement in C++?

The ternary conditional operator is an operator that combines multiple expressions into a larger expression. break is a statement and not an expression, so it can't be used inside a ternary conditional expression.

You could, though, rewrite your code like this:

while (current->left != nullptr) current = current->left;

Hope this helps!

Why can't I use break in a C# ternary expression?

It isn't possible using the ternary operator, but you can simplify your code structure as follows:

string input;
do {
Console.WriteLine("Enter 3 words seperated by spaces: ");
input = Console.ReadLine();
if (input != "") {
ConvertToPascal(input);
}
} while(input != "");

How to break a for loop in Ternary Operator

Each part of the ternary operator must be able to evaluate to the same type*, so you can't have one part output (which will return the cout object) while the other break's.

Instead, in your situation, why don't you add the condition to your for?

for(int i = 0; i < x.size() && x[i] == rating; i++) {
cout<<"Found your rating on pozition ";
}

But this might not be what you actually want

It seems you're trying to find an item's position in the array. If this is the case, and you're only looking for the first occurence, I'd suggest this instead:

int pos;
for(pos = 0; pos < x.size() && x[pos] != rating; pos++) { }

if(pos != x.size()) {
cout<<"Found your rating on pozition " << pos;
} else {
cout << "Couldn't find it!";
}

Or even better, use std::find!


*You could also have a throw in there. Thanks to @Lightness!

combined usage of shorthand c if else statement and break in a loop resulted an error

The conditional operator ?: and if statements aren't compatible replacements.

The conditional operator can only be used with scalar (arithmetic or pointer) operands; it cannot be used for program flow control. So it can't contain break and similar. In addition, it comes with a couple of subtle hiccups such as implicit type promotion and operator precedence issues. The only advantage of ?: over if is pretty much that it returns a value.

My rule of thumb is that if you can use if instead of ?:, then do it.

This gives safer and (usually) more readable code. The main use of ?: is when writing various function-like macros (which is something that should be avoided in the first place). Some rare exceptions exist where ?: does give more readable code, but my general advice is to just stay clear of it.

Shorthand If/Else statement with two statements inside IF

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.

Continue or break did not work in ternary operator

The problem with System.out.println(). Since System.out.println() always expect something that can be print eg.- String, int, byte etc.

But here continue is a statement to manage the control flow of a program and in returns nothing.

In this context you don't need to add the continue statement, since the flow is automatically do what you expect. And since you want to avoid space then you can do this -

int i=0;
while(++i<=30)
System.out.print(i%5==0?i : "");

How to break a loop by a shorthand if-else result?

Here is some information on the ternary operator and it's uses:
?: Operator (C#)

What you want to do is just impossible... it's not how it works.



Related Topics



Leave a reply



Submit