zoukankan      html  css  js  c++  java
  • [C++基本语法:从菜鸟变成大佬系列,就像1,2,3那么简单](六):C++的决策声明(判断)

    决策声明是什么?

    决策结构要求程序员指定一个或多个要由程序评估或测试的条件,以及在条件被确定为真时要执行的一个或多个语句,以及可选的,如果条件要执行的其他语句被认定是假的。

    以下是大多数编程语言中的典型决策结构的一般形式

    C++编程语言提供以下类型的决策制定语句。

    类型 声明和说明
    if 语句 'if'语句由一个bool表达式后跟一个或多个语句组成
    if...else 语句 'if'语句后面可以跟一个可选的'else'语句,该语句在bool表达式为false时执行
    switch 语句 'switch'语句允许根据值列表测试变量的相等性
    嵌套if语句 您可以在另一个“if”或“else if”语句中使用一个“if”或“else if”语句。
    嵌套switch语句 您可以在另一个'switch'语句中使用一个'switch'语句。

    If 语句

    • 句法

    C++中if语句的语法是 -

    1 if(boolean_expression) {
    2    // statement(s) will execute if the boolean expression is true
    3 }
    View Code

    如果布尔表达式的计算结果为true,那么将执行if语句中的代码块。如果bool表达式的计算结果为false,那么将执行if语句结束后(在结束大括号之后)的第一组代码。

    • 流程图

    View Code

    编译并执行上述代码时,会产生以下结果

    View Code

    For English visitors,please click here

    If...else 语句

    一个如果语句可以跟着一个可选的其他语句,当布尔表达式是假的,其执行。

    • 句法

    C ++中if ... else语句的语法是

    1 if(boolean_expression) {
    2    // statement(s) will execute if the boolean expression is true
    3 } else {
    4   // statement(s) will execute if the boolean expression is false
    5 }
    View Code

    如果bool表达式的计算结果为true,那么将执行if代码,否则将执行代码

    • 流程图

     


     1 #include <iostream>
     2 using namespace std;
     3  
     4 int main () {
     5    // local variable declaration:
     6    int a = 100;
     7    // check the boolean condition
     8    if( a < 20 ) {
     9       // if condition is true then print the following
    10       cout << "a is less than 20;" << endl;
    11    } else {
    12       // if condition is false then print the following
    13       cout << "a is not less than 20;" << endl;
    14    }
    15    cout << "value of a is : " << a << endl;
    16    return 0;
    17 }
    View Code

    编译并执行上述代码时,会产生以下结果 -

    1 a is not less than 20;
    2 value of a is : 100
    View Code

    if ... else...if ... else

    一个如果语句可以跟着一个可选否则,如果...别的语句,使用单如果...否则,如果声明,这是非常有用的,以测试各种条件。

    当使用if,else if,else语句时,要记住几点。

    • 一个if可以有零个或一个其他的,它必须在任何其他if之后。

    • 如果是的话,if可以有零到多个,并且它们必须在else之前。

    • 一旦其他成功,如果其他人将被测试,否则他没有留下其他人。

    • 句法

    C++中if ... else if ... else语句的语法是

    1 if(boolean_expression 1) {
    2    // Executes when the boolean expression 1 is true
    3 } else if( boolean_expression 2) {
    4    // Executes when the boolean expression 2 is true
    5 } else if( boolean_expression 3) {
    6    // Executes when the boolean expression 3 is true
    7 } else {
    8    // executes when the none of the above condition is true.
    9 }
    View Code

     1 #include <iostream>
     2 using namespace std;
     3 int main () {
     4    // local variable declaration:
     5    int a = 100;
     6    // check the boolean condition
     7    if( a == 10 ) {
     8       // if condition is true then print the following
     9       cout << "Value of a is 10" << endl;
    10    } else if( a == 20 ) {
    11       // if else if condition is true
    12       cout << "Value of a is 20" << endl;
    13    } else if( a == 30 ) {
    14       // if else if condition is true 
    15       cout << "Value of a is 30" << endl;
    16    } else {
    17       // if none of the conditions is true
    18       cout << "Value of a is not matching" << endl;
    19    }
    20    cout << "Exact value of a is : " << a << endl;
    21    return 0;
    22 }
    View Code

    编译并执行上述代码时,会产生以下结果 -

    1 Value of a is not matching
    2 Exact value of a is : 100
    View Code

    For English visitors, please click here

    Switch 语句

    开关语句允许一个变量来针对值的列表平等进行测试。每个值称为一个case,并且针对每种情况检查打开的变量。

    • 句法

    C++switch语句的语法如下

     1 switch(expression) {
     2    case constant-expression  :
     3       statement(s);
     4       break; //optional
     5    case constant-expression  :
     6       statement(s);
     7       break; //optional
     8   
     9    // you can have any number of case statements.
    10    default : //Optional
    11       statement(s);
    12 }
    View Code

    以下规则适用于switch语句

    • switch语句中使用表达式必须具有整数或枚举类型,或者是类类型,其中类具有单个转换函数为整数或枚举类型。

    • 您可以在交换机中包含任意数量的case语句。每个案例后跟要与之比较的值和冒号。

    • case constant-expression必须与switch中的变量具有相同的数据类型,并且必须是常量或文字。

    • 当接通的变量等于大小写时,该大小写之后的语句将一直执行,直到达到break语句。

    • 当达到break语句时,交换机终止,控制流跳转到switch语句后面的下一行。

    • 并非每个案例都需要包含休息时间。如果没有出现中断,则控制流将落到后续情况,直到达到中断。

    • 一个开关语句可以有一个可选的默认情况下,它必须出现在开关的结束。当没有任何情况属实时,默认情况可用于执行任务。默认情况下不需要中断。

    • 流程图

    C ++开关语句

     1 #include <iostream>
     2 using namespace std;
     3  
     4 int main () {
     5    // local variable declaration:
     6    char grade = 'D';
     7 
     8    switch(grade) {
     9       case 'A' :
    10          cout << "Excellent!" << endl; 
    11          break;
    12       case 'B' :
    13       case 'C' :
    14          cout << "Well done" << endl;
    15          break;
    16       case 'D' :
    17          cout << "You passed" << endl;
    18          break;
    19       case 'F' :
    20          cout << "Better try again" << endl;
    21          break;
    22       default :
    23          cout << "Invalid grade" << endl;
    24    }
    25    cout << "Your grade is " << grade << endl;
    26  
    27    return 0;
    28 }
    View Code

    这将产生以下结果

    1 You passed
    2 Your grade is D
    View Code

    嵌套if 语句

    嵌套 if-else语句总是合法的,这意味着你可以在另一个if或else if语句中使用if或else if语句。

    • 句法

    嵌套if 语句的语法如下 -

    1 if( boolean_expression 1) {
    2    // Executes when the boolean expression 1 is true
    3    if(boolean_expression 2) {
    4       // Executes when the boolean expression 2 is true
    5    }
    6 }
    View Code

    您可以使用与嵌套if语句类似的方式嵌套if else


     1 #include <iostream>
     2 using namespace std;
     3  
     4 int main () {
     5    // local variable declaration:
     6    int a = 100;
     7    int b = 200;
     8  
     9    // check the boolean condition
    10    if( a == 100 ) {
    11       // if condition is true then check the following
    12       if( b == 200 ) {
    13          // if condition is true then print the following
    14          cout << "Value of a is 100 and b is 200" << endl;
    15       }
    16    }
    17    cout << "Exact value of a is : " << a << endl;
    18    cout << "Exact value of b is : " << b << endl;
    19  
    20    return 0;
    21 }
    View Code

    编译并执行上述代码时,会产生以下结果 -

    1 Value of a is 100 and b is 200
    2 Exact value of a is : 100
    3 Exact value of b is : 200
    View Code

    Switch 语句

    可以将switch语句作为外部开关的语句序列的一部分。即使内部和外部交换机的大小写常量包含公共值,也不会产生冲突。

    C++指定允许至少256级嵌套用于switch语句。

    • 句法

    嵌套switch 语句的语法如下

     1 switch(ch1) {
     2    case 'A': 
     3       cout << "This A is part of outer switch";
     4       switch(ch2) {
     5          case 'A':
     6             cout << "This A is part of inner switch";
     7             break;
     8          case 'B': // ...
     9       }
    10       break;
    11    case 'B': // ...
    12 }
    View Code

     1 #include <iostream>
     2 using namespace std;
     3  
     4 int main () {
     5    // local variable declaration:
     6    int a = 100;
     7    int b = 200;
     8  
     9    switch(a) {
    10       case 100: 
    11          cout << "This is part of outer switch" << endl;
    12          switch(b) {
    13             case 200:
    14                cout << "This is part of inner switch" << endl;
    15          }
    16    }
    17    cout << "Exact value of a is : " << a << endl;
    18    cout << "Exact value of b is : " << b << endl;
    19  
    20    return 0;
    21 }
    View Code

    这将产生以下结果

    1 This is part of outer switch
    2 This is part of inner switch
    3 Exact value of a is : 100
    4 Exact value of b is : 200
    View Code

    ?: 运算符
    我们已经介绍了条件运算符"?:"在前一章中,如果...其他语句。它有以下一般形式

    1 Exp1 ?Exp2 : Exp3;
    View Code

    Exp1、Exp2 和 Exp3 是表达式。请注意冒号的使用和放置。

    "?"表达式的值如下所示:计算 Exp1。如果为 true,则计算 Exp2 并将成为整个"?"表达式的值。如果 Exp1 为 false,则计算 Exp3,其值将成为表达式的值。

    For English vistor, please click here

  • 相关阅读:
    leetcode[104]Maximum Depth of Binary Tree
    leetcode[105]Construct Binary Tree from Preorder and Inorder Traversal
    leetcode[106]Construct Binary Tree from Inorder and Postorder Traversal
    leetcode[107]Binary Tree Level Order Traversal II
    leetcode[108]Convert Sorted Array to Binary Search Tree
    iOS开发的各种开源库、开源资料、开源技术等等的索引库
    Graphics API Debugger
    ps滤镜效果的原理
    ps滤镜中英文对照
    ps原理
  • 原文地址:https://www.cnblogs.com/ssf-lrk/p/11231071.html
Copyright © 2011-2022 走看看