亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> C语言

C++ if, if...else 和嵌套 if...else

C++ if, if...else 和嵌套 if...else

在本教程中,我们将通过示例了解 if...else 语句来创建决策程序。

在计算机编程中,我们使用 if...else 语句在特定条件下运行一段代码,在不同条件下运行另一段代码。

例如,根据学生获得的分数分配等级(A、B、C)。


if...else的三种形式 C++ 中的语句。

  1. if 声明
  2. if...else 声明
  3. if...else if...else 声明

C++ if 语句

if 的语法 声明是:

if (condition) {
  // body of if statement
}

if 语句评估 condition 括号内 ( ) .

注意: { }里面的代码 是 if 的主体 声明。

<图>

示例1:C++ if 语句

// Program to print positive number entered by the user
// If the user enters a negative number, it is skipped

#include <iostream>
using namespace std;

int main() {

  int number;

  cout << "Enter an integer: ";
  cin >> number;

  // checks if the number is positive
  if (number > 0) {
    cout << "You entered a positive integer: " << number << endl;
  }

  cout << "This statement is always executed.";

  return 0;
}

输出 1

Enter an integer: 5
You entered a positive number: 5
This statement is always executed.

当用户输入5 , 条件 number > 0 被评估为 true 以及 if 正文中的语句 被执行。

输出 2

Enter a number: -5
This statement is always executed.

当用户输入-5 , 条件 number > 0 被评估为 false 以及 if 正文中的语句 没有被执行。


C++ if...else

if 语句可以有一个可选的 else 条款。它的语法是:

if (condition) {
  // block of code if condition is true
}
else {
  // block of code if condition is false
}

if..else 语句评估 condition 括号内。

<图>

如果 condition 评估 true ,

如果 condition 评估 false ,


示例 2:C++ if...else 语句

// Program to check whether an integer is positive or negative
// This program considers 0 as a positive number

#include <iostream>
using namespace std;

int main() {

  int number;

  cout << "Enter an integer: ";
  cin >> number;

  if (number >= 0) {
    cout << "You entered a positive integer: " << number << endl;
  }
  else {
    cout << "You entered a negative integer: " << number << endl;
  }

  cout << "This line is always printed.";

  return 0;
}

输出 1

Enter an integer: 4
You entered a positive integer: 4.
This line is always printed.

在上面的程序中,我们有条件 number >= 0 .如果我们输入大于或等于 0 的数字 ,然后条件评估 true .

在这里,我们输入 4 .所以,条件是 true .因此,if 主体内的语句 被执行。

输出 2

Enter an integer: -4
You entered a negative integer: -4.
This line is always printed.

在这里,我们输入 -4 .所以,条件是 false .因此,else 主体内的语句 被执行。


C++ if...else...else if 语句

if...else 语句用于在两个备选方案中执行代码块。但是,如果我们需要在两个以上的选择之间做出选择,我们使用 if...else if...else 声明。

if...else if...else 的语法 声明是:

if (condition1) {
  // code block 1
}
else if (condition2){
  // code block 2
}
else {
  // code block 3
}

在这里,

<图>

注意: else if 可以有多个 声明但只有一个 ifelse 声明。


示例 3:C++ if...else...else if

// Program to check whether an integer is positive, negative or zero

#include <iostream>
using namespace std;

int main() {

  int number;

  cout << "Enter an integer: ";
  cin >> number;

  if (number > 0) {
    cout << "You entered a positive integer: " << number << endl;
  } 
  else if (number < 0) {
    cout << "You entered a negative integer: " << number << endl;
  } 
  else {
    cout << "You entered 0." << endl;
  }

  cout << "This line is always printed.";

  return 0;
}

输出 1

Enter an integer: 1
You entered a positive integer: 1.
This line is always printed.

输出 2

Enter an integer: -2
You entered a negative integer: -2.
This line is always printed.

输出 3

Enter an integer: 0
You entered 0.
This line is always printed.

在这个程序中,我们从用户那里获取一个数字。然后我们使用 if...else if...else 梯子来检查数字是正数、负数还是零。

如果数字大于0if里面的代码 块被执行。如果数字小于0else if里面的代码 块被执行。否则,else里面的代码 块被执行。


C++ 嵌套 if...else

有时,我们需要使用 if 另一个 if 中的语句 陈述。这称为嵌套 if 声明。

把它想象成多层 if 陈述。有一个第一个,外部 if 声明,里面是另一个,内部的 if 陈述。它的语法是:

// outer if statement
if (condition1) {

  // statements

  // inner if statement
  if (condition2) {
    // statements
  }
}

注意事项:


示例 4:C++ 嵌套 if

// C++ program to find if an integer is positive, negative or zero
// using nested if statements

#include <iostream>
using namespace std;

int main() {

  int num;
    
  cout << "Enter an integer: ";  
   cin >> num;    

  // outer if condition
  if (num != 0) {
        
    // inner if condition
    if (num > 0) {
      cout << "The number is positive." << endl;
    }
    // inner else condition
    else {
      cout << "The number is negative." << endl;
    }  
  }
  // outer else condition
  else {
    cout << "The number is 0 and it is neither positive nor negative." << endl;
  }

  cout << "This line is always printed." << endl;

  return 0;
}

输出 1

Enter an integer: 35
The number is positive.
This line is always printed.

输出 2

Enter an integer: -35
The number is negative.
This line is always printed.

输出 3

Enter an integer: 0
The number is 0 and it is neither positive nor negative.
This line is always printed.

在上面的例子中,

注意: 如您所见,嵌套的 if...else 让你的逻辑变得复杂。如果可能,您应该始终尽量避免嵌套 if...else .


if...else 的主体,只有一个语句

如果 if...else 的正文 只有一个语句,你可以省略 { } 在节目中。比如你可以替换

int number = 5;

if (number > 0) {
  cout << "The number is positive." << endl;
}
else {
  cout << "The number is negative." << endl;
}

int number = 5;

if (number > 0)
  cout << "The number is positive." << endl;
else
  cout << "The number is negative." << endl;

两个程序的输出是一样的。

注意: 虽然没有必要使用 { } 如果 if...else 的主体 只有一个语句,使用 { } 使您的代码更具可读性。


更多决策

在某些情况下,三元运算符 可以替换 if...else 陈述。要了解更多信息,请访问 C++ 三元运算符。

如果我们需要根据给定的测试条件在多个备选方案之间做出选择,switch 可以使用语句。要了解更多信息,请访问 C++ 开关。


查看这些示例以了解更多信息:

判断数字是偶数还是奇数的C++程序

C++程序检查一个字符是元音还是辅音。

求三个数中最大数的C++程序


C语言

  1. C# if, if...else, if...else if 和嵌套 if 语句
  2. C++ while 和 do...while 循环
  3. C++ 中断语句
  4. C++ 类和对象
  5. C++ 内存管理:新建和删除
  6. C++ 公共、受保护和私有继承
  7. C++朋友函数和朋友类
  8. Python 语句、缩进和注释
  9. Java 嵌套和内部类
  10. C++ 日期和时间
  11. C++ 重载(运算符和函数)
  12. C++ 文件和流