ADM-201 dump PMP dumps pdf SSCP exam materials CBAP exam sample questions

《卷轴教程之C++入门》#12 条件语句四 – 译学馆
未登录,请登录后再发表信息
最新评论 (0)
播放视频

《卷轴教程之C++入门》#12 条件语句四

Using Conditional Statements in C++ Part 4

先按保存按钮 再按构建按钮
All right. So I’m going to click on save and then click on the bulid button.
构建整个程序 这会编译并链接它
Build this program. That’s compiling and linking it.
现在我们来运行它
Now, we’ll run it.
到这里来 控制台显示要输入种子颜色
So down here on the console says enter the seed color
我要输入红色
so I’m going to enter in red.
输入温度 假设温度是85度
Enter the temperature. I’ll say the temperature is 85.
土壤湿度为干燥
And the soil moisture is dry.
输出了“会长成蒲公英” 这正是我们期望得到的结果
So it outputs “A dandelion will grow” and it’s that what we expected.
回到这 看有关红色种子这段
So if we go back up here to the paragraph dealing with a red seed
温度在75度以上
it’s above 75 degrees
并且土壤干燥 就会出现蒲公英
and the soil is dry then it’s going to produce a dandelion.
通过前面的逻辑分析 可以知道这正是我们期待的结果 结果确实是这样的
And that’s what we expected if you look at our logic, that’s exactly what happened.
所以种子是红色 温度大于等于75度
So we have a red seed, temperature was greater than or equal to 75
且土壤湿度为干燥 就会输出蒲公英
and the soil moisture was dry so it outputs the dandelion.
这是很基础的东西
So very basic stuff here.
希望它讲得通
and hopefully that make sense.
我再运行一次 测试一下其他的执行路径
Let’s run it one more time just test another path of it exeuction here.
运行程序 这次输入种子为蓝色
So run this, this time we’ll type in blue.
假设温度为45度
We’ll say the temperature is 45.
土壤湿度为湿润
And enter the soil moisture:wet
控制台显示会长出蘑菇 我们检查一下 确保它是正确的
So it says a mushroom will grow. Let’s check that out, make sure that that is true.
如果种子是蓝色的
So if we have a blue seed
要长成花 温度必须在60到70度之间
and for a flower to grow it has been between 60 and 70.
我们的例子里 温度是45度
In our case we had 45.
然后是如果不在温度范围之内
And then it says that so if it doesn’t meet the temperature range here
它就会长成蘑菇 正是我们得到的结果
then it will grow mushroom that’s exactly what we got.
我们可以检查代码逻辑来证明它 种子为蓝色
We can look at our logic to confirm that. Blue
温度不在范围内
so the temperature was not between this range
所以最后要执行”else”
So we ended up with the “else”.
我们测试了这个条件 结果为假
So we tested this here, this turned out to be false.
所以最后会执行else语句中的内容
So it ends up doing whatever we have here in the else operation
就会长出蘑菇
so mushroom will grow.
你可能注意到了一些事
Now something that you probably noticed here
在知道了温度后
is that we’re actually asking for the soil moisture
我们还要求输入了土壤湿度
after we have got the temperature.
如果我们已经知道了会长出蘑菇
There is really no reason to ask for the soil moisture
再要求输入土壤湿度就没道理了
if we know that we will end up with a mushroom.
所以程序中可能有些地方可以修改
So that’s maybe something we can go and change in our program.
我们要把输入土壤湿度的代码移动一下
Okay, so let’s look at the moving this code here where we get the soil moisture.
往下移动到它起作用的地方
Down to the point where it actually matters.
所以我们要把这三行代码
So we are going to move these 3 lines of code
向下移动到种子为红色
down below for them in the case the red seed
且已知温度大于等于75的情况下
after we know that we have a temperature greater than or equal to 75.
因为只有在那种情况下 我们才会考虑
Because only at that point time, do we take into account
土壤湿度是湿润还是干燥
the soil moisture being wet or dry.
如果我们已经知道温度低于75度
So if we fall into a situation where temperature is less than 75
也就知道了我们最后会得到蘑菇
we’re know…we know that we’re going to get a mushroom.
土壤湿度是多少都没关系了
Doesn’t matter what the soil moisture is.
这和刚才蓝色种子情况相同
It’s the same case with the blue seed.
就温度而言 蓝色种子没落在这个范围
The blue seed is not falling in this range here in term of the tempetature.
我们最终会得到蘑菇
Then we’re all end up with a mushroom
所以除非在这个温度范围内
so there is no need to ask for the soil moisture
否则我们没必要知道土壤湿度
until we know that we’re in the correct temperature range.
我要选中这段代码
So I’m gonna just take this code here and select it.
我只是拖动鼠标 点击左键再拖动鼠标
I’m just dragging over, left clicking and dragging.
我会按住”ctrl”键
And then I’m going to use hold down “ctrl”
再按”x”键
and then hit the “x” key
就可以把这段代码剪切下来
and that will cut that bit of code.
可以看到Eclipse中的代码分析器
And you see that the code analyzer here in Eclipse
已经警告说
was already complaining it’s already saying that
这里有一些未解决的变量 但是只要我们到这里
we have some unresolved variables here but as soon as we come in here
到if语句的里面
inside of this if statement where we have the if
就是这个温度大于等于75度的if语句
temperature is greater than or equal to 75
粘贴代码 按住”ctrl”键
and paste it we’ll hold down “ctrl”
再按”v”键就可以粘贴了
and then hit the “v” key to paste.
eclipse的代码分析器就不会再有警告了
Then the eclipse code analyzer is not complaining any more.
这个就已解决了
That’s now resolved
这里还有未解决的变量 我们继续
still unresolved down here. So let’s go ahead and
把这段代码也粘贴到这
paste in this bit of code down here as well.
再次按”ctrl”加”v”
So “ctrl” “v” once again.
就粘贴上了
and we paste that code in.
现在在土壤湿度没用的时候 我们就不必输入它了
So now we’re not asking for the soil moisture until it actually matters.
我们继续 保存程序
So let’s go ahead and save this
构建程序
build it
然后运行程序
and we’ll run it.
这次我们输入红色
So we’ll put in a red
种子为红色
the seed color
输入温度为65度 这个温度要低于
And we’ll put in a temperature of 65, so that’s down below
可以长出花的温度
where we get a flower.
这里显示长出了蘑菇 可以看到
And it says a mushroom will grow and notice how we did
它没有要求用户输入土壤湿度
not ask the user for the soil moisture.
这很好
And that’s good.
我们不想问用户不必要的问题
We don’t want to ask them unnecessary questions
在我们已经提前知道
if we already know ahead of time that
不会得到那个结果的情况下
we’re not going to get that result.
我们再运行一次
Let’s run it one more time.
这次我们会使用蓝色种子
And we’ll do it for blue seeds.
输入蓝色
So we’ll type in blue
温度为95度
and we’ll put in a temperature of 95.
结果显示会长出蘑菇
And it says a mushroom will grow.
我们测试了
So we exercised
蓝色种子的第二种情况
the second case here if the seed color is blue.
现在我们来看看代码
All right, so let’s take a look at our code
看一下我们是如何用if语句测试是否为红色种子的
and notice how we have this if statement here to test if we have a red seed.
如果是蓝色种子 这还有另一个if语句
And then we also have another if statement here to see if we have a blue seed.
你或许想知道如果把这里改成else语句
And you may be wondering what would happen if we change this
会怎么样
to an else statement.
那就是说
So that’s saying that
如果种子的颜色不是红色
if the seed color is not red
它就一定是蓝色
then it has to be blue.
但是这种情况可能是错误的 有些人可能会输入
And that may not be the case right so somebody could actually input
绿色或棕色或者输入的甚至不是颜色
the color green or brown or maybe not even a color at all.
他们可能会输入任何东西
They could input anything.
所以只要输入不是红色
So anything other than red
就会执行到下面
would actually come down and try to execute
执行else语句中的内容
whatever we have here in this else statement here.
如果温度在60度到70度之间
So if the temperature happened to fall between 60 and 70
我们就要求输入土壤湿度
then we’ll ask for the soil moisture.
如果土壤湿度为湿润
And if it happened to be wet,
就输出”会长成蒲公英”
we would output that a dandelion would grow.
他们可能把种子颜色输入为
They may have been putted, you know, that the seed color
汉堡包或其他诸如此类的东西
was, you know, hamburger or something like that
这完全没有任何意义
something that doesn’t even make any sense.
所以在执行else语句时一定要十分小心
So we have to be careful whenever we use this else here.
一定要确保输入有效
We have to know that we have valid input
并且可以排除掉所有其他选项
and that we exhausted all other alternatives.
else语句中剩下的
And the only thing else left
就是这些东西了
is this stuff here.
这时才可以用 或者说我们是这样使用else语句的
That’s where we put how we would use the else statement.
我想我们可以让else语句保持不变
We could, I guess, leave the else in there.
我们可以在这做一些修改
If we came up here and maybe did something like this.
在得到种子颜色之后 可以测试
So after we get the seed color test to see if
种子颜色
the seed color
如果种子颜色不是
if the seed color is not equal to
红色
red
并且
and
种子颜色
the seed color
种子颜色
is… seed color…
不是蓝色
seed color is not equal to blue
如果是这种情况 或许我们可以直接向用户输出……
If that turns out to be the case, maybe we’ll just output to the user…
使用”cout”输出
We’ll do a “cout”, and then say
“颜色选择无效,
“Invalid color selected”
程序即将退出”
“Program will terminate”
所以我们的代码可以像这样
Okay, so we could have some bit of code like this
然后再返回0 现在就是
and then we could do maybe a return zero and now to actually
真正的退出程序了
terminate our program at that point of time.
所以如果这样写
So if we had that, it would actually be okay
使用else语句也没关系了
to have an else statement in here.
因为如果用户输入除了红色和蓝色之外的任何东西
Because if they input anything other than red or blue
都会运行到这 执行这条语句
it’s going to come in here and execute this statement
并返回0 这就意味着 会退出主程序
and then do the return zero meaning it’s going to exit out of our main program
退出主函数
our main function.
注意看 在这我们是怎么写这个布尔表达式的
Notice how we did construct the particular boolean expression here.
我们写了种子颜色不等于红色且种子颜色不等于蓝色
We said seed color not equal to red and seed color not equal to blue.
你一定要非常小心 如果在这我们用了”or”
You have to be very careful. If we were to use the “or” here.
它和”and”运算正好相反
It opposites to the “and” operation.
我们来看一下会发生什么 这也是一个常犯的错误
Let’s notice what happens because this is a common mistake
有些人会使用”or”而不是”and”
as well that someone who put an “or” here instead of an “and” statement.
现在这里是”or” 用户可能会输入蓝色
So with the “or” maybe they type in blue. Okay.
蓝色不等于红色 对吧
So blue is not equal to red, right?
这条语句就为真
So this would be a true statement.
只需要有一个表达式为真
And we only have to have one true statement
就可以使
in order for
整个表达式为真
this whole thing, in order for this whole entire expression to be true.
所以最后我们会执行这条语句 但是这不是我们想要执行的
So we would end up executing this statement here and that’s not what we wanted to have.
我们需要让
We needed to have
这两个语句都为真
both of these being true.
所以如果用户输入蓝色
So if they type in blue
这条语句会为真
this would turn out to be true
并且
and
这会变成蓝色不等于蓝色
this would be blue is not equal to blue.
这……
That’s…
这一定为假
that is in fact false.
真和假进行与操作 结果为假
So true and false for the “and” operation the whole expression turns out to be false.
所以不会执行这段代码
So we wouldn’t execute this bit of code here.
这正是输入蓝色时我们希望发生的
And that’s what we want to happen if they typed in blue.
所以在使用与运算和或运算
So do be careful
测试逻辑时一定要仔细
between using the “and” and the “or” operations and test out your logic.
接下来我们看一下它是怎样工作的
All right, so let’s see how this works.
我要先保存
I’m going to save it
然后构建
build it
并再次运行它
and run it once again.
这次我们要输入
All right, so we’ll type in
棕色
brown.
输出显示“颜色选择无效,程序即将退出”
And it says “invalid color selected. Program will terminate.”.
这正是我们希望发生的
That’s exactly what we expected to happen.
返回并且再运行一次
So go back and run it one more time
确保输入蓝色时它可以正常工作
and make sure it does work if we type in blue.
我们输入蓝色
So, let’s type in blue.
并且输入温度为75度
And we’ll put in a temperature of 75.
结果显示会长出蘑菇
And it has a mushroom will grow.
这正是我们所希望的 因为温度不在范围内
That’s exactly what we except because it did not fall into this temperature range.
我们再往下滚动一点 看蓝色种子的部分
Let’s scroll down a little further to get to the blue section.
温度不在60度到70度的范围内
So it didn’t fall them in between the 60 and 70 range.
所以最后会输出蘑菇
So we ended up with a mushroom here.
使用像这样的else语句也非常好
So it’s perfectly okay to have the else statement like we have here now
只要我们通过这个条件验证了输入的有效性的话
as long as we validated our input through this condition here.
所以我们写了这个条件语句
So we provided this conditional statement.
对于这个程序 可能我们要做的最后一件事是
All right, so maybe the last thing that we’ll do with this program is
对输入的土壤湿度做一些有效性验证
actually do some input validation for our soil moisture.
就像我们在这做的一样
So much like we did here.
对土壤湿度
For the seed color
可以做一些和种子颜色类似的操作
can do similar things for the soil moisture.
所以我们要到这来
So we’ll come down here and
并且在这下面写一些代码
write some code below this.
在得到土壤湿度后 我们就可以测试是否
So after we get the soil moisture, we can test to see if
土壤湿度
soil moisture
不等于湿润
is not equal to wet.
并且土壤湿度不等于干燥
and soil moisture is not equal to dry.
现在我们知道了什么?我们知道输入是无效的
What do we know? we know that we have invalid input there.
所以我们可以用”cout”输出
So we’ll say “cout”
“无效……”
and say “invalid”
输出“输入的土壤湿度无效”
say “invalid soil moisture”
可能”输入”这个词更合适
“was inputted”, maybe “inputted” is a better word to use
然后再输出“程序即将退出”
and then we’ll say “program will terminate”.
好了
All right.
我们可以这样做 然后再返回0 这样程序就真的结束了
So we call do that and then do a return zero and that actually ends the execution of the program.
所以这会退出主函数 并把控制权交还给操作系统
All right, so it exits the main function just returns it to operating system control.
我们已经写好了这段代码 希望它是正确的
All right, so we have got that and that looks correct hopefully.
土壤湿度不等于湿润并且不等于干燥
So the soil moisture is not equal to wet and not equal to dry
就意味着接收到的土壤湿度不是我们希望去使用的
that means we do not have a soil moisture that we expect to make use of.
因为有了这段代码 我们就可以到这
And since we have that bit of code, we could actually go in here
把这当成if语句 并把这个各种改成else
and have this if statement here but have this as an else.
所以我们可以修改这段代码 把这改成else 非常像我们在前面做的
So we can change this bit of code and have this as an else much like we did before.
我们可以复制这段代码
And we can copy this bit of code here
并且把它粘贴到蓝色种子这
copy that and then paste it down here for the blue one as well
也对输入做些有效性验证
do input validation as well here.
然后我们可以做什么呢?
And then what can we do?
我们可以把这去掉 改成else
We could take this out and change it to an else.
因为我们知道如果程序通过了这里的if语句
Because we know if we make it past this point here, after this if statement here
条件语句就不为真
we make it pass, this ends up not being true
就可以知道土壤湿度一定是湿润或干燥
then we must have the soil moisture being wet or dry.
这就是我们为什么可以用else的原因
Okay, so that’s the reason why we can do that else there
如果它不是湿润
because if it’s not wet
就一定是干燥 因为它通过了这个条件
it has to be dry if we’ve made it pass this condition here.
现在代码中可能还有一件事使你烦恼
Now one thing that may be bothering you about this code is
就是在几个地方有重复的代码段
that we have replication of code segments in several places here.
没错 这里代码重复 这里代码也重复
Right, so we have this replication of code here and reputation of code here.
希望这会使你有些不安
And hopefully that’s a little bit unsettling to you.
随着你编程越来越多 每次看到代码重复的时候
As you start programing more and more, anytime you see replication of code
都应该感到烦恼 随后我们
that should be upsetting. But latter on we’ll look at
或许可以去掉这些重复的代码
maybe being able to get rid of this replication of code
并且写一个函数 如果我们对做重复的工作不感兴趣的话
and write a function if we are not interested in doing the same operation again.
我们只需要简单的调用函数来做这些工作
We’ll just simply call a function to do that task
可以认为那些函数和这里的代码相同
that suppose to rewritting the same code over.
我想我们刚才的讨论已经涉及到了程序的绝大部分
All right, so I think that pretty much wraps up our discussion of program for.
最后我们可以保存程序
I guess, the last thing we can do is save our program and
或许还可以构建并运行它几次
maybe build it and run it a couple of times.
我们来构建并运行它
So let’s build this and run it.
接下来输入红色
So go ahead and type in red
温度假设为75度
and the temperature we’ll say is 75.
并且土壤湿度为
And we’ll put the soil moisture as…
我不知道 我们来编造一些无意义的东西 比如说泥土
I don’t know, let’s make up something it doesn’t make any sense which just say dirt
土壤湿度是泥土
the soil moisture is dirt
按回车键就会显示出“输入的土壤湿度无效 程序即将退出”
and hit enter and it says “Invalid soil moisture was inputted. Program will terminate.”
另一件我们可以做的事或许是
Only the other thing I guess we may do is
把“土壤湿度”用空格隔开 在字符串里面
we could just say soil moisture with a space we don’t have to use
没必要用变量名
the variable name inside that string literal.
所以我们可以修改它 在中间放一个空格
So we change that and put a space in there.
然后我们可以保存并重新构建程序
Then we can save and rebuild that.
或许我们可以检查一些种子为蓝色时 是否正常工作
Maybe we’ll check out the blue and see if it works.
输入蓝色
So blue.
然后输入温度为65度
And than we’ll put a temperature of 65.
土壤湿度就输入“湿” 这不是一个有效选项
And the soil moisture is we’ll say “moist” which is not a valid selection.
就显示出“输入的土壤湿度无效,程序即将退出”
So invalid moisture was inputted. Program will terminate.
这正是我们希望的结果
That’s exactly what we expect.
我们需要花费时间去组织程序结构 你或许可以有多种路径
So we need time of constructing a program, you may have multiple alternatives or multiple paths
去执行这个程序
that you can select through the program.
尝试去测试所有的执行路径以确保程序正常运行是一种很好的做法
It’s good to try to test and exercise all those paths to make sure that the program works.
我们当然还没有测试所有的执行路径
And we certainly haven’t exercised all the paths for this program
所以我希望你继续运行这个程序
so I encourage you to, you know, continue playing around this program
或许可以知道怎样去改进它
and maybe see how you could improve it.
我不认为它是完美的
I’m not saying it’s perfect.

发表评论

译制信息
视频概述

本节课程对通过实际代码对条件语句做了详细介绍。

听录译者

[B]hugue

翻译译者

[B]hugue

审核员

审核团O

视频来源

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

相关推荐