Is it necessary to write else part in every if condition?

后端 未结 11 1614
-上瘾入骨i
-上瘾入骨i 2021-02-19 05:16

The question I asked might be closed, But i just want to know that is it necessary to write else part of every if condition. One of my senior programmer said me that \"you shoul

11条回答
  •  深忆病人
    2021-02-19 05:32

    No, but I personally choose to always include encapsulating braces to avoid

    if (someCondition)
        bar();
        notbar();  //won't be run conditionally, though it looks like it might
    
    foo();
    

    I'd write

     if (someCondition){
            bar();
            notbar();  //will be run
     }
     foo();
    

提交回复
热议问题