-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f19242e
commit dcdba42
Showing
25 changed files
with
1,907 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
C 数组 Arrays | ||
=== | ||
|
||
数组用于在单个变量中存储多个值,而不是为每个值声明单独的变量。 | ||
|
||
要创建一个数组,请定义数据类型(如`int`)并指定数组的名称,后跟**方括号`[]`**。 | ||
|
||
要向其中插入值,请在花括号 `{}` 内使用逗号 `,` 分隔的列表: | ||
|
||
```c | ||
int myNumbers[] = {25, 50, 75, 100}; | ||
``` | ||
我们现在创建了一个包含四个整数的数组的变量。 | ||
## 访问数组的元素 | ||
要访问数组元素,请参考其**索引号**。 | ||
数组索引以 **0** 开头:`[0]` 是第一个元素。`[1]` 是第二个元素,依此类推。 | ||
此语句访问 `myNumbers` 中的**第一个元素 [0]** 的值: | ||
```c | ||
int myNumbers[] = {25, 50, 75, 100}; | ||
printf("%d", myNumbers[0]); | ||
// 输出 25 | ||
``` | ||
|
||
## 更改数组元素 | ||
|
||
要更改特定元素的值,请参阅索引号: | ||
|
||
```c | ||
myNumbers[0] = 33; | ||
``` | ||
|
||
```c | ||
int myNumbers[] = {25, 50, 75, 100}; | ||
myNumbers[0] = 33; | ||
|
||
printf("%d", myNumbers[0]); | ||
|
||
// 现在输出 33 而不是 25 | ||
``` | ||
## 循环遍历数组 | ||
您可以使用 [`for`](./c_for_loop.md) 循环遍历数组元素。 | ||
以下示例输出 `myNumbers` 数组中的所有元素: | ||
```c | ||
int myNumbers[] = {25, 50, 75, 100}; | ||
int i; | ||
for (i = 0; i < 4; i++) { | ||
printf("%d\n", myNumbers[i]); | ||
} | ||
``` | ||
|
||
## 设置数组大小 | ||
|
||
另一种创建数组的常用方法是指定数组的大小,然后再添加元素: | ||
|
||
```c | ||
// 声明一个由四个整数组成的数组: | ||
int myNumbers[4]; | ||
|
||
// 添加元素 | ||
myNumbers[0] = 25; | ||
myNumbers[1] = 50; | ||
myNumbers[2] = 75; | ||
myNumbers[3] = 100; | ||
``` | ||
|
||
使用这种方法,**你必须知道数组的大小,**才能让程序存储足够的内存。 | ||
|
||
您无法在创建后更改数组的大小。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
C 跳出循环 Break/Continue | ||
=== | ||
|
||
## Break | ||
|
||
您已经看过本教程前面章节中使用的 `break` 语句。 它用于“跳出” [`switch`](./c_switch.md) 语句。 | ||
|
||
`break` 语句也可用于跳出**循环**。 | ||
|
||
这个例子在 `i` 等于 4 时跳出循环: | ||
|
||
|
||
```c | ||
int i; | ||
|
||
for (i = 0; i < 10; i++) { | ||
if (i == 4) { | ||
break; | ||
} | ||
printf("%d\n", i); | ||
} | ||
``` | ||
|
||
## Continue | ||
|
||
如果指定条件发生,`continue` 语句会中断一次迭代(在循环中),并继续循环中的下一次迭代。 | ||
|
||
此示例跳过 4 的值: | ||
|
||
```c | ||
int i; | ||
|
||
for (i = 0; i < 10; i++) { | ||
if (i == 4) { | ||
continue; | ||
} | ||
printf("%d\n", i); | ||
} | ||
``` | ||
|
||
## 在 While 循环中中断并继续 | ||
|
||
您还可以在 `while` 循环中使用 `break` 和 `continue`: | ||
|
||
### Break 示例 | ||
|
||
```c | ||
int i = 0; | ||
|
||
while (i < 10) { | ||
if (i == 4) { | ||
break; | ||
} | ||
printf("%d\n", i); | ||
i++; | ||
} | ||
``` | ||
|
||
### Continue 示例 | ||
|
||
```c | ||
int i = 0; | ||
|
||
while (i < 10) { | ||
i++; | ||
if (i == 4) { | ||
continue; | ||
} | ||
printf("%d\n", i); | ||
} | ||
``` | ||
|
||
|
||
## break 示例 | ||
|
||
```c | ||
#include <stdio.h> | ||
int main(){ | ||
int i=1, sum=0; | ||
while(1){ // 循环条件为死循环 | ||
sum+=i; | ||
i++; | ||
if(i>100) break; | ||
} | ||
printf("%d\n", sum); | ||
return 0; | ||
} | ||
// 运行结果:5050 | ||
``` | ||
|
||
`while` 循环条件为 `1`,是一个死循环。当执行到第 `100` 次循环的时候,计算完 `i++;` 后 `i` 的值为 `101`,此时 `if` 语句的条件 `i> 100` 成立,执行 `break;` 语句,结束循环 | ||
|
||
```c | ||
#include <stdio.h> | ||
int main(){ | ||
int i=1, j; | ||
while(1){ // 外层循环 | ||
j=1; | ||
while(1){ // 内层循环 | ||
printf("%-4d", i*j); | ||
j++; | ||
if(j>4) break; // 跳出内层循环 | ||
} | ||
printf("\n"); | ||
i++; | ||
if(i>4) break; // 跳出外层循环 | ||
} | ||
return 0; | ||
} | ||
// 运行结果: | ||
// 1 2 3 4 | ||
// 2 4 6 8 | ||
// 3 6 9 12 | ||
// 4 8 12 16 | ||
``` | ||
|
||
当 `j>4` 成立时,执行 `break;`,跳出内层循环;外层循环依然执行,直到 `i>4` 成立,跳出外层循环。内层循环共执行了 `4` 次,外层循环共执行了 `1` 次。 | ||
|
||
## continue 示例 | ||
|
||
语句的作用是跳过循环体中剩余的语句而强制进入下一次循环。`continue` 语句只用在 `while`、[`for`](c_for_loop.md) 循环中,常与 [`if`](./c_conditions.md) 条件语句一起使用,判断条件是否成立。 | ||
|
||
```c | ||
#include <stdio.h> | ||
int main(){ | ||
char c = 0; | ||
while(c!='\n'){ // 回车键结束循环 | ||
c=getchar(); | ||
if(c=='4' || c=='5'){ // 按下的是数字键 4 或 5 | ||
continue; // 跳过当次循环,进入下次循环 | ||
} | ||
putchar(c); | ||
} | ||
return 0; | ||
} | ||
// 运行结果: | ||
// 0123456789↙ | ||
// 01236789 | ||
``` | ||
|
||
程序遇到 `while` 时,变量 `c` 的值为 `'\0'`,循环条件 `c!='\n'` 成立,开始第一次循环。`getchar()` 使程序暂停执行,等待用户输入,直到用户按下回车键才开始读取字符。 | ||
|
||
本例我们输入的是 `0123456789`,当读取到 `4` 或 `5` 时,`if` 的条件 `c=='4'||c=='5'`成立,就执行 `continue` 语句,结束当前循环,直接进入下一次循环,也就是说 `putchar(c);` 不会被执行到。而读取到其他数字时,`if` 的条件不成立,`continue` 语句不会被执行到,`putchar(c);` 就会输出读取到的字符。 | ||
|
||
`break` 与 `continue` 的对比:`break` 用来结束所有循环,循环语句不再有执行的机会;`continue` 用来结束本次循环,直接跳到下一次循环,如果循环条件成立,还会继续循环。 |
Empty file.
Oops, something went wrong.