C++学习之分支语句和逻辑运算符(if语句、逻辑表达式、字符函数库(cctype)和?:运算符)

 

1.当C++程序必须决定是否执行某个操作时,通常使用if语句来实现操作。if有两种格式:if和if else.

if(test-condition)
   statement

如果测试条件为true,则if语句将引导程序执行语句或语句块;如果条件是false,程序将跳过这条语句或语句块。

例如:计算输入中的空格和字符总数,则可以在while循环中使用cin.get(char)来读取字符,然后使用if语句判别空格字符并计算其总数。

#include<iostream>

int main()
{
	using std::cin;
	using std::cout;
	using std::endl;
	char ch;
	int spaces = 0;
	int total = 0;
	cin.get(ch);
	while (ch != '.')
	{
		if (ch == ' ')
		{
			++spaces;
		}
		++total;
		cin.get(ch);
	}
	cout << spaces << "   spaces, " << total;
	cout << "characters total in sentence" << endl;
	return 0;
}

当且仅当ch为空格是,++spaces才被执行。

2.if else语句

if语句让程序决定是否执行特定的语句或语句块,而if else语句则让程序决定两条语句或语句块中的那一条,这种语句对于选择其中一条操作很有用。

if(test-condition)
    statement1
else
    statement2

如果测试条件为true或者非零,则程序将执行statement1跳过statement2;否则相反。

例如,假设要通过对字母进行加密编码来修改输入的文本(换行符不变)。(以下程序将字符都进行加1处理换行符除外)

#include<iostream>

int main()
{
	char ch;
	std::cout << "Type, and I shall repet.\n";
	std::cin.get(ch);
	while (ch != '.')
	{
		if (ch == '\n')
			std::cout << ch;
		else
		{
			std::cout << ++ch;
		}
		std::cin.get(ch);
	}
	std::cout << "\nPlease excuse the slight confusion.\n";
	return 0;
}

3.if else if else 语句——使计算机程序提供了两个以上的选择。

直接上例子:猜数字(猜中程序结束)

#include<iostream>
const int FAVE = 25;

int main()
{
	using namespace std;
	int n;

	cout << "Enter a number int the range 1-100 to find";
	cout << "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);

		return 0;
}

4.逻辑表达式

4.1逻辑OR运算符:||——C++可以采用逻辑OR运算符(||),将两个表达式组合在一起,当两个条件有一个或全部满足某个要求的时候,则得到表达式的值为true;否则表达式的值为false。

举个例子:

i++<6||i==j;

这里由于||的优先级比关系运算符低,所以不需要再这些表达式中使用括号。假设i原来的值为10,则在对i和j进行比较时,i的值将为11.另外如果左侧的表达式为true则c++将不会去判定右侧的表达式,因为只要有一个表达式为true,则整个逻辑表达式为true

4.2.1逻辑AND运算符:&&——也是将两个表达式组合成一个表达式。仅当原来的两个表达式都为true时,得到的表达式的值才为true。举两个例子:

5==5&&4==4;//true because both expressions are true
5==3&&4==4;//false because both expressions are flase
5>8&&5<2;//flase because both expressions are flase

4.2.2用&&来设置取值范围——&&运算符还允许建立一系列if else if else语句,其中每种选择都对应与一个特定的取值范围。

例如以下程序:(另外,他还演示了一种用于处理一系列消息的技术。与char指针变量可以通过指向一个字符串的开始位置来表示字符串一样,char指针数组也可以表示一系列字符串,只要将每个字符串的地址赋给个元素)

#include<iostream>
const char *qualify[4] = {
	"10,000 - meter race.\n",
	"mud tug-of-war.\n",
	"masters came jousting.\n",
	"pie-throwing festival.\n"
};

int main()
{
	using namespace std;
	int age;
	cout << "Enter your age in years:";
	cin >> age;
	int index;
	if (age > 17 && age < 35)
		index = 0;
	else if (age >= 35 && age < 50)
		index = 1;
	else if (age >= 50 && age < 65)
		index = 2;
	else
		index = 3;
	cout << "You qualify for the" << qualify[index];

	return 0;
}

4.3逻辑NOT运算符:!——该运算符将它后面的表达式的真值取反。(!的优先级高于所有的关系运算符和算术运算符)

5.字符函数库cctype——C++C语言继承了一个与字符相关的、非常方便的函数软件包,他可以简化诸如确定字符是否为大写字母、数字、标点符号等工作,这些函数的原型是在头文件cctype中定义的。例如ch是一个字母,则isalpha(ch)函数返回一个非零值,否则返回0.同样如果ch是标点符号(如逗号或者句号),函数ispunct(ch)将返回true。(这些函数的返回类型为int,而不是bool,但通常bool转换让你能够将它们视为bool类型)

例如:这些函数比使用AND和OR运算符更方便。可入,下面是使用AND和OR来测试字符ch是不是字母字符的代码:

if((ch>='a'&&ch<='z')||(ch>='A')&&(ch<='Z'));
//与使用isalpha()相比:
if(isalpha(ch));

 

6.?:运算符——

expressional?expression:expressions

如果expression1为true,则整个条件表达式的值为expression2的值;否则,整个表达式的值为expression3的值。

5>3?10:12;//5>3is true ,so expression value is 10
3==9?25:18;//3==9 is flase,so expression value if 18

猜你喜欢

转载自blog.csdn.net/qq_38721302/article/details/82944553