Ask your own question, for FREE!
Computer Science 17 Online
OpenStudy (anonymous):

Hi! I am trying to work out efficiency in a program by adding a variable to each command which counts it. Does anyone know whether a command such as: while (x%2 == 0 or x%3 == 0 or x%5 ==): would count as 1 command or 3? (The language is Python but I imagine common semantics in all...)

OpenStudy (owlfred):

Hoot! You just asked your first question! Hang tight while I find people to answer it for you. You can thank people who give you good answers by clicking the 'Good Answer' button on the right!

OpenStudy (rsmith6559):

That is a compound conditional. Although there are 3 evaluations, if would be one statement. Generally, efficiency in programming is done with "Big O notation".

OpenStudy (anonymous):

I have no idea about big o notation, I'm still quite new. Just wanted to give it a go, as I just learnt about efficiency at all...

OpenStudy (anonymous):

So, each evaluation would add, so I should consider it 3?

OpenStudy (shadowfiend):

It really depends heh. Typically the way conditionals like this are implemented, it could be 1 to 3 operations. Traditionally, conditional logic `short circuits'. What that means is that if, above, x % 2 != 0, then the loop will quit without evaluating the other two conditions. This is also true in an if statement. If x % 2 == 0 but x % 3 != 0, then two evaluations will occur. It's fair to count it as 3 if you just want to count how many operations are happening, of course. Particularly in the body of the loop, if the loop has run, all three of the conditions have been checked. If the loop ends, one or two of the conditions may have been skipped.

OpenStudy (anonymous):

Ah, yeah, thanks, the short circuit thing is good for efficiency but makes this evaluation more tricky :) I see... Thanks!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!