But if I did have two lines of code or more, I would in fact need to put those back.
但是如果我的确有两行或更多的代码,我其实应该把花括号加上。
You can put a variable there, and because it's not encapsulated in curly braces, as we just discussed, that essentially means it's accessible everywhere in that file.
你可以加入一个变量,因为它不是封装在花括号里,就像我们刚才讨论的,本质的意思是它可以在文件的所有地方,都可以被访问到。
And then what I get to do inside the curly braces is literally line by line enumerate the cases that I want to apply to the following code.
然后我们在花括号中要做的是,逐行地列举接下来我想要,应用的代码。
It ultimately evaluates that line of code but just once the stuff inside the curly braces, but the semicolon on the end, essentially has the effect of this.
它最终执行了那行代码,但是只有一次,当那个东西在花括号里,分号放在后面,本质上影响了这个循环。
So the open curly brace followed by the closed curly brace means everything inside of these curly braces should get executed, but only if that first condition is actually true.
所以左花括号后面跟着,右花括号,这意味着花括号里面所有的代码,都应该是要被执行的,但只有当第一个条件是正确的。
So let me challenge you with this: inside of these curly braces where I have put a comment with slash slash -- a "comment" means nothing here gets compiled, it's just for the human to read.
让我用这个来挑战你们:,在这些花括号里面,我通过双斜杠写入了一个注释,-一条“注释“在这里不会被编译,它只是用来我们阅读的。
So when you define a function, you have, as we've seen these curly braces, that define all ; of the code that's associated with that function; open curly brace code, close curly brace, that is the function.
当你定义一个函数,就像我们看到的这些花括号,定义了与,那个函数相关的所有代码;,左花括号,代码,右花括号,这就是一个函数。
But for now, it's probably simpler just always use the curly braces just to get into the habit.
但是现在,简单起见,你可以,养成每次都使用花括号的习惯。
And that's fine, because inside of curly braces can you redeclare variables with the same name if you intend to quote unquote shadow the previous variable.
那是可以的,因为,在花括号里面,你可以重新定义一个,相同名字的变量,如果你想引用之前的变量。
Right. So similarly, if you've got one line of code with a semicolon at the end saying, this is the end of the statement, it doesn't really make sense to continue that conversation to try to continue conceptually that puzzle piece with curly braces.
对的,类似的,如果你得到了一行代码,后面有一个分号,标志语句的结尾,这就没有意义来继续,那个会话来尝试对那段代码,使用花括号。
So I actually don't need the curly braces because I only have a single line of code.
所以我实际上不需要那个花括号,因为我只有一行代码。
So if you do include it up top, the subsequent lines, even if you have these curly braces, are not, in fact, going to help you.
所以如果你在前面就包含它,后面的代码,即使你有这些花括号,实际上也没有用了。
And so what I was hinting at earlier when you put semicolons in the wrong place, and then proceed to have curly braces around arbitrary lines of code, you're creating an even more local scope there, which is generally not the right intended behavior.
在之前我暗示了,当你把一个分号放在错误的地方时,然后开始用花括号,括上任意行的代码,你在那里创建了一个局部范围,那个不是故意的行为。
The curly braces ensure that everything is logically clustered where it should be.
花括号确保了,逻辑上是聚集在一起的,它本来也应该这样。
Absolutely, by functions, but you can also create local scope with curly braces.
绝对的,是由函数定义的,但是你也可以用花括号来创建局部范围。
They are correct, as the picture suggests, but then I hit that curly brace.
它们是正确的,就像那个图所表示的,然后以那个花括号结束。
> Are scopes defined by curly braces or by functions?
>,范围是由花括号定义的还是函数定义的?
Inside the curly braces is this program's code, what am I doing?
在花括号里是这个程序的代码,我在做什么?
Are -- is scope defined by curly braces or by functions?
范围是由花括号定义的还是函数定义的?
The curly braces to encapsulate one or more lines of code.
花括号压缩了一行或多行代码。
But the moment swap returns, the moment we hit this bottom most curly brace, what conceptually happens in memory?
但是swap返回时,当我们抵达花括号底端是,在内存中发生了什么?
They only mean go there when you're inside the curly braces.
只有在花括号里时,意思才是定位到那里去。
So we don't need the curly braces.
所以我们不需要那个花括号。
So we hit the curly brace so the very bottom of the function and as soon as you hit that, the next line in the story is well, then we return to main and where are we executing in main?
我们抵达花括号了,这是函数的末尾了,一旦到了那个地方,在下一行,然后返回到main函数中,在main中哪里开始执行?
The curly braces are then the C's way of kind of making a puzzle piece that looks like this so you can put stuff inside.
花括号是C语言的一个方式,用来做一个像这样的程序块,所以你可以把代码写在里面。
When you're all out of specific cases and you want to handle everything else, you literally say "default:" And here I could end the program with break, but it's kind of unnecessary because once I'm at the bottom of the curly braces, that's it.
当它不在你列出的所有情况中,为了处理其他的情况,你可以用“default:“,这里我可以用break来终止这段代码,但是它是有点,不必要的,因为我已经在这个花括号的,底部了,就这样了。
When I hit that curly brace, swap's role in life is over.
当我以那个花括号结束,swap的角色就结束了。
As an aside, if you ever just have one line of code that you want to execute, one puzzle piece in Scratch terminology, ; you actually don't need the curly braces; you only need the curly braces if you have two or more lines of code that you want to execute if that condition is true.
此外,如果你只有一行代码,需要执行,在Scratch,术语中叫做,一句代码,你就不需要使用花括号了;,如果那个条件成立,你有两行或更多行的代码时需要执行时,你就需要花括号。
应用推荐