A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

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...)

  • This Question is Closed
  1. Owlfred
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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!

  2. rsmith6559
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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".

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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...

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  5. shadowfiend
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    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.

  6. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  7. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.