• #### 科普

SCIENCE

#### 英语

ENGLISH

#### 科技

TECHNOLOGY

MOVIE

FOOD

#### 励志

INSPIRATIONS

#### 社会

SOCIETY

TRAVEL

#### 动物

ANIMALS

KIDS

#### 卡通

CARTOON

#### 计算机

COMPUTER

#### 心理

PSYCHOLOGY

#### 教育

EDUCATION

#### 手工

HANDCRAFTS

#### 趣闻

MYSTERIES

CAREER

GEEKS

#### 时尚

FASHION

• 精品课
• 公开课
• 欢迎下载我们在各应用市场备受好评的APP

点击下载Android最新版本

点击下载iOS最新版本

扫码下载译学馆APP

#### 《卷轴教程之C++入门》#8 关系及布尔运算符

C++ Relational and Boolean Operators

Ok. So in this video we are gonna be looking at

relational operators, boolean operators and conditional statements.

And then we’ll look at applying all these concepts

to a specific program.

Let’s get started.

All right, let’s take a look at relational operators.

And quite a many of you’re familiar with

a lot of these relational operators

cause you’ve seen them in

various maths classes that you’ve probably had.

The first relational operator we have is the Greater Than operater.

So we gonna have the 6 expression here

to test to see if 10 is greater than 6,

and that is true,

so the result of this expression here

would be a true value,

a boolean value.

Ok. So, Less than,

we keep compare character value,

so we are comparing capital C to capital A.

And since capital C comes later in the alphabet than capital A,

this is where you result in a false value.

So C is not less than A,
C在A后面 所以C不小于A
since C is coming after A in the alphabet.

So it’s pretty cool that you’re gonna actually

compare character values using these relational operators.

Greater Than Or Equal To.

So in this case we are comparing 10.5 to 10.5.

We know that those would be equivalent values.

So it qualifies based off of this.

This equals here, so it is Greater Than Or Equal To,

uh, 10.5 is greater than or equal to 10.5, so that’s true.

Less Than Or Equal To.
5.5是否小于等于……
Is 5.5 less than or equal to, excuse me,

is negative 5.5 less than or equal to negative 100?

And the answer there is false

because negative 100 would be less than negative 5.5

so that turns out to be false.

So the way we test for equivalence when we using these relational operators

is not to just simply use a single Equal.

If we use the single equals that would be assignment.

So we don’t want to do asignment, we want to do equivalent,

so we have to do equal-equal.

This is a very common mistake that people make

when they are first learning how to program is,

they are wanning to compare two things to one another

and see if they’re the same,

but they’ll just simply use a single equal sign.

You need to use two.

So we may have a string value.

In this case we have “hi”.

And we will assign it to the string variable x.

And here we have “Hi” again.

But this time the first letter H is capitalized

where is here was actually lower case.

So if we try to compare those two values.

We would find out that they were not equal

since we do have a difference in

capitalization of this first letter.

So that would return a false value.

And then the last relational operator we have.

So we only have 6 of these.

Last one is the Not Equal To.

So it’s the exclamation point and then the equal sign.
a是一个整型数
So we have integer value, say, a,

that was assigned the value of 5,
b也是一个整型数 被赋值为12
and we have intanger value b that was assigned 12.

We could see if a was not equal to b

and that would, in fact, be true.

So that’s basically that we have, 6 relational operators,

and we can use them with various types of operands.

We can use them with the numeric data type.

So we’ve seen the int, we ‘ve seen the double,

we can use them with character types,

we can also use them with string types,

and we will see that we can also use them with

other data types as well

that we haven’t talked about just yet.

All right. So let’s look at some of the boolean operators.

And the first we’ll look at is the and operator.

And basically it says we have to have 2 things being true

in order for the overall expression to be true.

So we see here what we have is the truth table.

So we can have some expression

that resolves down to true or false

for P, and also for Q.

If we and those together, using our And operater

which is this double ampersand.

Then in order for that whole expression to be true,
P和Q必须都为真
then both P and Q have to be true.

So we have false and false results in false,

false and true results in false,

and truth and false results in false,

and then finally true and true results in true.

so maybe an example that we could think of is

testing to see if someone could vote in the United States

