c++ - Optimized code for two string compare in if condition -
i want 2 string compare , used 2 different if condition. there better way string compare in 1 if condition
if (strcmp(buff1(), config1) == 0) { if (strcmp(buff2, config2) == 0) { // code goes here } }
the equivalent code is:
if ((strcmp(buff1(), config1) == 0)) && (strcmp(buff2, config2) == 0)) { // code goes here }
note: compiler should generate same machine code both code samples. difference cosmetic , aimed @ reader of code.
you difference when add else
clauses:
if (strcmp(buff1(), config1) == 0) { if (strcmp(buff2, config2) == 0) { // code goes here } else { // else 1 } } else { // else 2 }
compared to:
if ((strcmp(buff1(), config1) == 0)) && (strcmp(buff2, config2) == 0)) { // code goes here } else { // single else clause }
Comments
Post a Comment