学习《C++ Primer Plus》08

                分支语句和逻辑运算符

if语句

if(test-condition)

statement

如果测试条件为true,则程序执行statement,后者可以是一条语句也可以是语句块。和循环测试条件一样,if测试条件也将被强制转换为bool值。

if else语句

if(test-condition)

statement1

else

statement2

if else if else结构

计算机程序可以提供两个以上的选择,可以将C++的if else语句进行扩展来满足这种需求。else之后应是一条语句,也可以是语句块。

//ifelseif.cpp--using if else if else

#include<iostream>

const int Fave = 27;

int main()

{

    using namespace std;

    int n;

 

    cout << "Enter a number in the range 1-100 to find "

            "my favorite number: ";

    do {

        cin >> n;

        if (n < Fave)

             cout << "too low--guess again:";

        else if (n > Fave)

             cout << "too high--guess again:";

        else

             cout << Fave<< " is right\n";

    } while (n != Fave);

    cin.get();

    cin.get();

    return 0;

}

很多程序猿将更直观的表达式variable==value反转为value==variable,以此来捕获将相等运算符写为赋值运算符的错误。

if (3 == n)

        cout << n;

 

 

 

逻辑表达式

或 or ||

||优先级比关系运算符低,因此不需要在这些表达式中使用括号。C++规定,||运算符是个顺序点,也就是说,先运行左边的值。如果左边表达式为true,则C++不会取判断右侧的表达式。

与 and &&

&&优先级低于关系运算符,先判断左侧,左侧为false不再判断右侧。

#include<iostream>

const int Arsize = 6;

int main()

{

    using namespace std;

    float naap[Arsize];

    cout << "Enter theNAAQs"

        "of\n your neighbors."

        "Progam terminates when you make\n" << Arsize << " entries"

        " or enter a negative value" << endl;

    int i = 0;

    float temp;

    cout << "First value: ";

    cin >> temp;

    while (i < Arsize&&temp >= 0)

    {

        naap[i] = temp;

        ++i;  //注意++i的位置

        if (i < Arsize)

        {

             cout << "Next value: ";

             cin >> temp;

        }

    }

    if (i == 0)

        cout << "No-data--bye" << endl;

    else

    {

        cout << "Enter your NAAQ: ";

        float you;

        cin >> you;

        int count = 0;

        for (int j = 0; j < i; j++)

             if (naap[j] > you)

                 ++count;

        cout << count << " of your neighbors have greater awareness of\n"

             "the New Age than you do.\n";

    }

 

    cin.get();   //只要前面有输入,必须两个cin.get()!!!!!!!!

    cin.get();

    return 0;

}

除以上两个条件判断,&&还可以用来设置取值范围!!!!

非 not !

!运算符将后面表达式的真值取反,!运算符的优先级高于所有的关系运算符和算术运算符。在写语句时,涉及逻辑运算符最好将表达式用括号括起来,将测试进行分组,不论需不需要括号。

其他表示方式

标识符and、or和not都是C++保留字,可以直接使用,但在C语言里需要加头文件iso646.h。

 

字符函数库cctype

例如

isalnum()  如果参数是字母数字,即字母或数字,该函数返回true。

isalpha()   如果参数是字母,该函数返回true。

isdigit()   如果参数是数字,返回true。

……

 

条件运算符?:

expression1? expression2:expression3;

它是C++中唯一一个需要三个操作数的运算符。e1为true,则整个表达式的值为e2,否则整个表达式的值为e3。

当代码变得复杂时 用if-else表示更为清晰。

 

switch语句

switch(integer-expression)

{

case label1:  statement(s)  //(s)表示复数,可能不只一条语句

case label2:  statement(s)

default:     statement(s)   //default默认语句

}

C++中的case只是行标签,而不是选项之间的界限,也就是说程序调到switch特定代码行后会依次执行后面所有语句,除非有明确的其他指示。

程序不会自动在执行到下一个case处停止,要让程序执行完一组特定语句后停止,必须使用break语句,这将导致程序调到switch后面的语句处执行。

//使用枚举量用作标签

//通常cin无法识别枚举类型(它不知道程序员如何定义它们的)

//因此该程序要求用户选择选项时输入一个整数

//switch语句将int值和枚举量标签进行比较时,将枚举量提升为int

//另外在while循环测试条件中,也会将枚举量提升为int

#include<iostream>

// create named constantes for 0-6

enum{red,orange,yellow,green,blue,violet,indigo};

 

int main()

{

    using namespace std;

    cout << "Enter the color code(0-6): ";

    int code;

    cin >> code;

    while (code >= red && code <= indigo)

    {

        switch (code)

        {

        case red: cout << " Her lips were rid.\n"; break;

        case orange: cout << " Her hair was orange.\n"; break;

        case yellow: cout << " Her shoes were yellow.\n"; break;

        case green: cout << " Her nails were green.\n"; break;

        case blue: cout << " Her sweatsuit was blue.\n"; break;

        case violet: cout << " Her eyes were voilet.\n"; break;

        case indigo: cout << " Her mood was indigo.\n"; break;

        }

        cout << "Enter color code(0-6): ";

        cin >> code;

   

    }

    cout << "Bye";

    cin.get();

    cin.get();

    return 0;

}

    switch语句并不是为处理取值范围而设计的。switch语句中的每一个case标签都必须是一个单独的值。另外,这个值必须是整数(包括char),因此switch无法处理浮点测试。然而,如果所有的选项都可以使用整数常量来标识,则可以使用switch或if-else语句。

 

break和continue语句

break和continue语句都使程序跳过部分代码。可以在switch语句或任何循环中使用break语句使程序调到switch或循环后面后面的语句处执行;continue语句用于循环中,让程序跳过循环体中余下的代码,并开始新一轮循环。

在for循环中,continue语句使程序直接跳到更新表达式处,然后调到测试表达式处。然而,对于while循环来说,continue使程序直接跳到测试表达式处,因此while循环体中位于continue语句之后的更新表达式都将被跳过,在某些情况下,这将使一个问题

 

goto语句

goto paris;

paris:…

下面语句直接调到使用paris做标签处。大多数情况下使用该语句不好,应使用结构化语句(如ifelse,switch,continue等)来控制程序的流程。

 

猜你喜欢

转载自blog.csdn.net/ly_222222/article/details/81227238