第13天3,21

//getchar

//EOF - end of file - 文件的结束标志

//putchar - 输出一个字符

#include<stdio.h>
int main()
{
    int ch = getchar();
    //printf("%c\n", ch);
    putchar(ch);//输出一个字符
    return 0;

    return 0;
}

int main()
{
    int ch = 0;
    //ctrl + z -getchar  就读取结束
    while ((ch = getchar()) != EOF)
    {
        putchar(ch);
    }
    return 0;
}

getchar\scanf都是输入函数,它需要去中间的缓冲区拿数据

int main()
{
    char password[20] = { 0 };
    printf("请输入密码:>");
    scanf("%s", password);
    printf("请确认密码(Y/N):>");
    //需清理缓冲区,要不然读取缓冲区的\n,会不读取Y直接输出
    //getchar();//处理缓冲区,处理‘\n’
    //情况2,123456 abcdef\n  后面多个字符拿不完
    //清理缓冲区中的多个字符
    int tmp = 0;
    while ((tmp = getchar()) != '\n')
    {   ;}

    int ch = getchar();
    if (ch == 'y')
    {
        printf("确认成功\n");
    }
    else
    {
        printf("确认失败\n");
    }
    return 0;
}

 while语句普通格式:

int main()
{
    int i = 1;  //初始化
    while (i <= 10)  //判断部分
    {
        printf("%d ", i);
        i++;  //调整部分
    }
    return 0;
}

for循环

for(表达式1;表达式2;表达式3)

         初始化       判断          调整

break和continue在for循环的作用

break是终止

continue是跳过此次,继续判断,和while语句里的continue不一样

int main()
{
    int i = 0;
    for (i = 1; i <= 10; i++)
    {
        if (i == 5)
            //break;
            continue;

        printf("%d ", i);
    }

    return 0;
}

for循环内可以嵌套

for语句的循环控制变量   建议:

1、不可在for循环体内修改循环变量,防止for循环失去控制。

2、建议for语句的循环控制变量的取值采用“前闭后开区间”写法。

for循环有变种

变种一:for(;;)//判断部分的省略-就是死循环,判断部分恒为真

变种二:多个变量

int main()
{
    int x = 0;
    int y = 0;
    for (x = 0, y = 0; x < 2 && y < 5; ++x, y++)
    {
        printf("hehe\n");
    }

    return 0;
}

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值