If i use an if statement with if and elif do I have to use else?

Home » Programming & Design » If i use an if statement with if and elif do I have to use else?
Programming & Design No Comments

in my code i used an if and two elifs, but i do not want to use an else. is that ok do I have to use an else to meet python regulatory standards???????????

idek

Other answer:

dday9:
The conditional statement breaks down into a few rules:
1) The keyword 'if' must always be present
2) The optional keyword 'elif' may be present only after the 'if' keyword
3) The optional keyword 'else' may be present only after the 'if' keyword. If there are any 'elif' statements, then the 'else' can only come after all 'elif' statements.
Dulara:
A good practice is not a must.! but it will save you. save your debugging time. when you are dealing with simple scenarios you may be omit the else: , but when your logic become bigger and bigger it's a good practice and you are making sure that you caught up all the values that the conditional statement can take by using else statement.

Eg: you can write your program like this.

class HelloWorld { public static void main(String[] args){System.out.println("HelloWorld");} }

and also you can write the same program

class HelloWorld {
public static void main(String[] args){
System.out.println("HelloWorld");
}
}

it works in both cases.
but what method do you prefer?

still you may be a kid who learning software engineering, programming. but one day you'll become a genius and working at a top level software company. only if you know how to work with a team. that's why we need good practices.

a simple answer.
Yes You must use ELSE:
because using else is not a must but it is a good software engineering practice.

Hope you got what i wanted to mean.
Have a nice day! 🙂

Just Another Soul:
That's correct. You absolutely don't have to use an else unless it would be useful.
brilliant_moves:
I would write it this way for completeness:
if …
elif …
elif…
else: pass
Chris:
You don't have to use else, no.
ifzal:
http://bit.ly/23pg55t here is your answer

LEAVE A COMMENT