C++每日一课(二十)

分支语句与逻辑运算符


if语句
C++当中在必须要决定执行某个操作时,通常使用if语句来实现选择。
if的两种格式:if、if else
if语句的语法
if(test-condition)
statement
如果test-condition测试条件为true,则执行statement语句,否则跳过statement语句而执行后面接下来的语句。
if测试条件也被强制转为bool值,0被转为false,非0转为true
/*
作者:xiesheng
时间:2017-07-27
版本:v1.0
说明:if语句
*/
#include <iostream>


int main() {
	
	using std::cin;
	using std::cout;


	char ch;
	int spaces = 0;
	int total = 0;
	cin.get(ch);
	while (ch != '.') {
		if (ch == ' ')
			++spaces;
		++total;
		cin.get(ch);
	}


	cout << "空格符有:" << spaces << "个。\n";
	cout << "总共有:" << total << "个字符。\n";
	system("pause");
	return 0;
}



xie sheng
advent.
空格符有:2个。
总共有:17个字符。
请按任意键继续. . .


从上面的结果可以看到:
1.当ch读到的是空格时spaces会加1,否则只加total.
2.字符总数中包含了回车换行符


if else语句
if语句是让程序来决定是否执行特定的语句或语句块,然而if else语句则让程序决定执行两条语句中的哪一条,这对于选择其中一种操作是很有用的。
if else语句语法
if(test-condition)
statement1
else
statement2


如果测试条件为true或者是非0,则程序就执行statement1,否则执行statement2


/*
作者:xiesheng
时间:2017-07-27
版本:v1.0
说明:if语句
*/
#include <iostream>


int main() {


	char ch;
	std::cout << "输入一行字符:\n";
	std::cin.get(ch);
	while (ch != '.') {
		if (ch == '\n')
			std::cout << ch;
		else
			std::cout << ++ch;
		std::cin.get(ch);
	}


	std::cout << "\n";


	system("pause");
	return 0;
}



输入一行字符:
advent .
bewfou!
请按任意键继续. . .


if else if else结构
注意:对于if else来说,else后面应是一条语句,也可以是语句块,由于if else 本身是一条语句,所以它可以放在else后面


/*
作者:xiesheng
时间:2017-07-27
版本:v1.0
说明:if语句
*/


#include <iostream>
#define FAVE 27


int main() {


	using namespace std;
	int n;


	cout << "请输入一个1~100的数值:";
	cout << "我输入的数值是:";
	do {
		cin >> n;
		if (n < FAVE)
			cout << "太小!---请继续";
		else if (n > FAVE)
			cout << "太大!---请继续";
		else
			cout << "正确!\n";
	} while (n != FAVE);


	system("pause");
	return 0;
}






请输入一个1~100的数值:我输入的数值是:50
太大!---请继续
25
太小!---请继续
35
太大!---请继续
30
太大!---请继续
27
正确!
请按任意键继续. . .




逻辑表达式
通常来说需要测试多种条件。比如:同时需要满足条件1和条件2或者是 需要满足条件1或者条件2中的一个条件,为了满足这种需求,C++提供了三种逻辑运算符,来组合或修改已有表达式。
逻辑或:||
逻辑与:&&
逻辑非:!


逻辑或:
在C++中使用||表达式组合在一起,如果原来表达式中的任何一个或全部都为true(或非0)则得到的表达式结果是true,否则表达式结果是false
C++中规定||这个运算符是个顺序点,也就是说先修改左边的值,再对右边的值进行比较
比如:i++<5||i==j
假如i=10,则在进行i与j进行比较时,i的值是11
当左侧的表达式为true时则C++会忽略右边的判断因为只要一个表达式为true则整个表达式的值就是true

/*
作者:xiesheng
时间:2017-07-30
版本:v1.0
说明:OR逻辑运算:||
*/


#include <iostream>


