i am trying find the Cost of an algorithm. it contains break statements. is there any cost of
break and Continue statement in a Algorithm?
Depends on how close you want to look.
On face value,
continue, etc. are implemented by unconditional jumps, which are (under most RAM-/CPU-like models) primitive instructions of the machine. So they certainly have some constant cost; $O(1)$ is a valid bound.
That's not very interesting, though: all primitive instructions have constant cost. If you want to answer questions like, "can I speed up my algorithm by adding/removing
continue?", the situation becomes more involved.
The true cost of jumps depends on many factors, e.g.
- the type of pipeline the CPU uses,
- which type of speculative execution happens (if any),
and so on. My guess is that an unconditional jump -- other than conditional jumps -- should behave well in many regards. They are most certainly cheaper than completing the loop would be.
If the true cost matters to you, I recommend you benchmark the alternatives at hand. Be aware that the effect you're trying to measure may be smaller than the noise introduced by benchmarking, and you may have to account for that.