C语言教程:if-else语句(执行基于条件为true或false的操作)

2018-10-0608:18:39编程语言入门到精通Comments8,085 views1字数 2118阅读模式

C语言中的if语句用于基于条件执行操作。通过使用if-else语句,您可以执行基于条件为truefalse的操作。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6392.html

使用C语言中的if语句有很多形式:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6392.html

  • if语句
  • if-else语句
  • if else-if语句并排
  • 嵌套if

1. if语句

if语句的语法如下 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6392.html

if(expression){  
    //code to be executed  
}
C

C语言中的if语句的流程图,如下所示 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6392.html

C语言教程:if-else语句(执行基于条件为true或false的操作)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6392.html

我们来看一个简单的c语言if语句的示例代码,创建一个源文件:if-statement.c,代码如下所示 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6392.html

#include<stdio.h>  
#include<conio.h>  
void main() {
    int number = 0;

    printf("enter a number:");
    scanf("%d", &number);

    if (number % 2 == 0) {
        printf("%d is even number\n", number);
    }

}
C

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6392.html

enter a number:100
100 is even number

2.if-else语句

如果conditiontruefalse都要执行对应代码块,则可使用C语言中的if-else语句来实现。if-else语句的语法如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6392.html

if(expression){  
//code to be executed if condition is true  
}else{  
//code to be executed if condition is false  
}
C

C语言中的if-else语句的流程图,如下所示 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6392.html

C语言教程:if-else语句(执行基于条件为true或false的操作)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6392.html

我们来看一个简单的C语言if-else语句的示例代码,创建一个源文件:if-else-statement.c,代码如下所示 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6392.html

#include<stdio.h>  
#include<conio.h>  
void main() {
    int number = 0;

    printf("enter a number:");
    scanf("%d", &number);

    if (number % 2 == 0) {
        printf("%d is even number\n", number);
    }
    else {
        printf("%d is odd number\n", number);
    }    
}
C

执行上面示例代码,第一次执行得到以下结果(输入整数:20),文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6392.html

enter a number:20
20 is even number
请按任意键继续. . .
Shell

第二次执行得到以下结果(输入整数:55),文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6392.html

enter a number:55
55 is odd number
请按任意键继续. . .
Shell

3.if else-if语句

if else-if语句用于从多个条件执行一个代码。 if else-if语句的语法如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6392.html

if(condition1){  
//code to be executed if condition1 is true  
}else if(condition2){  
//code to be executed if condition2 is true  
}else if(condition3){  
//code to be executed if condition3 is true  
}  
...  
else{  
//code to be executed if all the conditions are false  
}
C

C语言中的if else-if语句的流程图 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6392.html

C语言教程:if-else语句(执行基于条件为true或false的操作)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6392.html

下面给出了C语言中if-else-if语句的例子,创建一个源文件:if-ifelse-statment.c,其代码如下所示 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6392.html

#include<stdio.h>  

void main() {
    int number = 0;

    printf("enter a number:");
    scanf("%d", &number);

    if (number == 10) {
        printf("number is equals to 10\n");
    }else if (number == 50) {
        printf("number is equal to 50\n");
    }else if (number == 100) {
        printf("number is equal to 100\n");
    }else {
        printf("number is not equal to 10, 50 or 100\n");
    }
}
C

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6392.html

enter a number:88
number is not equal to 10, 50 or 100
Shell

4.嵌套if

嵌套if语句就是在一个if语句中嵌套一个或多个if语句,创建一个源文件:nested_if.c,参考如下示例代码:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6392.html

#include<stdio.h>  

void main() {
    int score = 0;
    printf("enter a score:");
    scanf("%d", &score);

    if (score >= 60) { // 下面是嵌套if-else语句
        if (score <= 80) {
            printf("分数大于60小于80,中等水平\n");
        }else if (score > 80 && score < 90) {
            printf("分数大于60小于80,成绩良好\n");
        }else{// 大于 90 以上
            printf("分数大于90,成绩优秀\n");
        }
    }else {
        printf("分数小于 60 分,不及格~!\n");
    }
}
C

执行上面查询语句,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6392.html

enter a score:90
分数大于90,成绩优秀
请按任意键继续. . .
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/6392.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/6392.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定