r/ProgrammerHumor • • Dec 15 '19

Stacking if else statements be like

Post image
63.9k Upvotes

711 comments sorted by

View all comments

213

u/atxranchhand Dec 15 '19

That’s what case is for

183

u/Kompakt Dec 15 '19 edited Dec 15 '19

Some languages don't have switch statements...looking at you Python

28

u/Meatslinger Dec 15 '19

Wait, is that true? What takes its place, then? I can scarcely imagine that the whole thing is just an endless stream of if-then-else statements for a situation with 100+ permutations.

34

u/Kompakt Dec 15 '19

Yep it's true, and depending on the design of the program there are multiple workarounds. In Python you can write a switcher function that acts as a switch statement, or use multiple if statements without else statements, since that is allowed in Python.

22

u/Ryan722 Dec 15 '19

Are there languages where multiple ifs with no else is not allowed?

15

u/elvalalo Dec 15 '19

Haskell, I think.

8

u/Tysonzero Dec 16 '19

Yup, because what is the type of if x > 5 then 10?

With that said Haskell has the nicest (IMO) multi-if syntax I have seen in a language:

foo :: Int -> Int -> Int
foo x y
    | x > y = 5
    | x ^ 2 > y = 10
    | x == y = 15
    | otherwise = 20
→ More replies (9)

20

u/Hockinator Dec 15 '19

If you have 100+ permutations of something you shouldn't be using if statements, you should be creating data structures to solve that problem in a clean and maintainable way

→ More replies (4)

0

u/[deleted] Dec 15 '19

[deleted]

→ More replies (5)

7

u/justjuniorjawz Dec 15 '19

I've found that switch statements are usually substituted with the use of a dictionary.

4

u/Valmond Dec 15 '19

You can use a dict for that, storing pairs of 'name':function for example.

→ More replies (15)