未登录,请登录后再发表信息
最新评论 (0)
播放视频

《卷轴教程之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.
我们一共有6个关系运算符
So we gonna have the 6 expression here
第一个是比较10是否大于6
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,
我们会比较字母C和A的大小
so we are comparing capital C to capital A.
因为在字母表中 C在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.
在这个例子中我们会比较10.5和10.5的大小
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,
所以10.5>=10.5 结果为真
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,
不对 是-5.5是否小于等于-100
is negative 5.5 less than or equal to negative 100?
结果为假
And the answer there is false
因为-100小于-5.5
because negative 100 would be less than negative 5.5
所以结果为假
so that turns out to be false.
相等要怎么判断呢?
What about Equals?
使用关系运算符做相等判断时
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.
第一个字符串是”hi”
In this case we have “hi”.
把它赋值给字符串变量x
And we will assign it to the string variable x.
第二个字符串也是”Hi”
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.
我们一共只有6个关系运算符
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,
被赋值为5
that was assigned the value of 5,
b也是一个整型数 被赋值为12
and we have intanger value b that was assigned 12.
我们可以比较一下a是否不等于b
We could see if a was not equal to b
结果为真
and that would, in fact, be true.
这就是6个基本关系运算符
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.
就像int以及double类型
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
分别赋值给P和Q
for P, and also for Q.
然后我们把P和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.
我们可以用P表示年龄
So maybe allowing P to be their age
Q表示国籍
and Q to be their citizenship.
如果一个人17岁
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.
如果一个人17岁且为美国公民
If they were 17 years of age and US citizens,
他也没有选举权
they still wouldn’t be able to vote.
如果一个人18岁
Say, they were 18 years of age
但不是美国人而是加拿大人
and they were not US citizens. They were Canadians.
他也不会有选举权
They wouldn’t be able to vote.
如果一个人18岁且为美国公民
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.
只有18岁及以上且为美国公民才有选举权
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
按住shift键
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,
一般来说 按shift键和\键
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,
必须要两个操作数P和Q均为假
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
一个人只有在18岁及以上
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.
如果一个儿童有18岁(及以上)父母的陪同
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.
所以可以用P代表一个人的年龄
So, maybe P represents the age of one individual
Q代表另一个人的年龄
and Q represents the age of another.
如果有两个人P和Q 其中P是16岁
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.
但是如果P是6岁
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.
同样的 如果P是25岁
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,
年龄在18岁及以上
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
按住shift键
holding on the shift key
然后按数字6键
and then hitting the six key.
注意不是数字键区的6
So not the six on the numeric keypad,
而是主键区的数字6
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.
如果P为假 非P就为真
So if P is false, not P will be true,
如果P为真 非P就为假
If P was true, not P would be false.
假设P可能表示某物为空或非空
So maybe P represents whether something is empty or not.
我们就可以通过给P取反来判断它是否为非空
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.

发表评论

译制信息
视频概述

本节课程简单介绍了C++中的关系运算符以及布尔运算符的用法。

听录译者

金鱼汤

翻译译者

[B]hugue

审核员

豆子

视频来源

https://www.youtube.com/watch?v=O7p0sB28NN0

相关推荐