程序猿入门攻略(六)

3.2 for 循环

3.2.1语法

for(表达式1;表达式2;表达式3)
    循环语句;

表达式1初始化部分,用于初始化循环变量的。

表达式2条件判断部分,用于判断循环时终止。

表达式3调整部分,用于循环条件的调整。

ex:

使用 for 循环在屏幕上打印1~10的数字。

#include <stdio.h>
int main()
{
    int i = 0;
    //i = 1(初始化部分);i <= 10(判断部分);i++(调整部分)
    for(i = 1;i <= 10;i++)
    {
        printf("%d ",i);
    }
    return 0;
}

for 循环流程图

我们对比一下 for 循环和 while 循环:

int i = 0;
//实现相同的功能,使用 while
i = 1;                //初始化部分
while(i <= 10)        //判断部分
{
    printf("hehe\n");
    i = i + 1;        //调整部分
}

//实现相同的功能,使用 for
for(i = 1;i <= 10;i++)
{
    printf("hehe\n");
}

可以发现在 while 循环中依然存在三个必须条件,但是由于风格问题使得三个部分很可能偏离较远,这样查找修改就不够集中和方便。所以,for 循环的风格更胜一筹,for 循环使用的频率也最高。

3.2.2 break 和 continue 在 for 循环中 

我们通过上面 for 循环的流程图可知,for 循环中也存在 break 和 continue ,他们的意义和在 while 循环中是一样的。

//代码1
#include <stdio.h>
int main()
{
	int i = 0;
	for (i = 1; i <= 10; i++)
	{
		if (i == 5)
			break;
		printf("%d ", i);
	}
	return 0;

}

//代码2
#include <stdio.h>
int main()
{
	int i = 0;
	for (i = 1; i <= 10; i++)
	{
		if (i == 5)
			continue;
		printf("%d ", i);
	}
	return 0;

}

 

3.2.3 for 语句的循环控制变量

建议:

1、不可在 for 循环体内修改循环变量,防止 for 循环失去控制。

2、建议 for 语句的循环控制变量的取值采用“前闭后开区间”写法。(具体情况需具体分析)

int i = 0;
//前闭后开的写法
for(i = 0;i < 10;i++)
{ }

//两边都是闭区间
for(i = 0;i <= 9;i++)
{ }

3.2.4 一些 for 循环的变种

#include <stdio.h>
int main()
{
	//代码1
	for (;;)
	{
		printf("hehe");
	}
	//for 循环中的初始化部分,判断部分,调整部分是可以省略的,
	//但是不建议初学时省略,容易导致问题

	//代码2
	int i = 0;
	int j = 0;
	//这里打印多少个hehe?
	for (i = 0; i < 10; i++)
	{
		for (j = 0; j < 10; j++)
		{
			printf("hehe");
		}
	}

	//代码4——使用多余一个变量控制循环
	int x, y;
	for (x = 0, y = 0; x < 2 && y < 5; ++x, y++)
	{
		printf("hehe\n");
	}
	return 0;
}

 代码2:(共打印100个hehe)

 代码3:

3.2.5 一道笔试题

//请问for语句要循环多少次
#include <stdio.h>
int main()
{
	int i = 0;
	int k = 0;
	for (i = 0, k = 0; k = 0; i++, k++)
		k++;
	return 0;
}

我是答案:

 

3.3 do...while() 循环

3.3.1 do 语句的语法:

do
	循环语句;
while (表达式);

3.3.2 执行流程 

3.3.3 do...while 语句的特点

循环至少执行一次,使用的场景有限,所以不是经常使用。

#include <stdio.h>
int main()
{
	int i = 10;
	do
	{
		printf("%d\n", i);
	} while (i < 10);
	return 0;
}

 3.3.4 do...while 循环中的 break 和 continue

#include <stdio.h>
int main()
{
	int i = 10;
	do
	{
		if (5 == i)
			break;
		printf("%d\n", i);
	} while (i < 10);
	return 0;
}

#include <stdio.h>
int main()
{
	int i = 10;
	do
	{
		if (5 == i)
			continue;
		printf("%d\n", i);
	} while (i < 10);
	return 0;
}

3.4 练习 

1、计算 n 的阶乘。

2、计算 1!+2!+3!+......+10! 

3、在一个有序数组中查找具体的某个数字 n 。(折半查找、二分查找)

  • 顺序查找:

  •  折半查找(二分查找)
