好了 开始讲while循环
All ready while loops.
实际上大部分编程语言中都有两种主要的循环函数
There are actually two major loop functions within most coding languages.
那就是while循环和for循环
That’s the while loop and for loop.
这两者之间有一些根本性的差别
There are some fundamental differences between the two of them.
但是他们基本上都能实现对方能实现的功能
But they both can do pretty much everything each other can do.
所以有时候只是个人偏好问题
So it’s sometimes it’s just kind of preference.
while循环做的事是 当满足一个条件时 持续循环
The while loop what it’s doing is it continues a loop while a condition is something.
一旦条件不满足 就继续执行下面的代码
As soon as that condition is not something anymore, it will continue on with a code.
很多时候 在while循环的内部 要么引用另一个函数
Many times within the actual while loop it’s either going to reference another function
用来检查确认条件是否满足
that checks to make sure that that condition is as such
要么则是包含某种计数器
or it contains some sort of counter
或是某种随时间改变的变量
or something that changes the variable over time.
做任何一种循环基本都是相同的 有点像定义一个函数 然后把代码放进去
To do any of the loops they’re all bascially the same, kind of like defining a function and putting the code in there.
做起来很简单 首先我们要定义变量
It’s quite simple you do. First we’re going to define the variable
并且设定条件 我们假定x等于1
so the condition that we’re going to talk about we’ll say x equals 1.
这就是我们的条件 现在来做一个基于这个条件的while循环
So now we’ve got the condition, now let’s make a while loop that depends on this condition.
所以你可以说当x小于5
So you can say while x is less than 5
记得在这里加一个冒号
and you always put a colon there.
回车 然后你可以看到 它已经自动缩进了
Enter and as you can see it already automaticlly indented for us.
如果你不在这里加冒号 然后回车 你会看到它不会自动缩进
Now if you don’t put the colon here and you do this, you’ll see it did not already indent.
这种方式可以比较快地发现你是否做错
That’s kind of quick way to figure out what you have done wrong.
Python在这方面做得非常棒
Python is really nice about doing this kind of stuff.
很多其他编程语言都需要你输入一些圆括号、方括号之类的
There’re many other languages you’re going to need some sort of parenthese brackets whatever.
在这里你不需要做那些 它已经给你设计了一个很好的格式
This way you don’t have to actually do that and it gives you a good form already.
让我们继续 当x小于5 我们就要打印
So anyways continue on, while x is less than 5, we are going to print out
代码“x is less than 5”(x小于5)
this will be our code “x is less than 5”.
然后 像我之前说过的
And then we are going to do what I was talking about earlier
我们要么是引用一个函数来决定x
we either have to reference the function that determines what x is
要么就在这个循环里改变变量x
or we can change the variable x within here.
我们必须改变这个条件
Something has to be changing
否则就会导致无限循环
this condition otherwise you’ll result in an infinite loop
或许你的目的是产生一个无限循环
which may be your…that your goals to have an infinite loop.
但是一般来讲无限循环都不是目的 那么接下来我们需要
But generally that’s not the goal. So then we’re going to do x
“x+=1” 意思是把1加到x上
plus equals 1, now what this means is you are just adding 1 to x.
“+=1″基本上等同于
Plus equals 1 equals is bascially the exact same thing
“x=x+1”
is x equals x plus 1.
这只是一种简写 你会在很多编程语言里看到它
This is just the shorter way to do it. You will find this in a lot of different coding languages.
保存代码 然后运行
So anyways we’ll save that, we’ll run it.
你可以看到 我们从1开始 x小于5 加1然后一直循环直到最后等于5
And as you can see we started with one, x is less than 5, add 1 and keep going until finally reach 5.
就不会再打印“x is less than 5”(x小于5)
And it would not print out “x is less than 5”.
所以很明显 这个循环运行了4次
So well obviously run this 4 times.
就像我说的 如果你真的真的真的很想要一个无限循环
Now like I was saying, if you really really really… want to have an infinite loop,
你可以 …… 你可以要么去掉这句
you could do something like you can either say you can get rid of x this here
要么说x …… 当1小于2的时候
or you could just say x…uh, you know while 1 is less than 2.
打印“x is less than 5”(x小于5)
You can print “x is less than 5”.
然后你可以看到 现在我们让自己陷入了一个无限循环 它会一直运行下去
And as you can see, now we just engaged ourselves in an infinite loop and it just keeps running forever.
如果它停下来了 那你整个程序都有大麻烦了
If this stops, you got a major problem going on with universe.
