C / C++中的“决策制定”有助于编写决策驱动的语句并根据某些条件执行特定的代码集。
在C / C++中, if-else-if阶梯帮助用户从多个选项中进行选择。 C / C++ if语句从上至下执行。一旦控制if的条件之一为true,就会执行与if关联的语句,其余的C else-if阶梯将被绕过。如果所有条件都不成立,则将执行最终的else语句。
句法:
if (condition)
statement 1;
else if (condition)
statement 2;
.
.
else
statement;
if-else-if阶梯的工作:
- 控制属于if块。
- 流程跳至条件1。
- 条件已测试。
- 如果Condition得出的结果为true,请转到步骤4。
- 如果Condition得出false,请转到步骤5。
- 当前块被执行。转到步骤7。
- 流程跳至条件2。
- 如果Condition得出的结果为true,请转到步骤4。
- 如果Condition得出false,请转到步骤6。
- 流程跳至条件3。
- 如果Condition得出的结果为true,请转到步骤4。
- 如果Condition得出false,则执行else块。转到步骤7。
- 退出if-else-if阶梯。
流程图if-else-if梯形图:
范例1:
C
// C program to illustrate nested-if statement
#include
int main()
{
int i = 20;
// Check if i is 10
if (i == 10)
printf("i is 10");
// Since i is not 10
// Check if i is 15
else if (i == 15)
printf("i is 15");
// Since i is not 15
// Check if i is 20
else if (i == 20)
printf("i is 20");
// If none of the above conditions is true
// Then execute the else statement
else
printf("i is not present");
return 0;
}
C++
// C++ program to illustrate if-else-if ladder
#include
using namespace std;
int main()
{
int i = 20;
// Check if i is 10
if (i == 10)
cout << "i is 10";
// Since i is not 10
// Check if i is 15
else if (i == 15)
cout << "i is 15";
// Since i is not 15
// Check if i is 20
else if (i == 20)
cout << "i is 20";
// If none of the above conditions is true
// Then execute the else statement
else
cout << "i is not present";
return 0;
}
C
// C program to illustrate nested-if statement
#include
int main()
{
int i = 25;
// Check if i is between 0 and 10
if (i >= 0 && i <= 10)
printf("i is between 0 and 10");
// Since i is not between 0 and 10
// Check if i is between 11 and 15
else if (i >= 11 && i <= 15)
printf("i is between 11 and 15");
// Since i is not between 11 and 15
// Check if i is between 16 and 20
else if (i >= 16 && i <= 20)
printf("i is between 16 and 20");
// Since i is not between 0 and 20
// It means i is greater than 20
else
printf("i is greater than 20");
}
C++
// C++ program to illustrate if-else-if ladder
#include
using namespace std;
int main()
{
int i = 25;
// Check if i is between 0 and 10
if (i >= 0 && i <= 10)
cout << "i is between 0 and 10" << endl;
// Since i is not between 0 and 10
// Check if i is between 11 and 15
else if (i >= 11 && i <= 15)
cout << "i is between 11 and 15" << endl;
// Since i is not between 11 and 15
// Check if i is between 16 and 20
else if (i >= 16 && i <= 20)
cout << "i is between 16 and 20" << endl;
// Since i is not between 0 and 20
// It means i is greater than 20
else
cout << "i is greater than 20" << endl;
}
输出:
i is 20
空运行示例1:
1. Program starts.
2. i is initialized to 20.
3. condition 1 is checked. 20 == 10, yields false.
4. condition 2 is checked. 20 == 15, yields false.
5. condition 3 is checked. 20 == 20, yields true.
5.a) "i is 20" gets printed.
6. "Outside if-else-if" gets printed.
7. Program ends.
范例2:
C
// C program to illustrate nested-if statement
#include
int main()
{
int i = 25;
// Check if i is between 0 and 10
if (i >= 0 && i <= 10)
printf("i is between 0 and 10");
// Since i is not between 0 and 10
// Check if i is between 11 and 15
else if (i >= 11 && i <= 15)
printf("i is between 11 and 15");
// Since i is not between 11 and 15
// Check if i is between 16 and 20
else if (i >= 16 && i <= 20)
printf("i is between 16 and 20");
// Since i is not between 0 and 20
// It means i is greater than 20
else
printf("i is greater than 20");
}
C++
// C++ program to illustrate if-else-if ladder
#include
using namespace std;
int main()
{
int i = 25;
// Check if i is between 0 and 10
if (i >= 0 && i <= 10)
cout << "i is between 0 and 10" << endl;
// Since i is not between 0 and 10
// Check if i is between 11 and 15
else if (i >= 11 && i <= 15)
cout << "i is between 11 and 15" << endl;
// Since i is not between 11 and 15
// Check if i is between 16 and 20
else if (i >= 16 && i <= 20)
cout << "i is between 16 and 20" << endl;
// Since i is not between 0 and 20
// It means i is greater than 20
else
cout << "i is greater than 20" << endl;
}
输出:
i is greater than 20
相关文章:
- C / C++中的决策
- C / C++ if else语句和示例
- C / C++ if语句和示例
- C / C++中的Switch语句
- C / C++中的Break语句
- 在C / C++中继续执行语句
- C / C++中的goto语句
- C / C++中的带有示例的return语句
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。