小朋友学C语言(29):switch case语句

switch case语句与if elseif语句类似,都是从多个选择条件里选取一个来执行。

(一)先来看一个if elseif程序

#include <stdio.h>
int main() 
{
    int number;
    printf("Please input an integer between 1~7: ");
    scanf("%d", &number);
    printf("Today is ");
    if(1 == number) 
    {
        printf("Monday!");
    }
    else if(2 == number) 
    {
        printf("Tuesday!");
    }
    else if(3 == number) 
    {
        printf("Wednesday!");
    }
    else if(4 == number) 
    {
        printf("Thursday!");
    }
    else if(5 == number) 
    {
        printf("Friday!");
    }
    else if(6 == number) 
    {
        printf("Saturday!");
    }
    else if(7 == number) 
    {
        printf("Sunday!");
    }
    else
    {
        printf("a wrong day! Please input an integer between 1~7!");
    }
    return 0;
}

运行结果:

Please input an integer between 1~7: 7
Today is Sunday!

(二)再用switch case语句实现相同的功能

#include <stdio.h>
int main() 
{
    int number;
    printf("Please input an integer between 1~7: ");
    scanf("%d", &number);
    printf("Today is ");
    switch(number) 
    {
        case 1:
            {
                printf("Monday!");
                break;      
            }
        case 2:
            {
                printf("Tuesday!");
                break;      
            }
        case 3:
            {
                printf("Wednesday!");
                break;      
            }
        case 4:
            {
                printf("Thursday!");
                break;      
            }
        case 5:
            {
                printf("Friday!");
                break;      
            }
        case 6:
            {
                printf("Saturday!");
                break;      
            }
        case 7:
            {
                printf("Sunday!");
                break;      
            }
        default:
            {
                printf("a wrong day! Please input an integer between 1~7!");
                break;              
            }
    }
    return 0;
}

运行结果1:

Please input an integer between 1~7: 7
Today is Sunday!

运行结果2:

Please input an integer between 1~7: 10
Today is a wrong day! Please input an integer between 1~7!

注意: switch case的结构为

switch(表达式或变量或常量)
{
     case 条件1:
        {
            执行语句;
            break;
        }
    case 条件2:
        {
            执行语句;
            break;
        }
    ……
    default:
        {
            执行语句;
            break;
        }
}

这里“break;”表示中断,若忘了写,程序会继续执行下面的条件。

(三)试试下面这个程序

#include <stdio.h>
int main() 
{
    int number;
    printf("Please input an integer between 1~7: ");
    scanf("%d", &number);
    printf("Today is ");
    switch(number) 
    {
        case 1:
            {
                printf("Monday!");  
            }
        case 2:
            {
                printf("Tuesday!");     
            }
        case 3:
            {
                printf("Wednesday!");       
            }
        case 4:
            {
                printf("Thursday!");    
            }
        case 5:
            {
                printf("Friday!");      
            }
        case 6:
            {
                printf("Saturday!");    
            }
        case 7:
            {
                printf("Sunday!");      
            }
        default:
            {
                printf("a wrong day! Please input an integer between 1~7!");            
            }
    }
    return 0;
}

运行结果:

Please input an integer between 1~7: 3
Today is Wednesday!Thursday!Friday!Saturday!Sunday!a wrong day! Please input an integer between 1~7!

(四)用while实现多次输入

#include <stdio.h>
int main() 
{
    int number;
    printf("Please input an integer between 1~7: ");
    scanf("%d", &number);
    // 循环终止条件为number小于或等于0 
    while(number > 0)
    {               
        printf("Today is ");
        switch(number) 
        {
            case 1:
            {
                printf("Monday!");
                break;      
            }
            case 2:
            {
                printf("Tuesday!");
                break;      
            }
            case 3:
            {
                printf("Wednesday!");
                break;      
            }
            case 4:
            {
                printf("Thursday!");
                break;      
            }
            case 5:
            {
                printf("Friday!");
                break;      
            }
            case 6:
            {
                printf("Saturday!");
                break;      
            }
            case 7:
            {
                printf("Sunday!");
                break;      
            }
            default:
            {
                printf("a wrong day! ");
                break;              
            }
        }//switch
        printf("\nPlease input an integer between 1~7: ");
        scanf("%d", &number);
    }//while
    printf("Loop end!\n");
    return 0;
}

运行结果:

Please input an integer between 1~7: 7
Today is Sunday!
Please input an integer between 1~7: 5
Today is Friday!
Please input an integer between 1~7: 1
Today is Monday!
Please input an integer between 1~7: 10
Today is a wrong day!
Please input an integer between 1~7: 0
Loop end!

