Once each time. Right? I only do one swap potentially, it-- though not one potentially, each time at the end of the loop I do a swap.
每次循环做一次,对么?我其实,就可能做了一次交换,也许并不是可能,每次循环的结束我都得做一次交换。
And I want to remind you, that block of code is a set of instructions, the same set of instructions that are going to be done each time through the loop.
我想提醒大家,这块代码是一个指令集,一个每次循环都会被执行的相同指令集,会变化的仅仅是变量的值。
Oh, there's a man at the store and now we have a loop where he is just looking through my inventory one at a time and reciting what it is I actually picked up.
商店里有一个人,现在我们有一个循环,通过这个循环他可以看到我的库存,然后显示出来我实际上都采摘了哪些水果。
The loop invariant what does the loop invariant mean? It says, here is a property that is true of this structure every time through the loop.
循环不变量是什么意思?,它是一个在结构体中,每次循环都为真的属性。
Which means, next time through the loop, that's going to pop out and return an answer.
循环会跳出并返回这个答案,如果不相等的话。
Well, you'll know when it crashes, that doesn't help you very much, but you can't always tell whether something's stuck in an infinite loop or whether it's simply taking a long time to compute.
好,你将学到它在什么时候会崩溃,这对你帮助并不大,但是你不能辨别出到底是在一个循环中,有什么东西卡住了呢,还是程序需要很长的时间来计算一个结果呢。
So each time through the loop, I'm doing three steps. Three basic operations.
因此每次运行这个循环,我做了三次基本运算。
It's basically cycling through the loop a multiplying by a each time.
这个代码就是简单的循环,每次乘一个。
So first time through the loop, the answer is a. Second time it-sorry, as it enters the loop, at the time it enter exits a the answer is a.
当第一次完成循环的时候,答案是a,第二次,对不起,在第一次进入循环的时候,答案就是。
And then I run a loop in which I read something in, I check to see if it's the right type, if it is, I change that variable to say it's now the correct type, which means the next time through the loop, I'm going to say I'm all set and I'm going to bounce out.
因为我还没输入,然后我运行一个循环,循环内部我输入一些东西,然后看看是不是正确的类型,如果是的话我改变input,Ok为真,来证明输入数是正确的类型,这就意味着下一次运行这个循环的时候,就可以跳出循环继续执行了。
Next time through the loop it goes to a cubed.
然后再一次循环变成a的三次方。
b times. So I've got to go 3 b steps. All right I've got to go through the loop b times I've got three steps each time, and then when I pop out of the loop, I've got two more steps. All right I've got the initiation of answer and the return of it.
好,我一共执行了b次循环,每次循环内部进行了三次基本操作,当跳出循环后还有两个基本操作,因此这个初始的答案就是,2+3b次基本操作,来完成这个循环。
Next time through the loop it goes to a squared.
循环完后变成a的平方。
That is basically saying, given some collection of data, I want to have again a looping mechanism, where now my process is, walk through this, the collection one element at a time. And for that, we have a particular construct, called a FOR loop.
现在我的过程就是,通过一个个访问这个集合中,的元素的方式,来遍历这个集合,为了达到这个目的,我们有一个特定的结构,成为FOR循环。
应用推荐