So in those quizzes hopefully you got a sense of just how many

variables there are for

deciding how many operations get executed when we run a method on particular inputs.

And so let's recap what you saw in those quizzes and let's think about what happens

when we search for the letter x, still in that same word San Diego.

So we're still looking at the hasLetter method and

now we're thinking about just looking for a different character.

So when we think about counting operations, I know that I think about

groaning because I don't wanna do that same line by line counting each one again.

That was kind of painful but I'd like to find a shortcut.

So let's think instead of grouping a whole bunch of instructions,

a whole bunch of operations into maybe something higher level, that then I can

think about how that higher level construct repeats over and over.

So let's think about what happens as we go through a single iteration of the for

loop, what that body of the for loop is going to contribute to my operation count.

And so, in a single iteration, what I need to do is,

well I need to test whether my current loop counter is less than the bound.

I need to, in that case, perform the contents of the body of the for loop.

And then I'm going to need to increment my loop counter.

Okay, so each of those contributes one instruction,

in this particular case, to the operation count all in all.

And this is, of course, assuming that in this current iteration of the for

loop I haven't found the character that I'm looking for,

because that would kick us out of the for loop.

And so let's just think about what's happening in an iteration, somewhere in

the middle of my algorithm, somewhere in the middle of the execution of the code.

And so in that case, each iteration takes 3 operations.

And so if we think back to looking for 'a',

then we only really did 2 iterations of the for loop.

We went through an iteration when our loop counter was at the 0, so

we were comparing a to capital S, and

then we had an iteration with our loop counter at position 1.

And so then we were comparing a to a, and then we broke out of the for loop and

were happy.

Okay, so, when we had just the 2 iterations, we got 7 operations.

And so now let's think about the how many iterations of the loop are there,

when we're looking for the letter 'x'.

Okay, so thinking back to the algorithm,

we're going from the beginning to the very end of the word San Diego.

And so we're going to be looking through every single position of the string, and

that means that there's going to be an iteration of the for

loop for each of the positions in the string.

And there are 9 such positions because this is a length 9 word.

Okay, now we can go ahead and do the calculations for

the number of operations based on the 9 iterations.

We see that there are going to be 29 operations.

And now you might stop and say, whoa, hold on,

I know my arithmetic, 9 times 3 is not 29, it's 27!

Think about what happens at the very beginning, when we initialized

the variable i, and at the very end, when we break out of the loop.

And so, there's going to be some niggly little counts at the very beginning and

the end that aren't exactly the same,

it's just doing 3 times the number of iterations.