这是完全正确的。(但请注意,每个操作都需要 O(1) 时间。例如,如果操作是函数调用,则还必须考虑其运行时复杂度)
Next time, you might find a "critical" operation and count only the number of times that operation executes. For example, in your example, it is obvious that there will be a constant number of times comparison and increment operations for each assignment operation executed, so you would be fine if you only counted the number of assignments.
It is always easy when you directly count the number of executions of operations. More difficult cases arise when you cannot directly count but for example arrive at a recursive formula, i.e. T(n+1) = a T(n/b) + f(n) etc.