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