I've written a little for loop, which is going to iterate over all of the elements in the list.
让我们先看看这儿的代码,我已经写了一个循环语句,用来迭代处理数组中所有的元素。
And so the guys in Office Space took that money and they-- -- their for loop-- way too fast is-- was the takeaway in that movie, when they freaked out because they had million dollars or something like that overnight.
所以在办公地点里的家伙才能够搞到钱-,然后利滚利--很快的方法-,这就是整个影片的亮点,当他们出于极度兴奋之中,因为他们已经一夜之间,变成百万富翁了。
And that's very nice, because this allows me to be sure that my FOR loop is going to terminate.
因为这让我确信我的FOR循环是会终止的,因为只要集合是有限的。
Well in C and other languages that we'll see this semester, you use what's called the for loop, and this as syntax isn't quite as straightforward but once you know what to look for, it's very easy to understand.
在C语言和一些我们这学期将要接触的编程语言中,你将会用到所谓的for循环,也许作为一种语法规则,它看起来不是那么的通俗易懂,但是你一旦知道曲中奥妙,就不难理解了。
is run a FOR loop here. I'm going to let the number of chickens be in this range.
我知道怎么做对吧?写一个小循环,我所需要做的就是运行一个for循环。
Because the for loop hands me placeholders in between those semicolons for variables and such, now I don't have those with while loops.
因为for循环给我提供了为变量准备的占位符,它们在分号之间,现在在while循环中,我没有这些。
But notice unlike a for loop, if you want to declare a variable or increment or decrement to variable, you've got to do that yourself.
但请注意不像for循环,如果你想声明一个变量,或者变大或变小一个变量,你需要自己完成。
At the end of the day, you can implement the exact same program using a while loop that you can using a for loop, it's just your code is going to look slightly different.
最终,你可以执行这个程序,再使用for循环的地方使用while循环,这只是使你的代码看起来稍微有点不同。
Now instinctively, prefer the for loop or the while loop?
现在凭感觉,你们喜欢for循环还是while循环?
I can write it explicitly. I can write it inside of a FOR loop. And we've started to add, and we'll see a lot more of this, examples of collections of structures so that we don't just have to do something that can be easily described as walking through a set of things but can actually be a collection that you walk through.
我可以明确的写出来,我可以把它在FOR循环的内部写出来,然后我们开始学习了add这个运算符,我们会在以后学习很多的,集合的数据结构的例子,这样一来我们可以进行一些,很简单的遍历集合的操作,我想要为大家指出的最后一件事情是。
You don't get the nice little feature of a for loop.
你没有理解for循环的美好的小特征。
And if I were to use that again, I'd just put it on your handout, I could go back and rewrite that thing that I had previously for finding the square roots of the perfect squares, just using the FOR loop. OK. What I want to do, though, is go on to-- or, sorry, go back to - my divisor example.
它可以是任意的集合,如果我又要去用这个方法的话,我会把它放在你们的课堂手册上的,我可以回过头去用FOR循环,重新写我们那个求平方数的程序,我想要做的是,是继续-哦抱歉,回到-我的除数那个例子。
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循环。
So here is a for loop.
这是用for循环语句。
So the question was, why is this return down at this level, it says, well if I ever execute out of this FOR loop, I get to the end of the FOR loop without hitting that branch that took me through the return, then and only then do I want to actually say, gee, I got to this place, there isn't any value to return, none I'm going to return none and none.
问题是,为什么它是在这个级别上返回值呢,意思就是如果我出了这个for循环,就意味着我运行的过程中没有走到任何,带return的支路上去,这时也只有这时我才能说天呐,我终于走到这儿了,这时不会返回任何值,只会返回none和。
If it isn't, then I'm going to go back around the loop, and notice what happens. If I get all the way around the loop, that is, all the way through that FOR loop and I never find a path that takes me through here, then the last thing I'm going to do is return a pair or a tuple with a special simple number none twice.
如果没有,我会回到循环看看发生了什么,如果我运行了整个循环,也就是整个for循环,我仍没有找到符合条件的路径,那么我最后做的事情就是返回一对,或者一个数组的两次none值。
And you'll find in the end that recursion is a feature of a language, it allows you to map some very obvious concepts like the phonebook tearing in half and half and half and half to actual code without it using some arbitrary human contrived framework like a for loop or a while loop which are much more stilted mechanisms.
最后你会发现递归是某种语言的一种特征,你可以用它将一些非常明显的思想,比如将电话簿划分为一半又一半,映射为实际代码,而不必使用那些人为框架,像for循环,while循环之类的,那是很呆板的机制。
And we won't wait here one hundred seconds for it to finish, but we're using the loop, we're updating a variable, and we're formatting it in a nice way.
我们将不会再这里等待100秒来等它完成,但是我们使用循环,我们更新一个变量,我们把它排成一个漂亮的格式。
Then the next thing in the loop, we're going to enter a nested loop and say for every college in the list e, we're going to print the name of the college.
循环中的第二件事,是进入一个嵌套循环,这个嵌套循环将数组中的,大学名字显示出来。
Sorry? OK. You're doing the whole thing for me What's the complexity just of this inner loop m How many times do I go through that loop?
就这一块代码的,我执行了多少次这个循环?,次对不对?,here?,Just,this,piece。,我一会儿就会去得到你的答案的?
And so when you start to poke around the puzzle pieces, you see that it can say things, we'll see movement in just a moment and sound and so forth, but at the end of the day, this is just like the program we used a moment ago for the sock exercise where we just hold Philip a step after step after step, do this, do this, do that, and yet, here too, we already see an opportunity for that thing we called the loop.
如果你认真看下这些拼图,你会发现它都是一些移动,并发出声音的功能块,就像我刚才的,练习程序一样,我们让飞利浦做这个动作,做那个动作,跟这里的情况一样,我们已经看到了称为循环的东西。
Otherwise, it's a pretty bad mistake, but sometimes you do want an infinite loop, Word for instance, Microsoft Word if you use it or pages or the like on your Mac or PC.
否则,它将会是一个错误,但有时你确实需要一个无穷循环,例如微软的,当你在Mac或者PC上使用它时。
Yes. The point is, it's not going to work, and she caught both of them, impressive, it's not going to work because iters left is already negative, it's just going to skip the whole loop, and I'm in trouble, so thank you for catching that.
是的,重点是,程序不会运行的,她发现了两个错误,很令人敬佩,程序不会运行,因为iters,left已经是负数了,它会跳过整个循环,然后就有麻烦了,谢谢你提出这个问题。
I'm again, iterating with this four loop, same code as before.
我再次,重复使用for循环,同样的代码。
And then we looked at this little loop before, for i in range number of stocks, I'm going to create two different lists of stocks, one where the moves, or distributions, are chosen from a uniform, and the other where they're Gaussian.
这里的这个小循环,因为i代表股票,我会建立两个不同的股票链表,一个是代表股票价格的移动,或者说是分布,它们是从均匀分布中得出的,而另一个链表是从高斯分布中得出的。
应用推荐