How to deal with declaration of the primitive type without the initial value known (C++)?

In some cases it happens for me to declare a variable without knowing its value first like:

int a;
if (c1) {
  a = 1;
} else if (c2) {
  a = 2;
} else if (c3) {
  a = -3;

Is it the standard professional practice to assign some clearly wrong value like -1000 anyway (making potential bugs more reproducible) or it is preferred not to add the code that does nothing useful as long as there are no bugs? From one side, looks reasonable to remove randomness, from the other side, magical and even “clearly wrong” numbers somehow do not look attractive.

In many cases it is possible to declare when the value is first known, or use a ternary operator, but here we would need it nested so also rather clumsy.

Declaring inside the block would move the variable out of the scope prematurely.

Or would this case justify the usage of std::optional<int> a and assert(a) later, making sure we have the value?

EDIT: The bugs I am talking about would occur if suddenly all 3 conditions are false that should “absolutely never happen”.


I would introduce a default value. I’m usually using MAX value of the type for this.

Shortest you can do this with the ternary operator like this:

#include <climits>

int a = c1 ? 1 : c2 ? 2 : c3 ? -3 : INT_MAX;