求知 文章 文库 Lib 视频 iPerson 课程 认证 咨询 工具 讲座 Model Center   Code  
会员   
要资料
 
 

C语言教程
C语言历史
C语言特点
C语言VS开发环境安装
C语言第一个程序
C语言程序执行流程
C语言printf()和scanf()函数
C语言变量
C语言数据类型
C语言关键字
C语言运算符
C语言注释
C语言转义序列
C语言常量
C语言控制语句
C语言if-else语句
C语言switch语句
C语言循环
C语言do-while循环
C语言while循环
C语言for循环
C语言break语句
C语言continue语句
C语言goto语句
C语言类型转换
C语言函数
C语言函数
C语言通过值和引用函数
C语言递归
C语言存储分类
C语言数组
C语言数组
C语言二维数组
C语言将数组传递给函数
C语言指针
C语言指针
C语言指针的指针
C语言指针算术运算
C语言字符串
C语言字符串
C语言gets()和puts()函数
C语言字符串函数
C语言strlen()函数
C语言strcpy()函数
C语言strcat()函数
C语言strcmp()函数
C语言strrev()函数
C语言strlwr()函数
C语言strupr()函数
结构联合体
C语言结构体
C语言结构体数组
C语言结构体嵌套
C语言联合体
文件处理
C语言文件处理
C语言fprintf()和fscanf()函数
C语言fputc()和fgetc()函数
C语言fputs()和fgets()函数
C语言fseek()函数
C语言rewind()函数
C语言ftell()函数
预处理器
C语言预处理器指令
C语言宏
C语言#include指令
C语言#define指令
C语言#undef指令
C语言#ifdef指令
C语言#ifndef指令
C语言#if指令
C语言#error指令
C语言#pragma指令
其它杂项
C语言数学函数
C语言命令行参数
 
 

C语言if-else语句
1231 次浏览
7次  

C语言中的if语句用于基于条件执行操作。通过使用if-else语句,您可以执行基于条件为true或false的操作。

使用C语言中的if语句有很多形式:

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

1. if语句

if语句的语法如下 -

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

C语言中的if语句的流程图,如下所示 -

我们来看一个简单的c语言if语句的示例代码,创建一个源文件:if-statement.c,代码如下所示 -

#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);
}
}

 

 

执行上面示例代码,得到以下结果 -

enter a number:100
100 is even number

2.if-else语句

如果condition为true或false都要执行对应代码块,则可使用C语言中的if-else语句来实现。if-else语句的语法如下:

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

 

C语言中的if-else语句的流程图,如下所示 -

我们来看一个简单的C语言if-else语句的示例代码,创建一个源文件:if-else-statement.c,代码如下所示 -

#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);
}
}

 

执行上面示例代码,第一次执行得到以下结果(输入整数:20),

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

 

 

第二次执行得到以下结果(输入整数:55),

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

3.if else-if语句

if else-if语句用于从多个条件执行一个代码。 if else-if语句的语法如下:

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语言中的if else-if语句的流程图 -

下面给出了C语言中if-else-if语句的例子,创建一个源文件:if-ifelse-statment.c,其代码如下所示 -

#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");
}
}

 

 

执行上面示例代码,得到以下结果 -

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

4.嵌套if

嵌套if语句就是在一个if语句中嵌套一个或多个if语句,创建一个源文件:nested_if.c,参考如下示例代码:

#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");
}
}

 

 

执行上面查询语句,得到以下结果 -

#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");
}
}

 

enter a score:90

分数大于90,成绩优秀

请按任意键继续. . .

Shell

 

 


您可以捐助,支持我们的公益事业。

1元 10元 50元





认证码: 验证码,看不清楚?请点击刷新验证码 必填



1231 次浏览
7次
 捐助