Ask your own question, for FREE!
Computer Science 22 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!
Latest Questions
Countless7Echos: Ah trying out the whole T.V girl drawing :p (I love drawing eyes)
8 minutes ago 1 Reply 1 Medal
kaelynw: starting to draw a hand
1 day ago 16 Replies 2 Medals
Twaylor: Rate it :D (Took 2 days)
2 days ago 7 Replies 0 Medals
XShawtyX: Art, Short Writing Assignment: Imagining Landscapes
2 hours ago 6 Replies 1 Medal
XShawtyX: Chemistry, Help ud83dude4fud83cudffe
2 days ago 13 Replies 1 Medal
kaelynw: tried a lil smt, the arm is off but i like the other stuff
3 days ago 27 Replies 3 Medals
kaelynw: art igg
3 days ago 14 Replies 1 Medal
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!