Else statement (if its needed?)

Is it possible to create every function without else statement or in some cases it’s needed?

1 Like

as everything, it depends

can you expand a bit on your doubt?
it is difficult to give an answer without some context

1 Like

It is sometimes needed.

Can you show me examples of when you need to use else statement?

Obviously a contrived example, but an example of the difference an else makes.

if (age < 21) {
    tooYoungToDrink = true;
}
tooYoungToDrink = false;
// you just served a minor!
if (age < 21) {
    tooYoungToDrink = true;
} else {
    tooYoungToDrink = false;
}
// good job
2 Likes

If you want to do something if some condition is true otherwise do a different thing?? If you don’t put something in an else it always runs – I’m sure you can appreciate that you don’t always want to run code, often you only want it to run if some condition is explicitly met/not met (otherwise don’t run it).

1 Like