
地 址:上海市虹口66号
电 话:18069431671
邮 箱:78158011@qq.com
C语言是判断一种广(guang)泛使用的计算(suan)机编程语言,它提供了(le)丰富的(de)判断运(yun)算(suan)符和数据类型(xing),使得程序员能够编写出高效、判断可移植的判断代码,在C语言中,判断判断语句是判断非常(chang)重要的一部分,它允许我们根据条件(jian)执行不同的判断代码块,本文将详细介绍如何在C语言中使用判断语句。判断(图片来源网络,判断侵删)
1、判断基本概念

在C语言中,判断判断语句主要有两种:if语句和switch语句,判断if语句用于(yu)实现多条件判断,判断而switch语句则用于(yu)实现多分支选择。判断

2、判断if语句

if语句的基本语法如下(xia):
if (条件表(biao)达式) { // 如果条(tiao)件为真,执行(xing)这里的代码} else { // 如果条件为假,执行这里的代码}条件(jian)表达(da)式可以是任何返回整数的表达式,包括算术表(biao)达式、关系表达式、逻辑表达(da)式等(deng),如果条件表达式的结果为非零(真(zhen)),则执行大括号内的代码;否则,执行else后面的代码。
注意:if语句可以嵌套使用(yong),即在一个if语句的内部再包含一个或多个if语句,这种情况下,外层的if语句称为主if语句,内(nei)层的if语句称为(wei)子if语句。
示例:
#include <stdio.h>int main() { int a = 10, b = 20; if (a > b) { printf("a大于b"); } else { printf("a小于等于b"); if (a == b) { printf("且a等于b"); } else { printf("且a不(bu)等(deng)于b"); } } return 0;}3、ifelse ifelse语句
if (条(tiao)件表达式(shi)1) { // 如果条件1为真,执行这里的代码} else if (条件表达式(shi)2) { // 如果条件(jian)1为假,且条件2为真,执行这里的代码} else if (条件表达式3) { // 如果条件1和条(tiao)件2都(dou)为假,且条件3为真,执行这里的代码} else { // 如果所有条件都为假,执行这里的代码}示例:
#include <stdio.h>int main() { int score = 85; if (score >= 90) { printf("优秀"); } else if (score >= 80) { printf("良好"); } else if (score >= 60) { printf("及格"); } else { printf("不及格"); } return 0;}switch语句用于实现多分支选择,其(qi)基本语法如(ru)下:
switch (表达式) { case 常量1: // 如果表达式的值等于常量(liang)1,执行这里的(de)代码 break; // 可选,用于跳出switch语句,如果没有(you)break,会继续执行后面的case代码块 case 常量2: // 如(ru)果表达式的值等于常量2,执行(xing)这里的代码 break; // 可选,用(yong)于跳出switch语句,如果没有break,会继续执行后(hou)面的case代码块 // ...其他case代码块... default: // 可(ke)选,当所有case都不匹配时,执行这里的代码}#include <stdio.h>#include <stdlib.h>#include <time.h>int main() { int num = rand() % 6 + 1; // 生成(cheng)1到6之间的随机数 printf("随机数是:%d", num); // 输出随机数的提示信息 switch (num) { // 根据随机数执行不同的操作 case 1: // 如果随机数等于1,输出“恭喜你,猜对了(le)!”并退出程序 printf("恭喜你(ni),猜对(dui)了!"); exit(0); // 退出程序,返回0表示正常(chang)结束程序运行过程,非0表示异常结束程序运行(xing)过(guo)程(如错误) case 2: // 如果随机数等于2,输出“很遗憾,猜错了。”并退出程序(这(zhe)里省略了exit(0)) printf("很遗憾,猜错了。"); // 输出猜错的提示信息(这里省略了exit(0)) // ...其他case代码块... // ...default代码块... } // end of switch statement } // end of main function return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return statement } // end of program execution return