while循环

while循环

格式:

先定义循环变量并赋初值
while(终止条件)
{
	语句块;
	增值或减值语句;
}

执行顺序:先赋初值,然后判断终止条件是否成立,如果成立就执行语句块,增值减值,然后继续判断终止条件是否成立,直到条件不成立就结束循环。

do_while

格式:

do
{
	语句块;
	增值减值语句;
}while(终止条件)

执行顺序:先执行后判断。

例题:打印1到9

#include<stdio.h>
int main(int argc, char const *argv[])
{
    int a = 1;
    while(a<10)
    {
        printf("%d\n",a);
        a++;
    }
    return 0;
}

while1
1 到10的和

#include <stdio.h>
int main()
{
    int a = 1;
    int sum = 0;
    while (a <= 10)
    {
        sum = sum + a;
        a++; //a自加 
    }
    printf("%d", sum);
    putchar(10);//'\n'的ASCII是10
    return 0;
}

while2

死循环:

for(;;)
{
}  
while(1)
{
}  
while(1);

循环控制语句

break:直接结束整个循环
continue:结束本次循环,继续下一次循环

使用场景:
在循环语句中,结束循环;使用时需要有判断语句

#include<stdio.h>
int main(int argc, char const *argv[])
{
    int i;
    for(i = 1;i <= 10;i++)  //使用时需要有判断语句
    {
        printf("%d ",i);	//打印从1到10
    }
    printf("\n");
    return 0;
}


break0

#include <stdio.h>
int main(int argc, char const *argv[])
{
    int i;
    for (i = 1; i <= 10; i++)
    {
        if (i = 4)		//当i自加到4,直接结束整个循环
            break;
    }
    printf("%d ", i);	//打印输出在for循环外面
    printf("\n");
    return 0;
}

break1

例题:从终端上输入一个字符并在终端上输出这个字符,当输入字符为‘q’时,程序结束。
方法一:

#include <stdio.h>
//从终端输入一个字符并在终端上输出这个字符,当输入字符'q'时,程序结束
int main(int argc, char const *argv[])
{
    char i;
    for (;;)
    {
        scanf(" %c", &i);//输入字符
        if (i == 'q')
            break;      //输入'q'程序结束
        printf("%c", i);
    }
    return 0;
}

输入“wdf"“td”"q"的结果
test1
方法二:

#include <stdio.h>
int main(int argc, char const *argv[])
{
    char ch;
    while (1)
    {
        scanf("%c", &ch);
        if (ch == '\n')
            continue;
        if (ch == 'q')
            break;
        printf("%c", ch);
    }
    return 0;
}

输入“wdf"“td”"q"的结果
test2

垃圾字符回收

参考资料垃圾字符回收及程序的三大结构
造成垃圾字符的原因:
scanf单字符输入时规定只接收一个字符,当我们输入更多的内容时,例如我们给a赋值时键盘上输入的是 1+回车 ,则1顺利传给了a,但是多余的回车带来的换行符 \n 却被遗留在了我们的缓冲区,再继续给b赋值时,我们又进行了一次 2+回车 的操作。从而造成 “b=” 后没有值,而且进行了一次换行的情况。
————————————————
版权声明:本文为CSDN博主「cai_bi_A_LV」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/cai_bi_A_LV/article/details/128716040

垃圾字符回收方法

  1. 通过空格回收
    可以回收一个或多个空格、tab、\n
scanf("%c %c",&a,&b);//只需要在使用scanf()函数时,控制格式的%c等字符前加上空格,即可实现垃圾字符的回收
  1. %*c
    可以回收任意一个字符,不推荐使用
  2. getchar()
    一般在循环中使用,可以回收任意一个字符
while(1)
{
	scanf("%c",&a);//
	printf("%c",a);//
}

练习:循环输入一个5位数,判断它是不是回文数。当输入0时循环结束。
即12321是回文数,个位与万位相同,十位与千位相同。

#include <stdio.h>
//循环一个五位数,判断是不是回文。输入0循环结束。
int main(int argc, char const *argv[])
{
    while (1)
    {
        int a, b, c, d, e;
        int finger;
        scanf("%d", &finger); //以“65253”为例
        //finger == a * 10000 + b * 1000 + c * 100 + d * 10 + e;
        a = finger / 10000;
        b = finger % 10000 / 1000;
        c = finger % 10000 % 1000 / 100;	//此步可省
        d = finger % 10000 % 1000 % 100 / 10;
        e = finger % 10;
        if (finger == 0)
        {
            printf("程序停止\n");
            break;  
        }
        else if (a == e && b == d)
            printf("%d是回文\n", finger);
        else
        {
            printf("这不是回文\n");
        }
    }
    return 0;
}

回文

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值