#include <stdio.h>
int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	int left = 0;
	int right = sizeof(arr) / sizeof(arr[0]) - 1;
	int key = 7;
	int mid = 0;
	while (left <= right)
	{
		mid = (left + right) / 2;
		if (arr[mid] > key)
		{
			right = mid - 1;
		}
		else if (arr[mid] < key)
		{
			left = mid + 1;
		}
		else
			break;
	}
	if (left <= right)
		printf("找到了,下标是%d\n", mid);
	else
		printf("找不到\n");
}

 

4、编写代码,演示多个字符从两端移动,向中间汇聚。

#include <stdio.h>
#include <windows.h>
int main()
{
	char arr1[] = "I love the code!";
	char arr2[] = "################";

	int left = 0;
	int right = strlen(arr2) - 1;

	while (left <= right)
	{
		arr2[left] = arr1[left];
		arr2[right] = arr1[right];
		printf("%s\n", arr2);
		Sleep(1000);//延时

		//清空屏幕
		system("cls");//system是一个库函数,可以执行系统命令
		left++;
		right--;
	}
	printf("%s\n", arr2);
	return 0;
}

5、编写代码实现,模拟用户登录情景,并且只能登录三次。(只允许输入三次密码,如果密码正确则提示登录成功,如果三次均输入错误,则退出程序)

#include <stdio.h>
#include <string.h>
int main()
{
	int i = 0;
	char password[20] = { 0 };
	//假设密码是字符串:abcdef
	for (i = 0; i < 3; i++)
	{
		printf("请输入密码:>");
		scanf("%s", password);
		if (strcmp(password, "abcdef") == 0)
		{
			printf("登陆成功\n");
			break;
		}
		else
		{
			printf("密码错误\n");
		}
	}
	if (i == 3)
	{
		printf("三次密码均输入错误,退出程序\n");
	}
	return 0;
}

6、猜数字游戏

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void menu()
{
	printf("*****************************\n");
	printf("********   1. play   ********\n");
	printf("********   0. exit   ********\n");
	printf("*****************************\n");
}
//RAND_MAX--rand函数能返回随机数的最大值。
void game()
{
	int guess = 0;
	//1、生成随机数
	//0~99-->1~100
	int ret = rand() % 100 + 1;//生成随机数的函数
	//printf("%d\n", ret);
	//猜数字
	while (1)
	{
		printf("请猜数字:>");
		scanf("%d", &guess);
		if (guess < ret)
		{
			printf("猜小了\n");
		}
		else if (guess > ret)
		{
			printf("猜大了\n");
		}
		else
		{
			printf("恭喜你,猜对了!\n");
			break;
		}
	}
}
//指针
int* p = NULL;
int a = 0;

int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));
	do
	{
		menu();
		printf("请选择:>");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();//猜数字的整个逻辑
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误,重新选择!\n");
			break;
		}
	} while (input);
	return 0;
}

4、goto 语句

C语言中提供了可以随意滥用的 goto语句和标记跳转的标号。

从理论上 goto语句是没有必要的,实践中没有goto语句也可以很容易的写出代码。

但是某些场合下goto语句还是用得着的,最常见的用法就是终止程序在某些深度嵌套的结构的处理过 程。

例如:一次跳出两层或多层循环。 多层循环这种情况使用break是达不到目的的。它只能从最内层循环退出到上一层的循环。

goto语言真正适合的场景如下:

for(...)
    for(...)
   {
        for(...)
       {
            if(disaster)
                goto error;
       }
   }
    …
error:
 if(disaster)
         // 处理错误情况

下面是使用goto语句的一个例子,然后使用循环的实现方式替换goto语句:

//一个关机程序
#include <stdio.h>
int main()
{
	char input[10] = { 0 };
	system("shutdown -s -t 60");
again:
	printf("电脑将在1分钟内关机,如果输入:我是猪,就取消关机!\n请输入:>");
	scanf("%s", input);
	if (0 == strcmp(input, "我是猪"))
	{
		system("shutdown -a");
	}
	else
	{
		goto again;
	}
	return 0;
}

而如果不适用goto语句,则可以使用循环:

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char input[10] = {0};
    system("shutdown -s -t 60");
    while(1)
   {
        printf("电脑将在1分钟内关机,如果输入:我是猪,就取消关机!\n请输入:>");
        scanf("%s", input);
        if(0 == strcmp(input, "我是猪"))
       {
            system("shutdown -a");
            break;
       }
   }
    return 0;
}

关于shutdown命令的扩展-(请点这里)

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值