3

I'm learning C++ and I encountered 2 different types of writing a piece of code, and I wanted to know what the difference is.

Is there any difference between:

 if(z==true)
  {
    cout << "Yes";
  }
  else
  {
    cout << "NO";
  }

and:

 if(z==true)
  cout << "YES";
 else
  cout << "NO";
3
  • 2
    there is one more piece of code having no difference to those you presented if you're interested. It's `if (z) cout << "YES"; else cout << "NO"; You should not compare bools to true or false, they're already bool. Jul 24, 2011 at 19:35
  • 3
    Also, you can (and should) omit the ==true part. See the answers on Make a big deal out of == true? for various takes on why it's bad.
    – user395760
    Jul 24, 2011 at 19:35
  • What book or instructor are you learning C++ from that didn't explain this to you? Jul 24, 2011 at 19:47

4 Answers 4

8

Technically no, but one is IMO better practice. If you omit the brace only the next line is executed, as opposed to executing everything within the brace. You can see how a quick change of code could raise problems.

Say you have

if(z==true)
  cout << "YES";
else
  x = 47;

and you modify it to

else
  x = 47;
  y = 99;

y = 99; is executed unconditionally. It's best to avoid these gotchas and just use the braces.

1
  • 2
    Have also seen problems when the statement not in braces is a macro: if it becomes a multi-line macro at some later point, the if statement suddenly behaves differently (and you lose a day of your life debugging!). In short: always use braces. Jul 24, 2011 at 21:11
6

No, there is no difference between those two. The {} are only needed if there are multiple statements, but some people argue that always having them reduces the risk for strange bugs if more statements are added later.

2
  • So its just that you can write it however you want in one of these 2 ways.. nithing more :) ? And thank you :)
    – ibanezz
    Jul 24, 2011 at 19:23
  • You can use whichever you want of those. In programming there are often several ways to do the same thing. Sometimes it is considered bad practice to do in some ways, even though it works, because it has a risk for confusion or subtle errors later when the code is updated. Jul 24, 2011 at 19:25
2

This code behaves in the same way. So this is only a stylistic issue.

Many programmers prefer the first since it's more robust. It's easy to change the second one in a way that doesn't work as expected. The {} are necessary if there are multiple statements(or no statement). So changes to the second one can easily result in code where one thinks that a part is conditional/unconditional where it's not. Especially when being sloppy with indentation.

Personally I think if you have a good IDE that can auto format code the risk is small.

2

There is no difference at all, the first example has a block of code instead of a single statement, its just a block with a single statement in it. The former is generally considered safer to write.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.