在C语言编程中,你有能力控制程序的流程。
特别是,程序能够决定它下一步应该做什么。而这些决定是基于你设定的某些预定义条件的状态。
程序将根据条件是否得到满足来决定下一步应该做什么。
如果一个特定的条件得到满足,就做一件事,如果这个特定的条件没有得到满足,就做另一件事,这种行为称为控制流。
例如,你可能只想在一个特定条件下执行一个动作。而你可能想在一个完全不同的条件下执行另一个动作。或者,你可能想在你设置的那个特定条件没有得到满足时,执行另一个完全不同的动作。
为了能够做到上述所有的事情,并控制程序的流程,你将需要使用if 语句。
在这篇文章中,你将了解关于if 语句的所有信息--它的语法和如何使用它的例子,以便你能理解它的工作原理。
你还将了解到if else 语句--也就是添加到if 语句中的else 语句,以增加程序的灵活性。
此外,你还将学习else if 语句,以便在你想为你的条件增加更多的选择。
以下是我们要讲的内容。
什么是C语言中的if 语句?
if 语句也被称为条件性语句,用于决策。它是道路上的一个分叉口或一个分支。
条件语句根据发生的检查或比较的结果采取特定的行动。
因此,总的来说,if 语句是根据一个条件做出决定的。
该条件是一个布尔表达式。一个布尔表达式只能是两个值中的一个--真或假。
如果给定的条件被评估为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 语句的基本意思是:"if 这个条件为真,做下面的事情,else 代替做这个事情"。
如果括号内的条件评估为true ,if 块内的代码将被执行。然而,如果该条件的值为false ,则else 块内的代码将被执行。
else 关键字是当if 条件为假时,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");
}
}
如果条件是true ,则运行if 块中的代码。
#output
Please enter your age: 14
You need to be over 18 years old to continue
如果条件是false ,则跳过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 语句的基本知识!
我希望你觉得这篇文章对你有帮助。
要学习更多关于C语言的知识,请查看以下免费资源。
非常感谢您的阅读,祝您编码愉快 :)