第6章 分支语句和逻辑运算符

6.1 if语句

程序清单6.1 if.cpp

// if.cpp -- using the if statement
#include<iostream>
int main()
{
	using std::cin;
	using std::cout;
	using std::endl;
	char ch;
	int space = 0;
	int total = 0;
	cin.get(ch);
	while (ch!='.')		//quit at end of sentence
	{
		if (ch == ' ')	//check if ch is a space
			++space;
		++total;
		cin.get(ch);
	}
	cout << space << " spaces, " << total;
	cout << " characters total in sentence\n";
	return 0;
 } 
Bing want to go to zoo.
5 spaces, 22 characters total in sentence

6.1.1 if else 语句

// ifelse.cpp -- using the if else statement
#include<iostream>
int main()
{
	char ch;
	std::cout << "Type, and I shall repeat.\n";
	std::cin.get(ch);
	while (ch != '.')
	{
		if (ch == '\n')
			std::cout << ch;		// done if newline
		else
			std::cout << ++ch;	// done otherwise
		std::cin.get(ch); 
	}
	std::cout << "\nPlease excuse the slight confusion.\n";
	return 0;
 } 
Type, and I shall repeat.
Bing want to go
Cjoh!xbou!up!hp
Bing want to eat
Cjoh!xbou!up!fbu
.

Please excuse the slight confusion.

6.1.2 格式化 if else 语句
6.1.3 if else if else 结构
程序清单6.3 ifelseif.cpp

// 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 ";
	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;
 } 
Enter a number in the range 1-100 to find my favorite number: 88
Too high -- guess again: 77
Too high -- guess again: 66
Too high -- guess again: 55
Too high -- guess again: 10
Too low -- guess again: 35
Too high -- guess again: 20
Too low -- guess again: 27
27 is right:

6.2 逻辑表达式

6.2.1 逻辑OR运算符: ||

// or.cpp -- using the logical OR operator
#include<iostream>
int main()
{
	using namespace std;
	cout << "This program may reformat your hard disk\n"
			"and destroy all your data.\n"
			"Do you wish to continue? <y/n>";
	char ch;
	cin >> ch;
	if (ch == 'y'|| ch == 'Y')
		cout << "You were warned!\a\a\n";
	else if (ch == 'n'||ch == 'N')
		cout << "Good!\n";
	else
	cout << "That was not a y or n!";
	return 0;
}
This program may reformat your hard disk
and destroy all your data.
Do you wish to continue? <y/n>y
You were warned!

6.2.2 逻辑AND运算符: &&
程序清单6.5 and.cpp

// and.cpp --using the logical AND operator
#include<iostream>
const int ArSize = 6;
int main()
{
	using namespace std;
	float naaq[ArSize];
	cout << "Enter the NAAQs (New Age Awareness Quotients) "
		 << "of\nyour neighbors. Program terminates "
		 << "when you make\n" << ArSize << " enties "
		 << "or enter a negative value.\n";
	int i = 0;
	float temp;
	cout << "First value: ";
	cin >> temp;
	while (i < ArSize && temp >= 0) // 2 quitting criteria
	{
		naaq[i] = temp;
		cout << "temp";
		++i;
		if (i < ArSize)
		{
			cout << "Next value: ";
			cin >> temp;
		}
	}
	if (i == 0)
		cout << "No data==bye\n";
	else
	{
		cout << "Enter your NAAQ: ";
		float you;
		cin >> you;
		int count = 0;
		for (int j=0; j<1; j++)
			if (naaq[j] > you)
				++count;
		cout << count;
		cout << " of your neighbors have greater awareness of\n"
			 << "the New Age than you do.\n";
	}
	return 0;
 } 
Enter the NAAQs (New Age Awareness Quotients) of
your neighbors. Program terminates when you make
6 enties or enter a negative value.
First value: 1
Next value: 2
Next value: 3
Next value: 4
Next value: 5
Next value: 6
Enter your NAAQ: 11
0 of your neighbors have greater awareness of
the New Age than you do.

6.2.3 用&&来设置取值范围
程序清单6.6 more_and.cpp

// more_and.cpp -- using the logical AND operator
#include<iostream>
const char * qualify[4] = 
{
	"10,000-meter race.\n",
	"mud tug-of-war.\n",
	"master canoe 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;
 }
Enter your age in years: 50
You qualify for the master canoe jousting.

6.2.4 逻辑NOT运算符
程序清单6.7 not.cpp

// not.cpp --using the not operator
#include<iostream>
#include<climits>
bool is_int(double);
int main()
{
	using namespace std;
	double num;
	cout << "Yo, dude!Enter an integer value: ";
	cin >> num;
	while (!is_int(num))
	{
		cout << "Out of range -- please try again: ";
		cin >> num;
	}
	int val = int (num);		//type cast
	cout << "You've entered the integer " << val << "\nBye\n";
	return 0; 
 } 
 
 bool is_int(double x)
 {
 	if (x <=INT_MAX && x >= INT_MIN)
 		return true;
 	else
 		return false;
  } 
Yo, dude!Enter an integer value: 1236487678234
Out of range -- please try again: 872
You've entered the integer 872
Bye
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值