Shorthand for if-else statement

Using the ternary 😕 operator [spec]. var hasName = (name === ‘true’) ? ‘Y’ :’N’; The ternary operator lets us write shorthand if..else statements exactly like you want. It looks like: (name === ‘true’) – our condition ? – the ternary operator itself ‘Y’ – the result if the condition evaluates to true ‘N’ – … Read more

#ifdef vs #if – which is better/safer as a method for enabling/disabling compilation of particular sections of code?

My initial reaction was #ifdef, of course, but I think #if actually has some significant advantages for this – here’s why: First, you can use DEBUG_ENABLED in preprocessor and compiled tests. Example – Often, I want longer timeouts when debug is enabled, so using #if, I can write this DoSomethingSlowWithTimeout(DEBUG_ENABLED? 5000 : 1000); … instead … Read more

If-else working, switch not [duplicate]

You need to break; after each statement in a case, otherwise execution flows down (all cases below the one you want will also get called), so you’ll always get the last case. switch(position) { case 0: textView.setText(R.string.zero); break; case 1: textView.setText(R.string.one); break; case 2: textView.setText(R.string.two); break; case 3: textView.setText(R.string.three); break; case 4: textView.setText(R.string.four); break; } … Read more