(五)若输入1~7,打印结果并结束程序;否则重新输入

#include <stdio.h>
void print_day(int num)
{
    switch(num) 
    {
        case 1:
        {
            printf("Monday!");
            break;      
        }
        case 2:
        {
            printf("Tuesday!");
            break;      
        }
        case 3:
        {
            printf("Wednesday!");
            break;      
        }
        case 4:
        {
            printf("Thursday!");
            break;      
        }
        case 5:
        {
            printf("Friday!");
            break;      
        }
        case 6:
        {
            printf("Saturday!");
            break;      
        }
        case 7:
        {
            printf("Sunday!");
            break;      
        }
        default:
        {
            break;              
        }
    }//switch           
}
int main() 
{
    int number;
    printf("Please input an integer between 1~7: ");
    scanf("%d", &number);
    // 默认循环条件一直为真,遇到break循环才会终止
    while(1)
    {
        if(number >= 1 && number <= 7)  
        {
            printf("Today is ");
            print_day(number);
            printf("\nProgram end!\n");
            break;
        }           
        printf("It is a wrong day!\n");
        printf("\nPlease input an integer between 1~7: ");
        scanf("%d", &number);
    }//while
    return 0;
}

运行结果:

Please input an integer between 1~7: 10
It is a wrong day!
Please input an integer between 1~7: 20
It is a wrong day!
Please input an integer between 1~7: 5
Today is a Friday!
Program end!

原文发布于微信公众号 - 海天一树(gh_de7b45c40e8b)

原文发表时间:2017-11-24

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏技术之路

wpf listBox 多列大图片效果

修改ListBox的模版 多列大图片效果,加上删除button 看图 ? 上代码! <Window x:Class="Thunder.SetCenter.Roo...

53370
来自专栏大内老A

Enterprise Library Policy Injection Application Block 之三:PIAB的扩展—创建自定义CallHandler(提供Source Code下载)

本系列的第一部分对PIAB使用场景进行了简单的介绍,作中阐述了通过PI(Policy Injection)的方式实现了Business Logic和Non-Bu...

354100
来自专栏函数式编程语言及工具

FunDA(16)- 示范:整合并行运算 - total parallelism solution

   在对上两篇讨论中我们介绍了并行运算的两种体现方式:并行构建数据源及并行运算用户自定义函数。我们分别对这两部分进行了示范。本篇我准备示范把这两种情况集成一体...

210100
来自专栏草根专栏

从头编写 asp.net core 2.0 web api 基础框架 (5) EF CRUD

Github源码地址:https://github.com/solenovex/Building-asp.net-core-2-web-api-starter-...

41860
来自专栏.NET后端开发

ADO.NET入门教程(七) 谈谈Command对象高级应用

摘要 在上一篇文章《你必须知道的ADO.NET(六) 谈谈Command对象与数据检索》中,我详细讲解了Command对象的基础知识以及基本用法。作为ADO.N...

54090
来自专栏jessetalks

async & await 的前世今生(Updated)

async 和 await 出现在C# 5.0之后,给并行编程带来了不少的方便,特别是当在MVC中的Action也变成async之后,有点开始什么都是asyn...

353110
来自专栏大内老A

我的WCF之旅(7):面向服务架构(SOA)和面向对象编程(OOP)的结合——如何实现Service Contract的继承

当今的IT领域,SOA已经成为了一个非常时髦的词,对SOA风靡的程度已经让很多人对SOA,对面向服务产生误解。其中很大一部分人甚至认为面向服务将是面向对象的终结...

20150
来自专栏丑胖侠

《Drools7.0.0.Final规则引擎教程》番外实例篇——FactHandler使用案例

背景 在使用具体的业务使用中,我们经常会通代码对Fact对象进行操作,Drools为我们提供了FactHandler来获取对象的句柄,通过此返回值可以对Work...

28550
来自专栏跟着阿笨一起玩NET

AutoResetEvent.WaitAll 等到人生三大事,然后大笑开心。

例子描述:人生都有追求幸福理想,下面就用三条线程得到房子,车子,妻子,等待全部得到后,显示人生圆满。

29420
来自专栏c#开发者

Winform 的一个多线程绑定DataGrid数据源的例子

我们都知道简单的运用多线程的方法有 1/ Thread thread=new Thread(new StartThread(this.method))     ...

31990

扫码关注云+社区

领取腾讯云代金券