if-else 语句是一种条件语句,用于在程序中做出基于条件的决策,语法如下:if (condition) { // if condition is true, execute this block }else { // if condition is false, execute this block }

C 语言中的 if-else 语句
if-else 语句是 C 语言中用于条件判断和执行不同代码块的控制结构。
语法:
<code class="c">if (condition) {
// if condition is true, execute this block
} else {
// if condition is false, execute this block
}</code>含义:
立即学习“C语言免费学习笔记(深入)”;
-
condition是一个布尔表达式,它计算为真或假。 - 如果
condition为真,则执行if块中的代码。 - 如果
condition为假,则执行else块中的代码。
用法:
- if-else 语句用于在程序中做出基于条件的决策。
- 如果条件为真,则执行特定的代码块,否则执行另一个代码块。
- 例如,以下代码检查一个数字是否是奇数或偶数:
<code class="c">int num = 5;
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}</code>嵌套 if-else 语句:
if-else 语句可以嵌套,创建更复杂的条件逻辑。例如,以下代码检查一个数字是正数、负数还是零:
<code class="c">int num = -5;
if (num > 0) {
printf("%d is positive.\n", num);
} else if (num < 0) {
printf("%d is negative.\n", num);
} else {
printf("%d is zero.\n", num);
}</code>











