It says, if answer, sorry, imagine x is -16, ANS is 0 is less than -16, no. So what does it do?
对不对?让我们来看看,如果答案,对不起0,如果x是-16的话,ANS为0,小于-16吧,对?
Back around to the test. What's ANS squared?
是的,那么下一步做什么呢?
Just to remind you what it does, we bound x to some value, we set up an initial variable called ANS or answer, answer and then we run through a little loop.
记住你要做些什么,我们给x赋一个值,我们建立一个初始变量,命名为ANS或者。
And I've got to worry about, is somebody else using ANS, answer, as a variable, in which case I've got to be really careful. Is somebody else using x as a variable? I've got to deal with a lot of those details.
而且我还要担心有没有别人,使用了ANS,answer作为变量,这样我就必须要非常小心,有没有别人用x当做变量呢?,我需要处理很多类似的细节。
So now, in this case, this code is going to, when we get here, check, and if you haven't seen that strange thing there, that exclamation point in bang computer-ese called a bang, it says x if ANS star ANS is not equal to x, all right?
那么现在,在这个例子中,当我们执行到这里,代码会去检查,如果你没有看到过这个奇怪的东西,这个感叹号在计算机学中被叫做,代码的意思是如果ANS的平方不等于?
I can either look at my flow chart, or I can look at the code. If I look at the flow chart, it says, I'm at this point. Look at ANS squared. Is it less than or equal to-- sorry, first of all, 0 ANS squared is 0, is it less than or equal to x, yes.
我可以看流程图也可以看我的代码,如果我看流程图的话,流程图这么说的,在这一点,看看ANS的平方,看是不是小于等于-对不起,首先,ANS的平方是。
But that table does not affect any bindings for other instances of the variable like x for ANS.
这时这个局部表就消失了,这个局部表将不会影响其它赋值,比方说像ANS对应的x变量。
What do I have here? I need the variable, ANS, ANS I need x, and I need ANS times ANS, ANS times ANS. Right. Those are the three things that are involved in this computation.
因此让我先用这个方法做吧,我这里需要什么呢?我需要变量,还需要x,然后将ANS乘以ANS,对,在这个计算中涉及到了三件事情。
Remember last time, we wrote this simple piece of code to print out even or odd. If, you know, x, 1 it was in fact, even or odd. So let me show you what a flow chart for that would look like, because I want to make a comparison point here.
这就像这里有一些脚蹼一样,首先进行测试,然后把球这样放来让ANS加,然后回来继续测试,最终跳出循环然后打印答案。
So answer, or ANS, is also bound only locally.
所以answer,或者ANS,也是局部变量的。
So what do I do? Change ANS. X doesn't change.
那么它是不是小于等于x呢?
5 Right. Sure. Square root of 16 is 5.
是的,那么ANS为5,AND的平方等于。
Now, the one that we're most interested in ANS is the one where, in fact, it gets out ANS, so you see down here in the code, there's a spot where it's going to return the value of ANS, which is what we want, right? That's the thing that holds the value that we intended to have.
现在我们最感兴趣的是这个,事实上它返回的是,所以你看这段代码,在这里返回ANS值,也就是我们想要的,对吧?,它就承载了我们想得到的值。
X stays the same. 4 squared is 16.
我没把x赋值为500?好,ANS为。
It's 1. Is it less than or equal to 16? Sure.
改变ANS,x不变,再去执行测试。
I test an end test. So the flow chart says, and the tradition was to do this in a diamond shape, I'm going to check if ANS times ANS-- oh, which way did I want to do this x - is less than or equal to x. Now that's a test.
首先进行终结测试,因此流程图就的去,传统的做法是把这个放到一个菱形里面,检查下是否ANS乘以ANS-哦,我刚才是怎么说的来着-是不是小于等于。
应用推荐