int main() {
	
	using namespace std;
	cout << "程序会清除你的数据\n" << "你要执行么?<y/n>";
	char ch;
	cin >> ch;
	if (ch == 'y' || ch == 'Y') {
		cout << "执行!\a\n";
	}
	else if (ch == 'n' || ch == 'N') {
		cout << "不执行!\n";
	}
	else {
		cout << "你必须要选择y或者n,当前输入错误,程序会退出!" << endl;
	}


	system("pause");
	return 0;
}




程序会清除你的数据
你要执行么?<y/n>Y
执行!
请按任意键继续. . .


逻辑与:
C++中使用&&把两个表达式组合成一个表达式,当原来的两个表达式都为true时结果才为true.
与||运算符一样,&&也是一个顺序点,它会首先判断左侧,并且在右侧进行判断之前会产生甩的的副作用。
如果左侧为false,则整个逻辑表达式为false,在这种情况下C++不会再对右侧进行判定。

/*
作者:xiesheng
时间:2017-07-30
版本:v1.0
说明:AND逻辑运算:&&
*/


#include <iostream>


const int ArSize = 3;


int main() {


	using namespace std;
	float scores[ArSize];	//定义一个float数组
	cout << "输入分数:\n";
	cout << "语文:";
	cin >> scores[0];
	cout << "数学:";
	cin >> scores[1];
	cout << "外语:";
	cin >> scores[2];


	if (scores[0] >= 60.0 && scores[1] >= 60.0 && scores[2] >= 60.0)
		cout << "分数合格!" << endl;
	else
		cout << "分数不合格!" << endl;


	system("pause");
	return 0;
}




输入分数:
语文:60
数学:79
外语:59
分数不合格!
请按任意键继续. . .


/*
作者:xiesheng
时间:2017-07-30
版本:v1.0
说明:AND逻辑运算:&&
*/


#include <iostream>
const char * desc[5] = {
	"儿童",
	"少年",
	"青年",
	"中年",
	"老年"
};


int main() {


	using namespace std;
	
	int age;
	cout << "请输入你的年龄:";
	cin >> age;
	int index;
	if (age > 0 && age <= 12) {
		index = 0;
	}
	else if (age > 12 && age <= 17) {
		index = 1;
	}
	else if (age > 17 && age <= 35) {
		index = 2;
	}
	else if (age > 35 && age <= 60) {
		index = 3;
	}
	else if (age > 60) {
		index = 4;
	}




	cout << "你现在处于:" << desc[index] << endl;


	system("pause");
	return 0;
}



请输入你的年龄:35
你现在处于:青年
请按任意键继续. . .




逻辑非:
C++使用!运算符表示逻辑非,它所后面的表达式的值取反,也就是说表达式本身的值是false,再取反则为true.


/*
作者:xiesheng
时间:2017-07-30
版本:v1.0
说明:非逻辑运算:!
*/


#include <iostream>
#include <climits>


bool is_int(double);


int main() {
	
	double num;


	std::cout << "请输入一个数值,系统会判断其是否在整型int取值范围内:";
	std::cin >> num;


	if (!is_int(num)) {
		std::cout << "超出了int型取值范围!";
	}
	else {
		std::cout << "在int型取值范围!";
	}
	std::cout << "\n";
	system("pause");
	return 0;
}


bool is_int(double d) {
	if (d <= INT_MAX && d >= INT_MIN) {
		return true;
	}
	else {
		return false;
	}
}




请输入一个数值,系统会判断其是否在整型int取值范围内:123123213213123123
超出了int型取值范围!
请按任意键继续. . .


逻辑运算符细节
在前面有提到逻辑运算符的优先级都比关系运算符低,那么如下表达式
x>5 && x<10 会被解释为(x>5) && (x<10)


!运算符优先级是高于所有的关系运算符的,因此要对表达式取反,必须使用括号括起来
比如:!(x>5)
另:!x>5 这个表达式总是为false,因为先做!x的运算结果是true或者false,接下来在关系运算中它被转为1或者0它总是小于5的
age>30 && age<45 || weight>300
上面这个表达式会被解释为(age>30 && age<45) || weight>300


注意:在使用组合的运算符时尽量要使用括号明确表达式的顺序

猜你喜欢

转载自blog.csdn.net/advent86/article/details/76408726