while 循环(1)

38 阅读1分钟

死循环

把我爱你输出三百次

#include <stdio.h>

int main(){
	int i=0;
	while(i<3000){
		printf("I miss you \n");
		i++;
	}
	
	return 0; 
} 

跳出循环 ,使用break;

判断闰年

#include <stdio.h>
int main() {
 while(1){
        int year;
        printf("\n请输入要判断的年份(0表示退出):");
        scanf("%d", &year);
        if(year == 0){
            break;
        }

        if( (year%100!=0 && year%4==0) || year%400==0 ) {
            printf("%d 是闰年\n",year);
        } else {
            printf("%d 不是闰年\n",year);
        }
    }
    printf("\n下次再来吧~");
    return 0;
}

循环变量 i j k

int i = 0; 
while(i < 9){
    printf("I miss you \n"); 
    i++; 
}


int i = 0; 
while(i < 9){
    printf("I miss you \n"); 
    i += 2; 
}


int i = 10; 
while(i > 3){
    printf("I miss you \n"); 
    i--; 
}

求累加

#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);

    return 0;
}