based off of their age, and based off of

whether they are US citizens or not.

So maybe allowing P to be their age
Q表示国籍
and Q to be their citizenship.

So if it turns out that they’re 17 years of age,

but yet they’re, uh, Canadian citizens,

then they would not be able to vote.

If they were 17 years of age and US citizens,

they still wouldn’t be able to vote.

Say, they were 18 years of age

and they were not US citizens. They were Canadians.

They wouldn’t be able to vote.

But if they were 18 and they were US citizens,

then they could vote.

Of course this is not taking into account,

then being a felon, and maybe being locked up in prison.

That comes down to a states’ right,

rights issue in the United States.

So we are ignoring that.

I don’t want anyone to get confused.

It’s only 18 and being a US citizen.

There are some other things, but this is a simplistic view.

So let’s basically the boolean operator, double ampersand.

We have to have both operands, both sides,
P和Q都为真时
P and Q being true

in order for the whole expression to be true.

All right. So now that’s look at the boolean operator for Or,

which is 2 pipes.

2 pipe symbols back to back, so that’s what we have here.

A pipe symbol and then another pipe symbol.

And the way that you create the pipe symbol

from the keyboard is to hold down the shift key

and then to hit the backslash key.

At least this is the case on most keyboards.

So you may want to check that out,

normally it just gonna be a vertical looking symbol.

It may have a break in the middle of it, it may not,

but typically just shift and backslash

will create the vertical pipe symbol.

So what is the Or operation mean?

Or basically says that we have to have at least

one of the operands being true

in order for the whole entire expression to be true.

So the only time we end up with a false,

if we look at our truth table here,

is if both operands, both P and Q, are in fact false.

So an example where we may use the or operation

is to test to see if two individuals

can get on a rider in a amusement park.

And maybe the stipulation for this ride

is that you have to be 18 years old

or older, in order to get one the ride.

Unless you are accompanied by an adult.

If you have a kid and their parent that is 18 years of age,

then the kid would be able to get on the ride.

So, maybe P represents the age of one individual
Q代表另一个人的年龄
and Q represents the age of another.

So if we had, say, two, you know, a 16 years old for P and
Q是7岁
a 7 year old for Q,

they could not get on that ride.

But if you had a 6 years old for P
Q是20岁
and a 20 years old for Q,

then they could get on the ride.

Same thing here we if had a 25year old
Q是4岁
and a 4 year old

then they would be able to get on the ride.

And if both of them were adults,

over the age of 18 or older,

then they would be able to get on the ride.

So basically the Or operation just says

as long as we have one of the operands being true,

then the whole expression is true.

All right. So let’s look at the Exclusive Or operation.
C++中的异或符号是
And the Exclusive Or symbol in c++

is the little hat or caret symbol.

So the way that you gonna make that is

holding on the shift key

and then hitting the six key.

So not the six on the numeric keypad,

but the other six.

And the Exclusive Or is very similar to our other Or

except for you cannot have both operands being true.

So it says that one and only one in our operands

can be true for the whole expression to be true.

So an example that we may think of is two lights,

which is they may be wired to the same exact light bulb.

And if both of those light switches are in the on position

then the result of the light switch will be off.

Whereas if either one and only one of those

light switches is in the on position,

then the light bulb will be on.

So, that would be a simple example, I guess,

to use for the Exclusive Or.

One and only one of the operands can be on

for it to be true.

Otherwise we have a false situation.

OK. So the last boolean operator that we are gonna look at is the Not operator.

So the Not operator is just simply the exclamation point.

And the Not operator is diffrent from the other boolean operators

in that it is a unitary operator,

meaning it only able works on one operand,

or is the other boolean operators was –

they were binary operators, so they worked on two operands.

And the Not operator is real simple. It basically says,

whatever our boolean expression is, we wanna flip it.

So if P is false, not P will be true,

If P was true, not P would be false.

So maybe P represents whether something is empty or not.

So we can test to see if it was not empty by doing not P.

So that’s a basic example using Not operator.

We’ll see other examples as we continue on in the series.

So that’s the last boolean operator.

So now we will look at actually solving a program, or a problem statement

and write a program that makes use of all these

only some of them, maybe not all of them.

[B]hugue