标签:style io 使用 strong for sp 代码 on c
1.while循环基本使用
int main()
{
	int cout = 0;
	while(count<=50)
	{
		++count;
		printf("做第%d次俯卧撑\n count");//(循环体)
		
	}
	return 0;
}
使用while 循环的时候,先确定需要重复执行的操作,在确定约束条件。
2.while循环的关键字
continue
结束当前的循环体,进入下一次循环体的执行
int main()
{
	int cout = 0;
	while(count<=50)
	{
		++count;
		if(count%10 ==0);
		{
			continue;  //如果是10的倍数,那么就跳出if重新判断循环条件
		}
		printf("做第%d次俯卧撑\n count");//(循环体)	
	}
	return 0;
}
break
直接结束整个while循环,退出整个循环的关键字
int main()
{
	int cout = 0;
	while(count<=50)
	{
		++count;
		printf("做第%d次俯卧撑\n count");
		if(count == 30)
		{
			break; //意思是循环进行到count== 30的时候退出整个while循环
		}
	}
	return 0;
}
总结
while 循环运行原理
1.如果一开始条件不成立,永远不会执行循环体
2.如果条件成立,就会执行一次循环体,执行完毕,再次判断条件是否成立.......
练习1:
提示用户输入一个正整数n,计算1+2+3+...+n的和
#include <stdio.h>
int main ()
{
	int n =0;  //  一定要初始化
	while(n <= 0)
	{
	//1.提示输入
	printf(请输入一个正整数:\n);
	//2.接收输入
	scanf("%d", &n);
	}
	
	
	
/*****************方法2**************
	int n;
	
	if ( n <= 0)  
	{
		printf("非法输入!!");
		return 0; 
	}
	int sum = 0
***********************************/
	int number =1 //默认被加的数值
	
	// 3.计算
	while (number < n)
	{
		sum += number;
		number++;
	}
	
	printf("%d\n", sum)
	return 0;
}
练习2:
计算100以内所有3的倍数的个数
#include <stdio.h>
int main ()
{
	// 记录3的倍数的个数
	int count = 0;
	// 记录当前检查的数值
	int number = 0;
	while (number <100 )
	{
		number++;
		//说明number是3的倍数
		
		if(number%3 == 0){
			count++;
		}
	}
	printf ("1-100内3的倍数的个数:%d\n, count")
	return 0;
}
while循环使用注意
while(10) // 10为真,为1
{
	printf("hahahah\n");
}
 
上面代码会引发死循环,所以要注意while循环的条件
int a = 10;
while(a>0)
printf("hahahah\n"); // 如果while后面没有大括号,那么会默认while后面第一个语句为循环体
{
}
int a = 10;
while(a>0); // 死循环,后面的分号代表while循环这个结构完了,后面不是while的部分
{
printf("hahahah\n");
}
while(1); //最简单的死循环
do while 循环
do{
}while(条件); // do while 循环不管条件成不成立都会先执行一次条件
很多情况下while 和do while是可以互换的
for循环
for(语句1;条件;语句2)
{
	循环体;
}
语句1:初始化语句
语句2:增量语句(执行完循环体后在执行的语句)
for循环一开始就会执行一次语句1(整个for循环只会执行一次)
判断条件是否成立,如果成立,就会执行一次循环体,然后就会执行语句2,再次判断条件是否成立
#include <stdio.h>
int main ()
{
	for( int count = 0; count  <50 ; count++)
	{
		printf("做第%d次俯卧撑\n,count");
	}
	return 0;
}
for 循环把变量定义在语句里面,语句结束后变量就会回收,而while 循环则把变量定义在语句外面,不能及时回收变量,所以for循环要比while性能要高。
for循环使用注意(在代码中详细介绍)
for(int i = 0;i<5;i++); //注意,不要随便在for()后面写分号
{
	printf("haha\n");
}
下面是错误写法,for定义变量要加{},不然变量的作用域会不明确,编译器报错
for(int i = 0;i<5;i++);
	int a = 10;
for(int i = 0;i<5;i++,a++);
{
	int a = 10; 这样定义是错误的,因为执行a++之前变量a就已经销毁了
	//int i = 10;  这样定义是没错误的,因为语句1定义的i的作用域已经包括了语句2里面的i++
}
//循环体每次执行完都会自动销毁
for(;;); // for循环最简单死循环
for循环嵌套(用在像输出QQ好友列表的时候)
#intclude <stdio.h>
int main()
{
	for(int i=3;i<=3;i++)
	{
		printf("好友列表%d\n",i);
		for(int j =1;j<=6; j++)
		{
			printf("好友%d\n",j);
		}
	}
	return 0;
}
上面代码会输出i个好友列表,每个列表有j个好友
for循环嵌套练习
// 提示用户输入一个正整数n,如果n=5,就输出下列图形,其他n值以此类推
*****
****
***
**
*
#include <stdio.h>
int main()
{
	/提示一个变量储存用户输入的值
	int n =0;
	
	//判断n值合不合理
	while(n <=0)
	{
		提示用户输入正整数
		printf("请输入一个正整数:\n");
		scanf(%d,&a);
	}
	//输出图形
	for(int i=0; i<=n;i++) //有多少行
	{
		for(int j=0;j<=n-i;j++)
		{
			printf("*");
		}
		pintf("\n");
	}
	return 0;
}
break和continue使用注意
break使用场合
1.switch语句,退出整个switch语句
2.循环结构:退出整个循环结构
while
do while 
for
continue 使用场合
循环结构:结束当前这次循环体,进入下一个循环体
while 
do while 
for
break和while 只对最进的循环体有效
标签:style io 使用 strong for sp 代码 on c
原文地址:http://www.cnblogs.com/jia694600474/p/3975934.html