循环语句

循环语句:
1.while
2.for
3.do while
1.while 语句的基本执行过程:
1.判定while语句的循环条件.(while 后面括号中的语句)2.如果条件成立,执行循环体.3.如果条件不成立,执行循环体.4.循环体执行完了,再次判定循环条件.
简单举例1.打印1-10

#include <stdio.h>
#include <stdlib.h>

int main(){
	int num=1;
	while (num <= 10){
		printf("%d\n",num);
		num++;
	}
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述
break 和continue 在while中的应用
break 是跳出当前循环,结束整个循环. continue 结束本次循环,直接跳到下一次循环.
2.break 和continue的使用简单举例

#include <stdio.h>
#include <stdlib.h>

int main(){
	int num=1;
	while (num <= 10){
		if (num == 5){
			num++;
			//continue; //遇到 continue直接跳转到while() 括号中(循环条件),然后继续执行下一次循环 continue 的运行结果  1 2 3 4 6 7 8 9 10
			break;      //遇到 break 直接跳转出当前循环,结束本次循环. 
		}
		printf("%d\n",num);
		num++;
	}
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述
3.从1-100找到第一个被3整除的整数

#include <stdio.h>
#include <stdlib.h>

int main(){
	int num = 1;
	while (num<=100){
		if (num % 3 == 0){
			printf("%d\n",num);
			break; //只要找到立马停止该循环
		}
		num++;
	}
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述
2.for 循环
for(表达式1;表达式2;表达式3)
for循环执行基本过程:1.初始化语句(表达式1)2.循环条件(表达式2)3.循环体(printf部分)4.循环变量更新(表达式3)5.循环条件(表达式2)
1.判定循环的次数

#include <stdio.h>
#include <stdlib.h>

int main(){
	int i = 0;
	int k = 0;
	  //表达式代表赋值  //表达式2,k等于0代表赋值   for (i = 0, k = 0; k = 0;i++,k++)
  for (i = 0, k = 0; k ==  0;i++,k++){   //当是这种情况代表,判定条件为k等于0时满足条件
		k++;
		printf("hehe");
	}
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述
2.打印1-10

#include <stdio.h>
#include <stdlib.h>

int main(){
	int i;
	for (i = 1; i <= 10;i++){
		printf("%d\n",i);
	}
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述

3. 从1到100中找到所有能被3 整除的整数

//法1.
#include <stdio.h>
#include <stdlib.h>

int main(){
	int num ;
	for (num = 1; num <= 100; num++){
		if (num %3 != 0){
			continue;
		}
		printf("%d",num);
		printf (" ");
	}
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述

//法2.
#include <stdio.h>
#include <stdlib.h>

int main(){
	int num = 1;
	while (num <= 100){
		if (num % 3 != 0){
			num++;
			continue;
		}
		printf("%d\n", num);
		num++;
	}
	system("pause");
	return 0;
}

法1和法2 进行比较 :
continue 在while和for中的用法不同,while中的continue接下下执行的是while()括号中的循环条件语句,
而for语句中接下来要执行的是表达式3,一定要将运行的步骤区分开
break 在在while和for中的用法相同
do while 语句
do while 语句 先执行循环体在执行循环条件.while语句先执行判定条件,在执行循环体
关于循环语句的综合例题

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

int main(){
	char arr1[] = "Welcome to beijing";
	char arr2[] = "##################";
	int left = 0;
	int right = strlen(arr1) - 1;
	while (left<=right){
		Sleep(100);
		arr2[left] = arr1[left];
		arr2[right] = arr1[right];
		left++;
		right--;
		system("cls");
		printf("%s\n", arr2);
	}
    system("pause");
	return 0;
}

运行结果:
在这里插入图片描述
2.模拟用户登陆情景,并且只能登陆三次.(如果密码正确则提示登陆成功,如果三次登陆错误则退出程序)

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
	char password[1024];
	int i;
	for (i = 0; i < 3; i++){
		printf("请输入您的登陆密码:\n");
		scanf("%s", &password);
		if (strcmp(password, "888888") == 0){
			printf("登陆成功\n");
			break;
		}
		else{
			printf("您的密码输入有误,请重试。\n");
		}
	}
	if (i == 3){
		printf("您已经输错三次了!\n");
	}
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述
C语言 strcmp() 函数用于对两个字符串进行比较(区分大小写)
头文件:string.h
原型:
strcmp(str1str2);参数 str1 和 str2 是参与比较的两个字符串。
strcmp() 会根据 ASCII 编码依次比较 str1 和 str2 的每一个字符,直到出现不到的字符,或者到达字符串末尾(遇见\0)。
返回值:
如果返回值 < 0,则表示 str1 小于 str2。
如果返回值 > 0,则表示 str2 小于 str1。
如果返回值 = 0,则表示 str1 等于 str2。
举例:使用C语言 strcmp() 函数比较用户输入的两个字符串。

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
	char str1[50] = { 0 };
	char str2[50] = { 0 };
	int i = 1;
	while (i <= 3){
		scanf("%s", &str1);
		scanf("%s", &str2);
		if (strcmp(str1, str2) == 0){
			/*printf("%s\n", str1);
			printf("%s\n", str2);*/
			printf("输入正确\n");
			break;
		}
		else{
			printf("请重新输入\n");
		}
		i++;
	}
	if (i == 4){
		printf("已经输入3次\n");

	}
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述
3.计算n的阶乘

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int Func(int n){
	int i;
	int ret = 1;
	for (i = 1; i <=n ; i++){
		ret = i*ret;
	}
	return ret;
}
int main(){
	int N;
	printf("请输入数字: N ");
	scanf("%d",&N);
	printf("%d\n",Func(N));
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述
4.计算n的阶乘之和

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

int Func(int n){
	int i;
	int ret = 1;
	for (i = 1; i <=n ; i++){
		ret = i*ret;
	}
	return ret;
}
int main(){
	int N;
	int sum = 0;
	int n;
	printf("请输入数字值: \n");
	scanf("%d",&N);
	for (int i = 1; i <= N; i++){
		sum = sum + Func(i);
	}
	printf("%d\n",sum);
	system("pause");
	return 0;
}

在这里插入图片描述
5.一组数字中查找某个数字

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int Find(int num){
	int arr[] = { 9, 5, 2, 7 };
	for (int i = 0; i < 4; i++){
		if (arr[i] == num){
			printf("找到了,下标是: %d\n", i);
		}
	}
}
int main(){
	int toFind = 2;
	Find(2);
	system("pause");
	return 0;

}

运行结果:
在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
	int toFind = 2;
	int arr[] = { 9, 5, 2, 7 };
	int i;
	for (i = 0; i < 4;i++){
		if (arr[i] == toFind){
			printf("找到了,下标是: %d\n",i);
		}
	}
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述
6.在一组有序的数组中查找一个数字(二分法查找)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){
	int toFind = 5;
	int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
	int left = 0;
	int right = sizeof(arr) / sizeof(arr[0]) - 1;
	/*while (left <= right){
		int mid = (left + right) / 2;
			if (arr[mid]>toFind){
				 right= mid - 1;
			}
			else if (arr[mid] < toFind){
				left = mid + 1;
			}
			else{
				printf("找到了,下标是: %d\n", mid);
				break;
			}
	}*/
	for (left = 0, right = sizeof(arr) / sizeof(arr[0]) - 1; left <= right; left++, right++){
		int mid = (left + right) / 2;
		if (arr[mid] > toFind){
			right = mid - 1;
		}
		else if (arr[mid] < toFind){
			left = mid + 1;
		}
		else{
			printf("找到了,下标是: %d\n", mid);
			break;
		}
	}
	if (left > right){
		printf("没找到");
	}
	system("pause");
	return 0;
}

运行结果:
在这里插入图片描述
**二分法查找: **有序数组 分为升序或者降序
7.猜数字游戏

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int Menu(){
	printf("--------------\n");
	printf(" $猜数字游戏$ \n");
	printf("--------------\n");
	int choice = 0;
	printf("请输入你的选择\n");
	printf("0.结束游戏\n");
	printf("1.开始游戏 \n");
	scanf("%d",&choice);
	return choice;
}

void  Game(){
	int toGuess = rand() % 100 + 1;// (产生1-100的随机数)
	int num;
	while (1){
		printf("请输入要猜的数字(1到100): \n");
		scanf("%d", &num);
		if (toGuess > num){
			printf("猜小了\n");
		}
		else if (toGuess < num){
			printf("猜大了\n");
		}
		else{
			printf("恭喜你,猜对了!\n");
		}
	}
}
int main(){
	srand((unsigned int )time(0));
	int choice = Menu();
	if (choice == 1){
		Game();
	}
	else {
		printf("Goodble");
	}
		system("pause");
		return 0;
}

运行结果:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值