首页 > 其他分享 >鹏哥C语言(进阶)27-30---循环语句 while

鹏哥C语言(进阶)27-30---循环语句 while

时间:2024-09-13 09:22:11浏览次数:3  
标签:ch 进阶 int 鹏哥 continue while 27 printf 循环

#define _CRT_SECURE_NO_WARNINGS
//--------------------------------------------------------------------------------------------------------------3.循环语句
// while 
// for
// do while
#include <stdio.h>

//----------------------------------------------------------------------------------------------------------3.1 while循环
/*
int main()
{
    while (1)//  ()表达式为真,执行循环
    {
        printf("hehe\n");//输出无数个 hehe ,死循环
    }
    return 0;
}
*/

-----------------------------------------------------------------
/*
int main()
{
    int i = 1;
    while (i <= 10)
    {
        printf("%d\n", i);// 输出 1 2 3 4 5 6 7 8 9 10
        i++;
    }
    return 0;
}
*/

//--------------------------------------------------------------------------------------------3.1.1 while循环中的break

//----------------------------------------------------------------------------------------- break 介绍
// 终止整个循环
/*
int main()
{
    int i = 1;
    while (i <= 10)
    {
        if (5 == i)
            break;//  break 跳出循环 ,直接终止循环
        printf("%d\n", i);// 输出 1 2 3 4 
        i++;
    }
    return 0;
}
*/

//----------------------------------------------------------------------------------------- continue 介绍
//continue 会跳过他后边的代码,直接回到判断语句再判断

//----------------------------------------------------------- continue 例一
/*
int main()
{
    int i = 1;
    while (i <= 10)
    {
        if (5 == i)
            continue; //  continue 会跳过他后边的代码,直接回到判断语句再判断
        printf("%d\n", i);// 输出 1 2 3 4 然后生成死循环
        i++;
    }
    return 0;
}
*/

//---------------------------------------------------------- continue 例二
/*
int main()
{
    int i = 1;
    while (i <= 10)
    {
        i++;
        if (5 == i)//语句为真,则执行continue 
            continue; //  continue 会跳过他后边的代码,直接回到语句while
        printf("%d\n", i);// 输出 2 3 4  6 7 8 9 11
        
    }
    return 0;
}
*/
//while 循环中的break 用于永久是终止循环
// continue 跳过本次循环后边的代码,直接去判断部分,进行下一次循环的判断


//----------------------------------------------------------------------------------------------------有趣的 while 循环

//------------------------------------------------------------------------------------------------代码1:清理缓冲区
/*
int main()
{
    //int ch = getchar(); // 输入e
     getchar()---------获取一个字符从输入,把字符的ASCLL码值拿出来放到 ch 里
    为什么用 int 不用 char
    // //EOF//end of file
    //printf("%c\n",ch);//输出e
    以字符的形式打印ch
    //putchar(ch); //输出e
    打印ch
    
    
    //---------------------------------------------------------清理缓冲区
    //int ch = 0;
    //while ((ch = getchar()) != EOF) //如果获取的字符不等于EOF,则进入循环打印
    //{
    //    putchar(ch);
    //}
    
    //----------------------------------------------------------举个例子
    //假设密码是一个字符串
    char password[20] = { 0 };
    printf("请输入密码:>(Y/N)");
    scanf("%s", password);
    
    //输完密码之后敲回车敲进去一个 \n,但是这个 \n 没有被输出,一直在缓冲区 
    //getchar(); //只能读走之前剩下的一个 \n
    
    //输入的密码中间有空格 ,例如absgd ajheg , scanf只能读取到absgd,而getchar只能读取一个空格
    //用while 循环把剩下的字符依次取出来,如下
    int ch = 0;
    while (ch = (getchar()) != '\n') //如果取出的字符不是 \n ,则继续取,直到取到 \n
    {
        ;
    }

    printf("请确认密码:>");
    int ret = getchar();
    if ('Y' == ret)
    {
        printf("YES\n");
    }
    else
    {
        printf("NO\n");
    }

    return 0;
}
*/


//-----------------------------------------------------------------------------------------------代码2:只打印数字字符
int main()
{
    char ch = '\0';
    while ((ch = getchar()) != EOF)
    {
        if (ch < '0' || ch>'9')//获取的字符的ASCLL码值 < 0的ASCLL值32  或者  >9 的ASCLL码值 41 ,则continue ,否则打印ch
            continue;
        putchar(ch);
    }
    //只打印数字字符
    return 0;
 }

标签:ch,进阶,int,鹏哥,continue,while,27,printf,循环
From: https://blog.csdn.net/most_wanted_/article/details/142201433

相关文章