LogFAQs > #877044063

LurkerFAQs, Active DB, Database 1 ( 03.09.2017-09.16.2017 ), DB2, DB3, DB4, DB5, DB6, DB7, DB8, DB9, DB10, DB11, DB12, Clear
Topic List
Page List: 1
TopicAnyone here familiar with C++?
treewojima
04/12/17 9:45:35 AM
#12:


you're free to omit brackets for if and else statements. however, the compiler will only execute the statement immediately following the conditional

if (true) cout << "it's true"
else cout << "it's false"


It's considered somewhat distasteful, especially because it can lead to code that either doesn't behave like you intended or is invalid:

if (true)
cout << "this will execute as part of the conditional";
cout << "this is NOT considered part of the conditional and will always execute";


or

if (true)
doSomething();
doSomethingElse();
else
cout << "this else statement is considered invalid because it doesn't have a matching if"


in general, I always include brackets unless it's a simple one liner
... Copied to Clipboard!
Topic List
Page List: 1