I've written a little for loop, which is going to iterate over all of the elements in the list.
让我们先看看这儿的代码,我已经写了一个循环语句,用来迭代处理数组中所有的元素。
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.
每次循环做一次,对么?我其实,就可能做了一次交换,也许并不是可能,每次循环的结束我都得做一次交换。
Now, all the cat is doing is that it's an infinite loop and it's checking if I am not facing bird, - face bird, and it's -- It's constantly chasing the bird.
猫能做的就是一个死循环,并且它能检测我是不是面对着一只鸟,如果没有就转向那只鸟-,它一直不停地追逐这只鸟。
Well I ought to be able to do that by simply generalizing the loop. And that's what the next piece of code on your a hand out shows you.
好的通过生成这个循环,我应该能够达到目的,这也是你们的讲义中下一段代码。
Well, let's take a look at this high7 variant that also use loops that also uses a loop. So this one is a little cute, I like to think. So notice this.
让我们看下这个仍然使用了循环的版本,这个有点意思,我认为,注意这个。
printf So here I'm culling printf in a loop again and again, but I'm literally overwriting the previous thing that I wrote.
所以这里我一次次地在循环中使用,但是我逐行地覆盖了我之前所写的东西。
And what's this little loop say to do? This little loop says I'm going to write a function or procedures that takes in two messages.
这个小循环是做什么的,这个小的循环是,我要写一个函数或者过程来接受两条消息。
If I want a copy of the string, I've got to whip out my week 1 skills of just iterating with a four loop from left to right and make copies of those characters.
我需要那个字符串的一个拷贝,我使用,第一周的技巧,从左到右做四次迭代,然后复制这4个字符。
So what I wanted to do is loop back, at least with a couple of things that we see recurring and very understandably so, but let's try to clear up a couple of frequently occurring issues.
所以我想要做的是回环,至少用两三个,我们看到的东西返回,并且可以理解地,让我们试着处理几个,频繁发生的事情。
Once you know what pieces to look for, what mental constructs to reach for, can you realize all right I want to make some dancing figures go back and forth, I need to loop this way and loop this way and check if I'm on the edge and so forth.
一旦确定需要查找的部分,想要达到那种层次,你是否意识到,我想做出一些可以来回走动跳舞的人,我需要按这种方式那种方式让它转圈,并检查我是否,到了屏幕的边缘等等。
I'm going to run through a loop, and I'm going to request some input, which I'm going to read in with raw input.
我要运行一个循环,我需要一些输入,需要读入一些原始的输入。
Nonetheless, having done that, let's look at what I'm going to do. I now want to run a loop where I need to collect things together.
尽管如此,讲完了这些,让我们去看看,我将要进行的操作,我想要运行一个,需要把东西收集起来的循环,我要命名它。
I'm going to build a list. Right, you can see that little loop going through there.
我要创建一个列表,是的,你可以看到这里有一个小小的循环。
I can be a little more careful about keeping track of what goes on inside of that loop. If I don't have any more work to do, let me just stop.
我能更小心点来跟,踪循环里发生了什么,如果没什么可干的了,就停下来吧。
Otherwise the test is always going to be true, we're going to go off here, but this would loop forever if I did that.
否则测试结果就会一直为真,我们需要离开这个测试,否则就会一直执行循环。
Now I have the ability to create a loop, that is, to reuse the same pieces of code over and over again as I go around.
现在我们已经学会如何创建循环了,也就是,重复的一遍又一遍的,执行同一片代码。
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.
商店里有一个人,现在我们有一个循环,通过这个循环他可以看到我的库存,然后显示出来我实际上都采摘了哪些水果。
swap I'm going to keep track of a little variable called swap, it's initially true, and as long as it's true, I'm going to keep going, but inside of the loop I'm going to set it to false, and only if I do a swap will I set it to true.
我打算跟踪一个小的变量称为,初始值为真,只要它是真的,算法就会继续,但是在循环里,我打算把他设为假,除非交换发生,就把它设为真。
I'm going to have it take in as input how many legs I got, how many heads do I have, and I just want to write a little loop.
这就是解决问题的思路,我将要输入我有多少条腿,以及我有多少只头。
At the end of the first round, I've got the smallest element at the front. At the end of the second round, I've got the smallest two elements at the front, in fact I got all of them sorted out. And it actually runs through the loop multiple times, making sure that it's in the right form.
看看发生了什么,在第一轮结束后,我把最小的元素移到了前面,第二轮结束后,我把最小的,两个元素移到了前面,实际上,所有的元素都排好序了,实际上,这个算法运行了几次循环,确认下这是正确的形式。
So I take 2 plus 3 b steps to go through this loop OK. So if b is 300, it takes 902 steps.
好,如果b是300的话就是902步,如果b是3000那就有9002步。
So each time through the loop, I'm doing three steps. Three basic operations.
因此每次运行这个循环,我做了三次基本运算。
They're inside that loop-I've got the wrong glasses so I'm going to have to squint- and we've got one test which is a comparison we've got another test which is a multiplication-- sorry, not a test, we've got - another step which is a multiplication- and another step that is a subtraction.
他们在循环里面呢,我带错眼镜了所以不得不斜视-,然后我们这里有一个比较的测试,还有一个乘法的测试,-对不起,不是测试,是另外一个乘法的步骤-,还有一个减法的步骤。
You could run through a little loop to say keep trying until you get one. But one of the ways I could deal with it is what's shown here.
你可以运行一个小循环来不断地做,直到得到一个输入,但你应该做的是弄明白这里到底要干什么。
And now I want to run through the loop.
现在我想进入循环了。
And I need to initialize it outside of the loop.
我需要初始化它。
How do I know when I'm done with the loop?
我怎么知道循环完毕了呢?
The inner loop, this part here, I do m times.
这个循环执行了多少次呢?
What's the first thing I do in a loop?
我在循环中做的第一件事情是什么呢?
How many times do I go through the loop?
那我执行了多少次循环呢?
应用推荐