Skip to content

Commit 5da2bc9

Browse files
committed
add scope
1 parent ee66802 commit 5da2bc9

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

Lesson07/Lesson07.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ do {
124124

125125
![dowhile](./dowhile.png)
126126

127-
通过对比**while循环****do-while循环**的流程图,我们可以发现二者的区别:**while**是先判断再执行循环体;而**do-while**是先执行循环体再判断的。也就是说,**do-while循环**的循环体**无论如何一定会执行一遍**
127+
通过对比**while循环****do-while循环**的流程图,我们可以发现二者的区别:**while**是先判断再执行循环体;而**do-while**是先执行循环体再判断的。也就是说,**do-while循环**的循环体**无论如何一定会执行一遍**另外要注意**do-while**的最后是要打分号的哦!
128128

129129

130130

@@ -146,6 +146,8 @@ for(expression1; expression2; expression3) {
146146

147147
![for](./for.png)
148148

149+
> 思考一下:水仙花数的示例代码的**while**循环该如何改为**for**循环呢?
150+
149151

150152

151153
### break和continue
@@ -178,3 +180,31 @@ for(expression1; expression2; expression3) {
178180

179181
## 2 变量的作用域
180182

183+
变量的作用域,指的是**变量可以被使用的范围**
184+
185+
不难知道,一个变量首先需要被声明才可以被使用,在其被声明前是无法被使用的。
186+
187+
```java
188+
i = 1; // compile error, because here we can't find i
189+
int i = 999; // declare i
190+
i = 1; // successfully find i
191+
```
192+
193+
学过了判断和循环后,我们使用代码块的频率越来越频繁了。代码块就是用**大括号**包起来的一部分代码。我们要注意,在某一块代码块内声明的变量,仅在当前代码块内有效,在代码块执行结束后,这个变量的空间就会被释放,在代码块外是不能引用到这个变量的。循环中声明的变量,也只在当前迭代有效。
194+
195+
```java
196+
i = 1; // compile error, because here we can't find i
197+
j = 1; // compile error, because here we can't find j
198+
199+
if (true) {
200+
int j = 0;
201+
}
202+
203+
for(int i = 0; i < 10; i++) {
204+
System.out.println(i); // successfully find i
205+
}
206+
207+
i = 1; // compile error, because here we can't find i
208+
j = 1; // compile error, because here we can't find j
209+
```
210+

0 commit comments

Comments
 (0)