文章标题

来源:互联网 发布:2016淘宝开学季活动 编辑:程序博客网 时间:2024/05/16 10:34

# C语言if…else语句
—–
16340287
数据科学与计算机学院
—–
目录
[TOC]
—–

1.一个if ... else语句在C编程语言的语法:

if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
else
{
/* statement(s) will execute if the boolean expression is false */
}
如果布尔表达式的计算结果为true,则if块中的代码将被执行,否则else块的代码将被执行。
作为真正的C编程语言承担任何非零和非空值,如果它是零或空(null),那么它被假设为假值。
此处输入图片的描述

2.if … else例子
#include

int main ()
{
/* local variable definition */
int a = 100;

/* check the boolean condition */
if( a < 20 )
{
/* if condition is true then print the following */
printf(“a is less than 20\n” );
}
else
{
/* if condition is false then print the following */
printf(“a is not less than 20\n” );
}
printf(“value of a is : %d\n”, a);

return 0;
}
上面的代码编译和执行时,它会产生以下结果:

a is not less than 20;
value of a is : 100

3.if…else if…else语句需牢记几点。
a.An if can have zero or one else’s and it must come after any else if’s.

b. An if can have zero to many else if’s and they must come before the else.

c. Once an else if succeeds, none of the remaining else if’s or else’s will be tested.

在此强加一个公式
R^n - 1 = R^n - 1^n = (R-1)(R^(n-1) + R^(n-2) + … + R^0) = (R-1)R^(n-1) + (R-1) R^(n-2) + … + (R-1)R^0

0 0