0

How can I use a continue statement in a ternary operator?

for (i = 0; i < arr_size - 1; i++)
        {
                ar[i] == 1 || ar[i] == 2 || ar[i] == 3 ? count++ : continue;
        }

I can replace it like this.

for (i = 0; i < arr_size - 1; i++)
        {
                ar[i] == 1 || ar[i] == 2 || ar[i] == 3 ? count++ : count;
        }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
TheHawk3r
  • 69
  • 6

3 Answers3

4

You can't use continue in this way as it is a statement, and the operands of the ternary operator must be a expression.

Like any operator, its result has a value which can be used in other expressions. If it were allowed to use continue in the way you want, what would the value of the expression be? It doesn't make sense to use in that way.

The ternary operator isn't appropriate for what you want. It should be rewritten as an if statement:

if (ar[i] == 1 || ar[i] == 2 || ar[i] == 3) {
    count++;
}
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Re “It doesn't make sense to use in that way”: Functions with return values have results that are values which can be used in other expressions, but they contain statements, and they might not return (due to `longjmp`, `exit`, and so on). So clearly it can make sense for an expression to contain statements. This incorrect reasoning is not necessary to answer the question; the conditional operator simply requires expressions for its operands, in the absence of language extensions, regardless of the reasons. – Eric Postpischil Apr 24 '21 at 13:06
2

Ternary operators have some special rules.

For example, you only can use expressions in these three parameters. But continue; is a complete sentence. So it can't work with ternary operators.

If you have to use ternary operators with continue, you can try this:

for (i = 0; i < arr_size - 1; i++)
        {
                bool flag = false;
                ar[i] == 1 || ar[i] == 2 || ar[i] == 3 ? count++ : (flag = true);
                if(flag) continue;
        }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

See this C reference and read n1570 or some newer C standard. Read also Modern C and study for inspiration the source code of existing free software coded in C, such as GNU make.

Expressions and statements are different.

Code:

for (i = 0; i < arr_size - 1; i++)
{
    if (ar[i] == 1 || ar[i] == 2 || ar[i] == 3)
        count++;
    else
        continue;
}

Given that else continue is the last thing in the loop, you could omit it.

Hint: compile your C code with a recent GCC compiler invoked as gcc -Wall -Wextra -g.

Read the documentation of your C compiler (e.g. GCC) and your debugger (e.g. GDB).

If so allowed, use static source code analysis tools like the Clang static analyzer, or Frama-C, or Bismon or the DECODER project.

(For both Frama-C and Bismon and DECODER, contact me—in 2021—by email to basile.starynkevitch@cea.fr.)

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547