mardi 25 mai 2021

If or for loop? Which is more efficient in this case?

Computer Engineering Student here! I'm currently building a checker game, and in a particular part of the code I have to choose between two methods that result in the same function.

I could either use if statements or a for loop.

In the for loop, I'd have to loop all 64 checker grid to locate the piece that I want, but through an algorithm, I could find that specific piece using an if statement.

But here's the catch, the if statements take up more lines to code while the loop takes only a few lines. Which is more memory efficient method in this case?

My idea is that the if statements are more efficient although they take up more line since the loop has to literally loop 64 times despite taking up very few lines of code.

Am I right? If I am wrong, have at the keyboard Sirs and Ma'ams!

Aucun commentaire:

Enregistrer un commentaire