在 C 语言中,您可以控制程序的执行流程。
程序能够决定下一步应该做什么是基于您设置的某些预定义条件的状态。
程序会根据条件是否满足来决定下一步应该做什么。
满足特定条件时做一件事,不满足特定条件时做另一件事,这种行为叫做控制流。
例如,您可能希望仅在特定条件下执行某个操作。你可能想在完全不同的条件下执行另一个动作。或者,当您设置的特定条件不满足时,您可能想要执行另一个完全不同的操作。
为了能够完成上述所有操作并控制程序的流程,您需要使用 if 语句。
在本文中,您将学习有关if语句的所有知识——它的语法和如何使用它的示例,以便您能够理解它的工作原理。
您还将了解 if else 语句——即添加到 if 语句中的 else 语句,以增加程序灵活性。
此外,您还将了解当您想要为条件添加更多选择时使用的else if语句。
C 语言中的 if 语句是什么?
if 语句也称为条件语句,用于决策。它就像一个岔路口或一个分支。
条件语句根据发生的检查或比较的结果执行特定的操作。
因此,总而言之,if 语句根据条件做出一个决定。
条件是布尔表达式。布尔表达式只能是两个值之一—— true 或 false。
如果给定条件的计算结果为 true,则执行 if 块内的代码。
如果给定条件的计算结果为 false,则忽略并跳过 if 块内的代码。
如何在 C 语言中创建 if 语句——初学者的语法分解
C 语言中 if 语句的一般语法如下:
if (condition) {
// run this code if condition is true
}
让我们来分析一下:
- 使用 if 关键字开始 if 语句。
- 在圆括号内,包含一个需要检查和计算的条件,它总是一个布尔表达式。这个条件只计算为 true 或 false。
- if 块由一组花括号
{}
表示。 - 在 if 块中,有代码行-确保代码是缩进的,这样更容易阅读。
if 语句示例
接下来,让我们看一个 if 语句的实际例子。
我将创建一个名为 age 的变量,它将保存一个整数值。
然后我将提示用户输入他们的年龄并将答案存储在变量 age 中。
然后,我将创建一个条件来检查变量 age 中包含的值是否小于 18。
如果满足该条件,则在控制台上打印一条消息,让用户知道要继续执行的话至少需要年满 18 岁。
#include <stdio.h>
int main(void) {
// variable age
int age;
// prompt user to enter their age
printf("Please enter your age: ");
// store user's answer in the variable
scanf("%i", &age);
// check if age is less than 18
// if it is, then and only then, print a message to the console
if (age < 18) {
printf("You need to be over 18 years old to continue\n");
}
}
我使用 gcc conditionals.c
命令编译代码,其中 gcc 是 C 编译器的名称,conditionals.c 是包含 C 源代码的文件的名称。
然后,运行代码,输入 ./a.out
。
当询问我的年龄时,我输入 16 并得到以下输出:
#output
Please enter your age: 16
You need to be over 18 years old to continue
条件 (age < 18) 的计算结果为 true,因此执行 if 块中的代码。
重新编译并重新运行该程序。
这一次,当询问年龄时,我输入 28 并得到以下输出:
#output
Please enter your age: 28
因为条件的计算结果为 false,因此 if 块的主体被跳过。
由于也没有指定在用户年龄大于18岁的情况下应该发生什么,因此没有输出任何信息。
可以写另一个 if 语句,如果用户的年龄大于 18,打印一条消息到控制台,这样代码就更清晰了:
#include <stdio.h>
int main(void) {
// variable age
int age;
// prompt user to enter their age
printf("Please enter your age: ");
// store user's answer in the variable
scanf("%i", &age);
// check if age is less than 18
// if it is, print a message to the console
if (age < 18) {
printf("You need to be over 18 years old to continue\n");
}
// check if age is greater than 18
// if it is, print a message to the console
if (age > 18) {
printf("You are over 18 so you can continue \n");
}
}
编译并运行代码,当提示输入年龄时,再次输入 28:
#output
Please enter your age: 28
You are over 18 so you can continue
这段代码可以工作了。有一种更好的方法来编写它,您将在下一节中看到如何做到这一点。
C 语言中的 if else 语句
多个 if 语句本身是没有帮助的——特别是当程序变得越来越大时。
因此,出于这个原因,if 语句要伴随一个 else 语句。
if else 语句本质上意味着“如果此条件为 true,则执行以下操作,否则则执行其他操作”。
如果括号内的条件计算结果为 true,则 if 块内的代码将执行。但是,如果该条件的计算结果为 false,则将执行 else 块中的代码。
else 关键字是当 if 条件为 false 且 if 块内的代码不运行时的解决方案。它提供了一个替代方案。
一般语法如下所示:
if (condition) {
// run this code if condition is true
} else {
// if the condition above is false run this code
}
if else 语句示例
现在,让我们重新审视前面的两个单独的 if 语句的示例:
#include <stdio.h>
int main(void) {
int age;
printf("Please enter your age: ");
scanf("%i", &age);
if (age < 18) {
printf("You need to be over 18 years old to continue\n");
}
if (age > 18) {
printf("You are over 18 so you can continue \n");
}
}
让我们改用 if else 语句重写它:
#include <stdio.h>
int main(void) {
int age;
printf("Please enter your age: ");
scanf("%i", &age);
// if the condition in the parentheses is true the code inside the curly braces will execute
// otherwise it is skipped
// and the code in the else block will execute
if (age < 18) {
printf("You need to be over 18 years old to continue\n");
} else {
printf("You are over 18 so you can continue \n");
}
}
如果条件为真,则运行 if 块中的代码:
#output
Please enter your age: 14
You need to be over 18 years old to continue
如果条件为假,则跳过 if 块中的代码,转而运行 else 块中的代码:
#output
Please enter your age: 45
You are over 18 so you can continue
什么是 else if 语句?
当您想要有多个条件可供选择时会发生什么?
如果您希望在多个选项之间进行选择,并希望在操作上有更多的变化,那么您可以引入一个 else if 语句。
else if 语句本质上意味着“如果条件为真,则执行对应操作。如果不是,则改为执行此操作。如果以上都不为真,并且所有其他方法均失败,则最后执行此操作。”
一般语法如下所示:
if (condition) {
// if condition is true run this code
} else if(another_condition) {
// if the above condition was false and this condition is true,
// run the code in this block
} else {
// if the two above conditions are false run this code
}
else if 语句示例
让我们看看 else if 语句是如何工作的。
假设你有以下例子:
#include <stdio.h>
int main(void) {
int age;
printf("Please enter your age: ");
scanf("%i", &age);
if (age < 18) {
printf("You need to be over 18 years old to continue\n");
} else if (age < 21) {
printf("You need to be over 21\n");
} else {
printf("You are over 18 and older than 21 so you can continue \n");
}
}
如果第一个 if 语句为真,则块的其余部分将不会运行:
#output
Please enter your age: 17
You need to be over 18 years old to continue
如果第一个 if 语句为假,则程序继续执行下一个条件。
如果下一个条件为真,则执行 else if 块中的代码,并且其余部分不运行:
#output
Please enter your age: 20
You are need to be over 21
如果前面的两个条件都为假,那么是执行最后的 else 块:
#output
Please enter your age: 22
You are over 18 and older than 21 so you can continue
总结
现在您已经了解了 C 语言中 if、if else 和 else if 语句的基础知识!
希望这篇文章对您有帮助。