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

Viewed 33603

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 should write else part in every if condition" . Suppose we have no condition for write in else part then what should we do ? I assume a healthy discussion will going on here....

12 Answers

No, It's not required to write the else part for the if statement.

In fact most of the developers prefer and recommend to avoid the else block.

that is

Instead of writing

if (number >= 18) {
    let allow_user = true;
} else {
    let allow_user = false;
}

Most of the developers prefer:

let allow_user = false;
if (number >= 18) {
    let allow_user = true;
}
def in_num(num):
    if num % 3 == 0:
        print("fizz")
    if num % 5 == 0:
        print("buzz")
    if (num % 3 !=0) and (num % 5 !=0):
        print(num)

see in this code else statement is not necessary.

Related