随笔 - 66  文章 - 0  评论 - 0  阅读 - 22203

C语言知识点十三: C判断

有关C语言if else语句的用法很简单,大致可以分为三种:

1.当有多个条件要判断时连续用if语句:

if(条件1)

if(条件2)

if(条件3)

……

#include<stdio.h>
int main()
{
    int score[5];
    int i;
    printf("Please Enter your five subjects'scores:\n");
    for(i = 0; i < 5; i++)
    {
        scanf("%d", &score[i]);
    }
    for(i = 0; i < 5; i++)
    {
        if(score[i] >= 90)
        {
            printf("Your NO.%d subject is excellent!\n", i+1);
        }
        if(score[i] >= 80 && score[i] < 90)
        {
            printf("Your NO.%d subject is good!\n", i+1);
        }
        if(score[i] >= 60 && score[i] < 80)
        {
            printf("Your NO.%d subject is just so so!\n", i+1);
        }
        if(score[i] < 60)
        {
            printf("Your NO.%d subject is fail!\n", i+1);
        }
    }
    return 0;
}

2.当只有两个相反条件需要判断的时候就用如下格式:

if(条件)

else//这里是不要条件的,条件就是if语句条件的相反情况。

#include<stdio.h>
int main()
{
    int number = 0;
    printf("If you like computer program, you can enter 1, or enter else number!\n");
    scanf("%d", &number);
    if(number == 1)
    {
        printf("Congratulation! Please study on https://blog.csdn.net/weixin_41588502!\n");
    }
    else
    {
        printf("What are you interested in?\n");
    }

    return 0;
}

3.这种形式与第一种相似,只是用法变成了另一种形式:

if(条件1)

else if(条件2)

else if(条件3)

……

else

#include<stdio.h>
int main()
{
    int score[5];
    int i;
    printf("Please Enter your five subjects'scores:\n");
    for(i = 0; i < 5; i++)
    {
        scanf("%d", &score[i]);
    }
    for(i = 0; i < 5; i++)
    {
        if(score[i] >= 90)
        {
            printf("Your NO.%d subject is excellent!\n", i+1);
        }
        else if(score[i] >= 80)
        {
            printf("Your NO.%d subject is good!\n", i+1);
        }
        else if(score[i] >= 60)
        {
            printf("Your NO.%d subject is just so so!\n", i+1);
        }
        else
        {
            printf("Your NO.%d subject is fail!\n", i+1);
        }
    }
    return 0;
}

 

posted on   Daniel_lmz  阅读(350)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示