mercredi 31 mars 2021

Is there a benefit to using a boolean over say, an int with an 'if' statement

As a begineer coder, I wrote a program, something along the lines of:

ArbitraryVariable = 1
--Code--
--Code--
--Code--
If ArbitraryVariable == 1:
    --Code--
    --Code--
    --Code--

I would set ArbitraryVariable to 1 if I wanted it to do that thing, and anything else (usually 0) if I didn't, which i guess accomplishes a de facto true/false logic.

My friend who actually knows what he's doing informs me this would've been better accomplished with a boolean as it's true/false, which makes sense, but I am curious if there is actually any specific benefit to doing this, other than a very slight increase in optimisation and it seemingly just being the boolean's 'thing'?

I wrote this in python, but feel free to answer across languages.

Aucun commentaire:

Enregistrer un commentaire