mardi 22 août 2017

"switch" or "else-if without curly braces" [duplicate]

I want to use one of the following versions of my code.

  • I am wondering if I can leave the curly braces in if-else-if.
  • Are they faster than switch?
  • Are the versions the same (semantically)?

Version 1:

switch (myenum) {
    case TEST:
        test();
        break;
    case PRODUCTION:
        production();
        break;
    default:
        break;
}

Version 2:

// I omit the curly braces
if (myenum == MyEnum.TEST)
    test();
else if (myenum == MyEnum.PRODUCTION)
    production();

Version 3:

// With curly braces
if (myenum == MyEnum.TEST) {
    test();
} else if (myenum == MyEnum.PRODUCTION) {
    production();
}

Aucun commentaire:

Enregistrer un commentaire