Click here to Skip to main content
15,887,267 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: if else Style Pin
jschell26-Jan-24 5:49
jschell26-Jan-24 5:49 
GeneralRe: if else Style Pin
PIEBALDconsult27-Jan-24 7:17
mvePIEBALDconsult27-Jan-24 7:17 
GeneralRe: if else Style Pin
Matt Bond29-Jan-24 3:02
Matt Bond29-Jan-24 3:02 
GeneralRe: if else Style Pin
Maximilien26-Jan-24 1:47
Maximilien26-Jan-24 1:47 
GeneralRe: if else Style Pin
BernardIE531726-Jan-24 2:02
BernardIE531726-Jan-24 2:02 
GeneralRe: if else Style Pin
trønderen26-Jan-24 2:06
trønderen26-Jan-24 2:06 
GeneralRe: if else Style Pin
trønderen26-Jan-24 2:26
trønderen26-Jan-24 2:26 
GeneralRe: if else Style Pin
Daniel Pfeffer26-Jan-24 4:09
professionalDaniel Pfeffer26-Jan-24 4:09 
As others have said, it depends on the context.

I prefer a ternary operator if I must choose between returning one of two results:
C++
Foo foo = (condition ? Foo1 : Foo2);

For choosing different actions, I prefer the if/then/else style, even if it would be possible to write this as a ternary expression:
C++
if (condition)
{
   expressions1;
}
else
{
   expressions2;
}

A "cascading" if/then/else is useful for subordinate cases:
C++
if (condition1)
{
   expressions1;
}
else
{
   if (condition2)
   {
      expressions2;
   }
   else
   {
      expressions3;
   }
}

Note that there is angoing debate whether good style requires that one wrap even single expressions in curly brackets. ( {...} }. If is usually not required by the language, but can help clarify the flow.

Note that there are cases where a "cascading ternary operator" can also be useful, and actually clearer than the "cascading if/then/else".
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.

GeneralRe: if else Style Pin
Matt Bond29-Jan-24 3:06
Matt Bond29-Jan-24 3:06 
GeneralRe: if else Style Pin
Greg Utas26-Jan-24 4:42
professionalGreg Utas26-Jan-24 4:42 
GeneralRe: if else Style Pin
0x01AA26-Jan-24 4:55
mve0x01AA26-Jan-24 4:55 
GeneralRe: if else Style Pin
Greg Utas26-Jan-24 5:25
professionalGreg Utas26-Jan-24 5:25 
GeneralRe: if else Style Pin
honey the codewitch26-Jan-24 8:11
mvahoney the codewitch26-Jan-24 8:11 
GeneralRe: if else Style Pin
Mircea Neacsu26-Jan-24 6:02
Mircea Neacsu26-Jan-24 6:02 
GeneralRe: if else Style Pin
honey the codewitch26-Jan-24 7:21
mvahoney the codewitch26-Jan-24 7:21 
GeneralRe: if else Style Pin
0x01AA26-Jan-24 8:09
mve0x01AA26-Jan-24 8:09 
GeneralRe: if else Style Pin
trønderen26-Jan-24 8:29
trønderen26-Jan-24 8:29 
GeneralRe: if else Style Pin
0x01AA26-Jan-24 9:32
mve0x01AA26-Jan-24 9:32 
GeneralRe: if else Style Pin
Gary R. Wheeler26-Jan-24 13:12
Gary R. Wheeler26-Jan-24 13:12 
GeneralRe: if else Style Pin
BernardIE531726-Jan-24 13:34
BernardIE531726-Jan-24 13:34 
AnswerRe: if else Style Pin
Ravi Bhavnani26-Jan-24 20:53
professionalRavi Bhavnani26-Jan-24 20:53 
GeneralRe: if else Style Pin
trønderen27-Jan-24 9:05
trønderen27-Jan-24 9:05 
GeneralRe: if else Style Pin
BernardIE531727-Jan-24 10:24
BernardIE531727-Jan-24 10:24 
GeneralRe: if else Style Pin
trønderen27-Jan-24 11:44
trønderen27-Jan-24 11:44 
GeneralRe: if else Style Pin
BernardIE531727-Jan-24 12:16
BernardIE531727-Jan-24 12:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.