-7

I would like to know the correct usage of conditionals such as if statements to avoid undefined behaviours. Let's start with an example:

uint8_t x = 0;
bool y = false;
bool z = false;

if ((x == 135) and !y and !z) {
    //do something
}
else if ((x == 135) and y) {
    x = 5;
    z = true;   
}
else if ((x == 5) and z) {
    x = 135;
    z = false;
}
else {
    //do something
}

Now, will I get undefined behaviour by not including all 3 variables into every condition? Will every unaccounted for condition go into the else statement? If so, what happens if I get rid of the else statement? I have the exact same if statement (in a more complex scenario) and I seem not to be getting into the right statements every time.

Please enlighten me if there is a rule for this?

Superlokkus
  • 4,731
  • 1
  • 25
  • 57
Hypomania
  • 41
  • 4
  • 9
    You don't have to include _any_ of the variables in your conditions. –  Mar 11 '19 at 16:25
  • 3
    Welcome to Stack Overflow! Sounds like you could use a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – NathanOliver Mar 11 '19 at 16:26
  • @NeilButterworth, I understand that but for the sake of the demonstration I have. What's wrong with my "flow"? – Hypomania Mar 11 '19 at 16:26
  • That would not be undefined behavior. That would be a bug in your code. Which we can't debug since we don't know what you want the code to do. – Federico klez Culloca Mar 11 '19 at 16:26
  • There is no undefined behaviour. – Dmytro Dadyka Mar 11 '19 at 16:26
  • @NathanOliver, I got C++ Primer :) – Hypomania Mar 11 '19 at 16:27
  • 11
    *"will I get undefined behaviour"* You might be under impression that you can get UB out of thin air in C++, but it's not *that* bad. :p – HolyBlackCat Mar 11 '19 at 16:27
  • "Undefined behavior" is a specific term in C++ where you do something that's explicitly called out in the standard as a big "don't do this." What you're describing is more along the lines of a common oversight in code, that, as others have said, you'll find while debugging. – jwismar Mar 11 '19 at 16:30
  • 2
    it think it would help to make the question more clear if you try more to explain why you think there is something wrong with your code. There is nothing obviously wrong, so it isnt that easy to answer – 463035818_is_not_an_ai Mar 11 '19 at 16:32
  • how did you get the idea that your code has something do to with undefined behaviour? – 463035818_is_not_an_ai Mar 11 '19 at 16:32
  • 1
    @DmytroDadyka This sounds borderline unconstructive, imo. Maybe you should explain why exactly you think it doesn't "carry any value"? – HolyBlackCat Mar 11 '19 at 16:33
  • 3
    @Hypomania about your rant (which I rolled-back) a grand total of 6 people downvoted your question. It seems a bit harsh to judge a community of about 9.000.000 people based on that. – Federico klez Culloca Mar 11 '19 at 16:37
  • 3
    Questions like this attract a lot of negative attention because they show a lack of understanding of the fundamentals. Undefined behaviour in C++ is a specific thing, not a miasma that sort of pervades it where it can show up without warning. The `if` statement is no source of great mystery, nor is how boolean or integer values work in logical expressions. An example of undefined behaviour is accessing a pointer after it's been freed, or accessing an element outside of array bounds, in other words, things you shouldn't do in the first place. – tadman Mar 11 '19 at 16:39
  • 6
    There's absolutely nothing wrong with learning or having a misunderstanding of the fundamentals, that's to be expected when picking up a new programming language. The trouble is when asking on Stack Overflow we can't really explain all of that to you, there's [entire books written on C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) that do the job much better, so the best plan here is to get a handle on that first, and then your questions will be better received because we can actually help and give a technical answer beyond "No". – tadman Mar 11 '19 at 16:41
  • 3
    if you want to explore undefined behaviour, change the first lines to `uint8_t x; bool y; bool z;`. Using uninitialized variables is undefined behaviour. – 463035818_is_not_an_ai Mar 11 '19 at 16:48
  • 1
    @tadman I probably didn't express myself the way I wanted to, I didn't mean undefined behaviour in a sense that C++ describes it, I meant random, but nevermind, I will let this be my lesson. – Hypomania Mar 12 '19 at 08:17
  • If you're a little wobbly on [boolean logic](https://en.wikipedia.org/wiki/Boolean_algebra), which is nothing to be ashamed of as it can be subtly tricky, I'd suggest a primer on that, too. That'll help understand how logical expressions can be rewritten or reworked and still preserve the same behaviour. – tadman Mar 12 '19 at 16:41
  • 1
    @tadman, thank you, really appreciated! – Hypomania Mar 15 '19 at 13:27

3 Answers3

5

will I get undefined behaviour by not including all 3 variables into every condition?

The behaviour of not including all variables into every condition is not undefined by itself.

Will every unaccounted for condition go into the else statement?

Statement-false (i.e. the statement after the keyword else) is executed if the condition is false.

what happens if I get rid of the else statement?

The execution continues from the statement after the if statement.

eerorika
  • 232,697
  • 12
  • 197
  • 326
2

As a rule of thumb yes, it's a bad idea to attempt to read a variable that has not been initialised as quite often the behaviour on doing that is undefined. But that's not the case here: all your variables are initialised.

But all you need in an if(...) condition is something that evaluates to either true or false. On that point your code is absolutely fine. I'd use && and || rather than and and or though as the former are more common.

For a pretty comprehensive set of constructs that are undefined, see What are all the common undefined behaviours that a C++ programmer should know about?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

No, you don't have undefined behaviour here. What it sounds like is that you don't have an accurate mental model of conditional (boolean) logic. else is always an optional part of an if statement.

Exactly one of the { ... } blocks will be executed. Changing the values of x, y or z inside any of them will not cause any others to be executed, the decision is made first.

Caleth
  • 52,200
  • 2
  • 44
  • 75