C语言中case和when的用法

C语言中case和when的用法,第1张

C语言中没有when,与case搭配使用的是switch。switch......case是C语言中一种选择匹配命令。

下面通过具体的实例来说明其用法:

int a=2, b

switch(a)

{

    case 0 :   // 如果a=0,执行该case中的语句

    {

        b = a

        break

    }

    

    case 1 :  // 如果a=1,执行该case中的语句

    {

        b = a+1

        break

    }

    case 2 :  // 如果a=2,执行该case中的语句

    {

        b = a+2

        break

    }

    default:  // 如果a与上面所以case后面的值都不相等,执行default中的语句

        b = 0

}

when和while的区别

①when是at or during the time that, 既指时间点,也可指一段时间;

while是during the time that,只指一段时间,因此when引导的时间状语从句中的动词可以是终止性动词,也可以是延续性动词,而while从句中的动词必须是延续性动词。

②when 说明从句的动作和主句的动作可以是同时,也可以是先后发生;while 则强调主句的动作在从句动作的发生的过程中或主从句两个动作同时发生。

③由when引导的时间状语从句,主句用过去进行时,从句应用一般过去时;如果从句和主句的动作同时发生,两句都用过去进行时的时候,多用while引导,如:

a. When the teacher came in, we were talking.

当此句改变主从句的位置时,则为:

While we were talking, the teacher came in.

b. They were singing while we were dancing.

④when和while 还可作并列连词。when表示“在那时”;while表示“而,却”,表对照关系。如:

a. The children were running to move the bag of rice when they heard the sound of a motor bike.

孩子们正要跑过去搬开那袋米,这时他们听到了摩托车的声音。

b. He is strong while his brother is weak.

他长得很结实,而他弟弟却很瘦弱。

具体你可以参考这一段。

when,while,as引导时间状语从句的区别

when,while,as显然都可以引导时间状语从句,但用法区别非常大。

一、when可以和延续性动词连用,也可以和短暂性动词连用;而while和as只能和延续性动词连用。

① Why do you want a new job when youve got such a good one already?(get为短暂性动词)你已经找到如此好的工作,为何还想再找新的?

②Sorry,I was out when you called me.(call为短暂性动词)对不起,你打电话时我刚好外出了。

③Strike while the iron is hot.(is为延续性动词,表示一种持续的状态)趁热打铁。

④ The students took notes as they listened.(listen为延续性动词)学生们边听课边做笔记。

二、when从句的谓语动词可以在主句谓语动作之前、之后或同时发生;while和as从句的谓语动作必须是和主句谓语动作同时发生。

1.从句动作在主句动作前发生,只用 when。

①When he had finished his homework,he took a short rest.(finished先发生)当他完成作业后,他休息了一会儿。

②When I got to the airport,the guests had left.(got to后发生)当我赶到飞机场时,客人们已经离开了。

2.从句动作和主句动作同时发生,且从句动作为延续性动词时,when,while,as都可使用。

①When /While /As we were dancing,a stranger came in.(dance为延续性动词)当我们跳舞时,一位陌生人走了进来。

②When /While /As she was making a phonecall,I was writing a letter.(make为延续性动词)当她在打电话时,我正在写信。

3.当主句、从句动作同时进行,从句动作的时间概念淡化,而主要表示主句动作发生的背景或条件时,只能用 as。这时,as常表示“随着……”;“一边……,一边……”之意。

① As the time went on,the weather got worse.(as表示“随着……”之意)

② The atmosphere gets thinner and thinner as the height increases.随着高度的增加,大气越来越稀薄。

③As years go by,China is getting stronger and richer.随着时间一年一年过去,中国变得越来越富强了。

④The little girls sang as they went.小姑娘们一边走,一边唱。

⑤The sad mother sat on the roadside,shouting as she was crying.伤心的妈妈坐在路边,边哭边叫。

4.在将来时从句中,常用when,且从句须用一般时代替将来时。

①You shall borrow the book when I have finished reading it.在我读完这本书后,你可以借阅。

②When the manager comes here for a visit next week,Ill talk with him about this.下周,经理来这参观时,我会和他谈谈此事。

三、when用于表示“一……就……”的句型中(指过去的事情)。

sb.had hardly(=scarcely) done sth.when...=Hardly / Scarcely had sb.done sth.when...

①I had hardly /scarcely closed my eyes when someone knocked at the door.=Hardly / Scarcely had I closed my eyes when someone knocked at the door.我刚一闭上眼,就有人在敲门了。

②I had hardly /scarcely entered my room when the telephone rang.=Hardly /Scarcely had I entered my room when the telephone rang.我刚一走进房门,电话就响了。

引自

http://zhidao.baidu.com/link?url=O-dXDd7uTBOIILGY9eigheR1ThDPp4IMMCuDtBRAgDUc6xXc0DuaOsI23lRyGNww5bR2LfFTCisNUu5pox4eMa

别忘记采纳啊

when 作为关系副词,在定语从句中做时间状语,相当于介词+关系代词

I still remember the year when we met for the first time. (when= in which )

Sunday is the day when very few people go to work(when= on which)

when做疑问副词,When can you come?


欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/yw/12094726.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存