while循环语句

55 阅读1分钟

死循环。while表达式一直为

#include <stdio.h>
int main(){
	// 我爱你,输入三次
	int i = 0;
	while(i<3000){
		printf("I miss you \n", i+1);
		i++;
	} 
} 

image.png

#include <stdio.h>
int main(){
	while(1){	
		printf("hello world!"); 
        }
	while(0){ 
		printf("hello world!");
	}
	while(2>1){
		printf("hello world!");
	}
	// 输入一个年份,判断是否闰年? 
	while(1){
		int year;
		printf("请输入一个年份:");
    	scanf("%d", &year);
		if((year%4==0 && year%100!=0) || (year%400==0)){
			printf("%d 年是闰年\n", year); 
		}else{
			printf("%d 年不是闰年\n", year); 
		}
	}
}
  




image.png

求累加s=1+2+3+4+5

#include <stdio.h>
int main(){
	// 	s = 1 + 2 + 3 + 4 + 5 + ... + 100
	
	int s = 0;
	int i = 1;
	while(i <= 100){
		printf("%d \n", i);
		s = s + i;
		i++;
	}
	printf("%d \n",s);
}


image.png

作业1: s=1+3+5+7+9+...+99

int s = 0;
	int i = 1;
	while(i <= 100){                             
		printf("%d \n", i);
		s = s + i;
		i += 1;
	}
	printf("%d \n",s);

}

image.png

作业2: s=2+4+6+..+100

int s = 0;
int i = 2;
while(i <= 100){                             
    printf("%d \n", i);
		s = s + i;
		i += 2;
	}
	printf("%d \n",s);


image.png