当前位置:
首页 > 最新政策 > c语言if语句 用法是什么(陈岩)

知识点

c语言if语句 用法是什么(陈岩)

c语言if语句是f (expression)语句1 [else语句2]。

c语言中if语句的用法

c语言提供三种形式的if语句

1.if (expression)语句。

示例:if(x >:y)printf(& quot;% d & quot,x);

此时,如果表达式为真,则执行printf语句。

2.if (expression)语句1 else语句2

例如:

if(x >;y)printf(& quot;% d & quot,x);

else printf(& quot;% d & quot,y);

此时,如果x >: Y为真,执行语句printf(& quot;% d & quot,x),然后直接跳过else,并跳过printf语句(& quot% d & quot,y),以执行以下语句。

如果x >: Y为false,语句将打印(& quot% d & quot,x),执行语句printf(& quot;% d & quot,x).

3.if(表达式1)语句1

Else if(表达式2)语句2

Else if(表达式3)语句3

Else if(表达式m)语句m

Else语句n

此时,哪个表达式为真,后面的if语句就运行。如果表达式3成立,执行语句3。

在每个语句中,可以有多个语句,但需要大括号。

多重if语句和else if语句有什么区别

多个if由所有if判断

If else if只要满足条件,就不再判断后续的else if

诸如

a = 2;

if(a = = 1)c = 1;

if(a = = 2)c = 2;

if(a % 2 = = 0)c = 3;

最终结果c=3

a = 2;

if(a = = 1)c = 1;

else if(a = = 2)c = 2;

else if(a % 2 = = 0)c = 3;

最终结果c=2