今天看啥  ›  专栏  ›  3 ERROR(s)

C语言—循环语句超详解

3 ERROR(s)  · CSDN  ·  · 2020-01-01 00:00

前言

一、while循环

1.语法结构

while(表达式)
{
   //   循环语句;
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

2.while循环中break的作用

在while循环中遇到break,就停止后期所有的循环,直至终止循环。

案例如下:

#include <stdio.h>
int main()
{
   int i=1;
   while(i<10)
   {
     if(i==5)
     {
       break;
     }
     printf("%d",i);
     i+i+1;
   }
   return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

输出结果为:

在这里插入图片描述

3.while循环中continue的作用

终止本次循环,跳到while循环的语句判断部分,进行下一次循环的入口判断

案例如下:

int main()
{  
		int i = 1;
		while (i<10)
		{
			i = i + 1;
			if (i == 5)
				{
					continue;
				}
			printf("%d", i);
		}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

输出结果为:
在这里插入图片描述
由结果可知当continue成功运行时continue之后的语句不再执行,并且直接进行下一次while判断。

二、for循环

1.语法结构

for (表达式1; 表达式2; 表达式3)
{
	循环语句;
}
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

2.for循环和while循环对比

实现相同的功能:

while循环:

int i = 0;
i = 1;
while (i < 10)
{
	printf("%d", i);
	i++;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

for循环:

for(i=0;i<10;i++)
{
printf("%d",i);
}
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

由此可见相比于while循环,for循环的编码风格更好,在查找和修改中更方便!

三、do while循环

1.语法结构

do 
	{	
		循环语句;
	} 

while(循环表达式);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2.语法特点

循环至少执行一次!

3.do while循环中continue的作用

nt main()
{
	int i=0;
	do
	{
		i++;
		if(i==5)
		 continue;
		 printf("%d",i);
	
		 
	}while(i<10);
	
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

结果展示:
在这里插入图片描述

由结果可知当continue成功运行时continue之后的语句不再执行,并且直接进行下一次while判断。

四、演示案例(数字炸弹游戏)

1.游戏介绍

由电脑随机输出一个范围内的数字(可自定义范围),用户猜测并输入。若正确则输出正确若不正确则输出猜大了或猜小了。

2.思路分析

1.利用随机数种子生成随机数。
2.键盘输入一个数字跟随机数进行对比。使用三个if条件判断。

3.代码展示

初始界面:

void Game()
{
	int quit = 0;
	while (!quit)
	{ 
		int select = 0;
		printf("*************************\n");
		printf("*************************\n");
		printf("**********请选择*********\n");
		printf("*1.开始游戏****2.退出****\n");
		printf("*************************\n");
		printf("*************************\n");
		scanf("%d", &select);
		switch (select)
		{
		case 1:
			Guess();
			break;
		case 2:
			quit = 1;
			break;
		default:
			printf("你输入的有问题");
			break;
		}
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

Guess()函数:

void Guess()
{
	srand((unsigned long)time(NULL));//随机数种子
	int x = rand()%100+1;//定义猜测的范围是1,100
	int guess = 0;
	while (1)
	{
		printf("请开始猜-->");
		scanf("%d", &guess);
		if (guess == x)
		{
			printf("你猜对啦!\n");
			break;
		}
		else if (guess < x)
		{
			printf("你猜小啦!\n");
		}
		else
		{
			printf("你猜大啦!\n");
		}
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

主函数:

int main()
{ 
	Game();
}
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

4.结果展示

在这里插入图片描述




原文地址:访问原文地址
快照地址: 访问文章快照