How do I avoid having a chain of nested if and else if?

How do I avoid having a chain of nested if and else if?

I'm new to coding and I'm working on a project and found myself falling into

If
Code
If
Code
Elseif
Code

There has to be something better that I'm not aware of right ?

I just don't want to be like that yandere dev

case

/thread
also, most times you can reduce the level of nesting ifs just coding in another way.

Does not work in all languages. Python does not have switch statements (worst thing about the language), but there you can use a dictionary of functions.

yo estoy trabajando en lua tambien

>How do I avoid having a chain of nested if and else if?
You don't. Why would you even want that?

functions, hundreds of them

each one simple enough to debug, but with a specific task assigned to it.

then code like this:

try:
foo1()
except e:
foo2()
finally:
foo3()

in foo1() you can do an if-else, or try if else except , etc.

break everything into simple functions that call each other and which you can check/debug easily.

You could replace:
if x < y:
z = True
else:
z = False


With:
z = False

if x < y:
z = True


But the first option is generally considered better practice and neater imo.

If you're dealing with lots of elseif blocks, you'll just want to use a switch statement.

if (condition&&condition) { do; }

Why are a bunch of ifs even bad? It's like goto where if someone sees one instance of it ever they have a heart attack

Could you set z equal to the result of x < y directly?

Yep.
z = x < y


You could also do something like this, which is called a ternary operation.
z = "a" if x < y else "b"

Can and should.

>implying that isn't the same sort of thing

Switch statements are worse actually.

depends on the problem really. sometimes it can't be avoided due to the nature of the problem.

other times you may not understand the problem thoroughly enough to reduce the conditionals to an absolute minimum.

and yet other times restructuring the order of your code can change the logic used.

Write a truth table of the conditions. You'll often find that it's easier to check the negative case vs all the positive ones. Sometimes you'll even realize some cases are redundant. Also nested if's with no else statements can be reduced to a single conditional with &.

Even still, try to keep the conditional nesting as flat as possible. If it's more than 3 levels deep, you're (probably) doing something wrong.

So
Not nested. But just a lot of ifs. Is ok?

I mean if you have to check for different things and NEED a different response for each thing and aren't using a switch

If
Code
ElseIf
Code
Elseif
Code
Else
Code

>Not nested. But just a lot of ifs. Is ok?
Sure. Why wouldn't it be?

It looks ugly to me

>NEED a different response for each thing
if you just want to return some message, use a hashmap.

Not to me. It expresses the pattern you want to express in the clearest and least ambiguous way.

If your language supports it, then depending on what conditions you check a switch/case statement can sometimes look nicer on the page.

I find that switch statements are almost never more readable than if/else chains. In my code, I never ever use them, and the code is better for it.

YANDERE?