All ready while loops.
There are actually two major loop functions within most coding languages.
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
so the condition that we’re going to talk about we’ll say x equals 1.
So now we’ve got the condition, now let’s make a while loop that depends on this condition.
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 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
we either have to reference the function that determines what x is
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
plus equals 1, now what this means is you are just adding 1 to x.
Plus equals 1 equals is bascially the exact same thing
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”.
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.