if语句(if statement)是指编程语言(包括c语言,C#,VB,汇编语言等)中用来判定所给定的条件是否满足,根据判定的结果(真或假)决定执行给出的两种操作之一。的小编精心为您带来了C语言if语句的使用讲解【优秀5篇】,如果对您有一些参考与帮助,请分享给最好的朋友。
if函数除了遵守一般函数的通用规则以外,还有其特有的注意事项。
1、括号必须成对,上下对应。
2、if函数有N个条件则有N+1个结果,即若结果只有3种情况的,那么条件只要2个就够了。
3、if函数最多允许出现8个返回值(结果),也就意味着,最多套用7个if。
4、多个if嵌套时,尽量使用同一种逻辑运算符。
即:统一使用大于号或者统一使用小于号。
避免出现不必要的错误。
5、if是个好函数,很管用。
他的格式是:=if(条件1,返回值1,返回值2)。
多个嵌套的格式:=if(条件1,返回值1,if(条件2,返回值2,if(条件3,返回值3,返回值4)))。
这里先写3层嵌套,4、5、6、7层同理。
IF函数介绍
IF函数一般是指Excel中的IF函数,根据指定的条件来判断其“真”(TRUE)、“假”(FALSE),根据逻辑计算的真假值,从而返回相应的内容。
可以使用函数 IF 对数值和公式进行条件检测。
IF函数的语法
IF(logical_test,value_if_true,value_if_false)
Logical_test 表示计算结果为 TRUE 或 FALSE 的任意值或表达式。
例如,A10=100 就是一个逻辑表达式,如果单元格 A10 中的值等于 100,表达式即为 TRUE,否则为 FALSE。
本参数可使用任何比较运算符(一个标记或符号,指定表达式内执行的计算的类型。
有数学、比较、逻辑和引用运算符等。)。
Value_if_true logical_test 为 TRUE 时返回的值。
例如,如果本参数为文本字符串“预算内”而且 logical_test 参数值为 TRUE,则 IF 函数将显示文本“预算内”。
如果 logical_test 为 TRUE 而 value_if_true 为空,则本参数返回 0(零)。
如果要显示 TRUE,则请为本参数使用逻辑值 TRUE。
value_if_true 也可以是其他公式。
Value_if_false logical_test 为 FALSE 时返回的值。
例如,如果本参数为文本字符串“超出预算”而且 logical_test 参数值为 FALSE,则 IF 函数将显示文本“超出预算”。
如果 logical_test 为 FALSE 且忽略了 value_if_false(即 value_if_true 后没有逗号),则会返回逻辑值 FALSE。
如果 logical_test 为 FALSE 且 value_if_false 为空(即 value_if_true 后有逗号,并紧跟着右括号),则本参数返回 0(零)。
VALUE_if_false 也可以是其他公式。
一、虚拟条件句:
条件状语从句是非真实情况,在这种情况下要用虚拟语气。
1、条件从句与现在事实不一致,其句型为:
句型1:If +主语+过去时,主语+ should (could, would, 或might) +动词原形
(1) If I were you, I would study hard.
(2) If it rained, I would not be here now.
2、条件从句与过去事实不一致,其句型为:
句型2:If +主语+had+过去分词,主语+ should(could, would, 或might)+ have +过去分词
(3) If the doctor had come last night, the boy would have been saved.
(4) If I had not studied hard, I would have failed in the exam last term.
3、条件从句与将来事实不一致,其句型为:
句型3:If +主语+过去时/ should +动词原形/ were to+动词原形,主语+ should (could, would, 或might) +动词原形
(5) If it should rain tomorrow, we would stay at home.
(6) If I were to go to the moon one day, I could see it with my own eyes.
(7) If you missed the film tonight, you would feel sorry.
注意问题:
1.If条件句中绝对不可以出现would。
2、根据句中的时间状语,有时可能出现“混合虚拟”的情况,即主句可能是现在的情况,条件句也许是发生在过去的情况,但都要遵守上述句型。如:
(8) If you had studied hard before, you would be a college student now and you would graduate from a college in four years’ time.
3、条件句中如果出现were, had, should可省去if,将主语与这些词倒装。例如:
(9) Had the doctor come last night, the boy would have been saved.
(10) Were I to go to the moon one day, I would see it with my own eyes.
(11) Should it rain tomorrow, we would stay at home.
二、名词性虚拟语气:
在表示命令、建议、要求、惊叹时的名词性从句中需用虚拟语气,基本句型:
句型4:主语+(should)+动词原形
(12) Mother insisted that John (should) go to bed at 9 o’clock.(宾语从句)
(13) It was required that the crops (should) be harvested at once.(主语从句)
(14) It is surprising that she shouldn’t pass the math exam. (主语从句)
(15) The suggestion that he (should) be invited was rejected.(同位语从句)
(16) Their demand is that their wages (should) be increased.(表语从句)
注意:在这种句子中绝不可以出现would, must, could等。
三、虚拟语气在一些特殊词中的使用或含蓄条件句:
句型5:wish后的宾语从句
与现在愿望不一致:主语+过去时;
与过去愿望不一致:主语+ had +过去分词 / could + have +过去分词;
与未来愿望不一致:主语+ would / could +动词原形。如;
(17) I wish I were you.
(18) I wish I had visited the White House when I was in the States.
(19) I wish I could meet you tomorrow at the party.
句型6:It’s time句型(当It’s time后用that从句时应该为“主语+ should +动词原形”或“主语+过去时”)
(20) It’s time that you went / should go to school.
句型7:If only引起的感叹句相当于“How I wish +宾语从句”
(21) If only he could come! 他要能来就好了。
(22) If only I had known the answer! 我要早知答案就好了。
句型8:would rather, as if(though)引导的句子也需使用虚拟,表示过去的情况用过去完成时,表示现在与将来的情况
用过去时。
(23) I’d rather you posted the letter right away.
(24) I’d rather you had returned the book yesterday.
(25) She loves the children as if they were hers.
(26) Alan talked about Rome as if he had been there.
句型9:without, but, but for, but that, otherwise引起的短语或句子常暗含着虚拟条件。
(27) Without you, I would never know him.
(28) But for your cooperation, we wouldn’t have done the work so well.
(29) But that she was afraid, she would have said no.
(30) I would be most glad to help you, but I’ am busy now.
(31) I should have come to the party yesterday, but I was busy.
(32) I am busy now; otherwise I would do you the favor!
句型10:If it were not for 。.。 / If it hadn’t been for 。.。 (要不是因为……),其后面的主句也需使用虚拟。
(33) If it were not for his help, I wouldn’t go home now.
(34) If it hadn’t been for the determined captain, all the passengers on the board wouldn’t have been saved.
对过去的虚拟
条件从句(if):主语+had done 主句might/would/should/could+have done
对现在的虚拟
if+ 主语+动词过去式(be用were)主句 might/would/should/could+do
对将来的虚拟
if+主语+动词过去式(be用were)或主语+should do或主语+were to do
主句 might/would/should/could+do
e.g.
Tom got to the station in time because he started earlier.
If Tom had started late, he would have missed the train.
Do you think the thief entered through the door?
No, if he had, I don't believe, he would have broken the living room window.
If the book weren't so expensive, I would buy it.
If you didn't live so far away, we would be able to visit you more.
What would you do if you lost your passport in a foreign country?
Why hasn't he come? If he should not come on time, we would have to put off the trip.
2、注意事项
e.g.
If she hadn't work hard at English in the past, she wouldn't work as well as a secretary in a large company now. 混合时间的虚拟语气 从句为对过去的虚拟,主句是现在。
Had we not used an out-of-date timetable, we wouldn't have missed the train.
if引导从句的倒装从句相当于 if we had not used an out-of-date timetable, 同样,were还有should 在从句中都可以提前,省略if.
If only 要是。.。就好了
If only I were younger!
If only I had worked harder in my teenage years!
If only I should travel tomorrow!
1.if引导的条件状语从句可以放在主句之前,也可以放在主句之后,如果放在主句之前,中间要用逗号将主句和从句隔开。
例如:
If I am free, I will come to see you.
= I will come to see you if I am free.
如果我有空,我就来看你。
2、 在含if引导的条件状语从句的复合句中,主句用一般将来时,从句通常用一般现在时态表示将来意义,即“主将从现”原则。
例如:
If it snows tomorrow, we will go skiing.
如果明天下雪,我们就去滑雪。
3、 在含if引导的条件状语从句的复合句中,语句的谓语还可含有情态动词can、must、may等,主句也可是祈使句。
例如:
If it stops raining, we can go out.
如果雨停了,我们就能出去。
4、 在含if引导的条件状语从句的复合句中,如果主句部分描述的是客观事实或真理,要用一般现在时。
例如:
If you heat the ice, it turns into water.
如果你加热冰,它就会变成水。