C语言循环与函数详解
循环结构
在编程中,我们经常需要重复执行某段代码。C语言提供了多种循环结构来处理这种需求。
1. while循环
语法:
c
while(condition) {statement(s); }
特点:
先判断条件,后执行循环体
条件为假时,循环体可能一次都不执行
示例:
c
#include <stdio.h>int main() {int a = 10;while(a < 20) {printf("value of a: %d\n", a);a++;}return 0; }
2. for循环
语法:
c
for(init; condition; increment) {statement(s); }
执行流程:
执行初始化语句(仅一次)
测试循环条件
如果条件为真,执行循环体
执行增量语句
重复步骤2-4
示例:
c
#include <stdio.h>int main() {int a;for(a = 10; a < 20; a = a + 1) {printf("value of a: %d\n", a);}return 0; }
3. do...while循环
语法:
c
do {statement(s); } while(condition);
特点:
先执行循环体,后判断条件
循环体至少执行一次
示例:
c
#include <stdio.h>int main() {int a = 10;do {printf("value of a: %d\n", a);a = a + 1;} while(a < 20);return 0; }
4. 嵌套循环
可以在一个循环内部使用另一个循环。
嵌套for循环示例:
c
#include <stdio.h>int main() {int i, j;for(i = 2; i < 100; i++) {for(j = 2; j <= (i/j); j++) {if(!(i%j)) break; // 找到因子,不是质数}if(j > (i/j)) printf("%d is prime\n", i);}return 0; }
循环控制语句
1. break语句
作用: 立即终止循环
示例:
c
#include <stdio.h>int main() {int a = 10;while(a < 20) {printf("value of a: %d\n", a);a++;if(a > 15) {break; // 当a>15时终止循环}}return 0; }
2. continue语句
作用: 跳过本次循环的剩余代码,直接开始下一次循环
示例:
c
#include <stdio.h>int main() {int a = 10;do {if(a == 15) {a = a + 1;continue; // 跳过本次循环的剩余部分}printf("value of a: %d\n", a);a++;} while(a < 20);return 0; }
3. goto语句
语法:
c
goto label; ... label: statement;
示例:
c
#include <stdio.h>int main() {int a = 10;LOOP:do {if(a == 15) {a = a + 1;goto LOOP; // 跳转到标签LOOP}printf("value of a: %d\n", a);a++;} while(a < 20);return 0; }
注意: 不推荐使用goto语句,因为它会使程序流程难以跟踪。
4. 无限循环
使用for循环实现:
c
#include <stdio.h>int main() {for(;;) {printf("这里将永远执行.\n");}return 0; }
使用while循环实现:
c
#include <stdio.h>int main() {while(1) {printf("这里将永远执行.\n");}return 0; }
终止方法: 按 Ctrl + C
函数
函数是一组一起执行任务的语句,使代码更模块化和可重用。
1. 函数定义
语法:
c
return_type function_name(parameter list) {body of the function }
组成部分:
返回类型:函数返回值的数据类型
函数名:函数的实际名称
参数列表:函数接收的参数
函数体:包含函数功能的语句集合
示例:
c
/* 函数返回两个数字之间的最大值 */ int max(int num1, int num2) {int result;if(num1 > num2)result = num1;elseresult = num2;return result; }
2. 函数声明
函数声明告诉编译器函数的名称、返回类型和参数。
语法:
c
return_type function_name(parameter list);
示例:
c
int max(int num1, int num2); // 包含参数名 int max(int, int); // 不包含参数名(也有效)
3. 函数调用
示例:
c
#include <stdio.h>/* 函数声明 */ int max(int num1, int num2);int main() {int a = 100;int b = 200;int ret;/* 调用函数获取最大值 */ret = max(a, b);printf("Max value is : %d\n", ret);return 0; }/* 函数定义 */ int max(int num1, int num2) {int result;if(num1 > num2)result = num1;elseresult = num2;return result; }
4. 函数参数传递
按值传递(默认方式)
将实际值的副本传递给函数
函数内对参数的修改不影响原始值
示例:
c
#include <stdio.h>void swap(int x, int y); // 函数声明int main() {int a = 100;int b = 200;printf("Before swap, value of a : %d\n", a);printf("Before swap, value of b : %d\n", b);swap(a, b); // 按值传递printf("After swap, value of a : %d\n", a); // a仍然是100printf("After swap, value of b : %d\n", b); // b仍然是200return 0; }void swap(int x, int y) {int temp;temp = x;x = y;y = temp;return; }
按引用传递
传递变量的地址
函数内对参数的修改会影响原始值
示例:
c
#include <stdio.h>void swap(int *x, int *y); // 函数声明int main() {int a = 100;int b = 200;printf("Before swap, value of a : %d\n", a);printf("Before swap, value of b : %d\n", b);swap(&a, &b); // 按引用传递printf("After swap, value of a : %d\n", a); // a变为200printf("After swap, value of b : %d\n", b); // b变为100return 0; }void swap(int *x, int *y) {int temp;temp = *x; // 保存地址x处的值*x = *y; // 将y的值放入x*y = temp; // 将temp的值放入yreturn; }
总结
循环结构提供了重复执行代码的能力,包括while、for、do...while循环
循环控制语句(break、continue、goto)可以改变循环的正常执行流程
函数使代码模块化,提高可重用性
参数传递有两种方式:按值传递(不影响原始值)和按引用传递(影响原始值)
掌握这些概念对于编写高效、可维护的C语言程序至关重要。