while循环

83 阅读1分钟
#include <stdio.h>
/*
  while循环 
*/
int main(){
	// 把我爱你,输出3000次。 
	int i = 0;
	while(i<3000){
		printf("I miss you\n",i+1);
		i++;
	}
	return 0;
	
}

	

运行结果: image.png


#include <stdio.h>
/*
  while循环 
  1.死循环。 while(表达式) 表达式一直为ture,循环一直执行 
*/
int main(){
//	while(1){
//		printf("hello world");
//	}
//	while(0){
//		printf("hello world");
//	}
//	while(2>1){
//		printf("hello world");
//	}
	
	
	while(1){
		int year;
	    printf("请输入要判断年份(0表示退出):");
	    scanf("%d",&year);
	
	    if(year % 4== 0 && year % 100 != 0) || (year % 400 == 0) {
	        printf("%d年是闰年\n",year);
	    }else{
	    	printf("%d年不是闰年\n",year);
		}
	
	}
	 printf("\n下次再来~"); 
}

image.png

#include <stdio.h>
/*
  while循环 
  1.死循环。 while(表达式) 表达式一直为ture,循环一直执行
   2.跳出循环,使用break;
   3.循环变量
   4. 求累加 s =1+2+3+4+5 
*/
int main(){
	//s = 1+2+3+4+5+6+...+100
	int s = 0;
	int i = 1;
	while(i <= 100){
		printf("%d\n",i);
		s = s + i;
		i++;
	}
	printf("%d\n",s);
	
	return 0;
}
	
	

image.png