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

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

Using Conditional Statements in C++ Part 3

好的 我们已经写完红色种子的代码
All right, so now that we have all of our code in here for the red seed.
对于蓝色种子 我们也会进行类似操作
Let’s go and do the same thing or something similar for our blue seed.
先将代码下拉到这儿
So we’ll come down here.
在这里 我们已经判断了种子是否为蓝色
So we already have this first test here to see if seed color is blue,
现在进入这段代码
and now we need to just come in and write some if statements, so ‘if’,
来添加一些新的条件语句
and this is gonna be something new as well,
若温度在60到70之间
if the temperature is between 60 and 70,
你或许会这样写
so you may be, you know, inclined to write this as
60小于等于temp
’60’ and then put less than or equal to ‘temp’,
然后在这里 你或许会像这样
and then put this here, so maybe something like this,
用数学符号补全等式
maybe be the way that you’ll write it in some mathematical notation,
显然这句表明了温度介于60到70之间
so very strickly that’s saying the temperature is between 60 and 70.
然而在C++中 你不能这么写
Unfortunately you cannot get away with this in C++.
现在让我来展示正确的做法
So let me show you the correct way that we write this expression.
如果变量或数值有两个限制范围
So if we have two bounds that variable or value has to be between.
我们必须分开来表述变量
We have to express the variable being associated separately with both of those bounds.
因此我们可以把temp或60写在前面
So we could say ‘temp’. Well you could say ’60’ or ‘temp’ first.
比如temp大于等于60
You could say ‘temp >= 60’,
“与”temp小于等于70
‘&& temp <= 70',
这才是表达的正确方法
so that’s the way we have to write that expression there.
因此如果给定数值落在某范围内
So if we have a particular value and it needs to fall on a range,
为了精确表示这个变量
we have to specify that particular variable
我们需要和它的上下限联系起来
associated with both the lower range and the upper range.
同时这里使用了布尔算子“&&”表示“与”操作
And notice that I used the ‘&&’ boolean operator here
与此相反的为布尔算子“||” 即“或”操作
as opposed to the ‘||’ operator.
为了展示两种操作符的不同 我们把这里改成“||”
So if we put ‘||’, just to show you the difference here, if we put ‘||’ in here,
有些人也许会将输入温度设为150
somebody may put in the temperature of 150,
这是非常高的土壤温度了 对吧?
that’s a very hot soil temperature, right?
而此处可以看到 150大于60
So it would qualify here, so 150 is in fact greater than 60,
该条件已经满足
so this would be true.
因此无须判断另一条件的真假
So doesn’t matter if this part was false or not.
而事实上 150没有落在这个范围内
So really 150 doesn’t fall on the range.
所以 这里需要使用“&&”操作符而不是“||”操作符
We don’t need have the ‘||’ operation there, we need have the ‘&&’ opration.
因为这样写 对于两边来说
Cos both this and the right hand side,
它们的条件都必须为真
so both the left and right hand side need to be true.
好了 我们在这儿添加左括号
All right, so come down and put a ‘{‘ in.
然后继续往下拉
And we’ll come down a little bit below here right.
并在其他右括号上方补全括号
Right above other close brace.
这就是全部的操作了 但事实上 我把右括号放在了错误的位置
So what we were saying is that all of this, opps I actually put my close brace in the wrong spot,
它需要放置在“Otherwise”的上面
need be above the ‘Otherwise’ here.
此时 如果温度在60和70之间
So if the tempure is on our rage between 60 and 70.
我们将执行其他代码
We’ll do some code here, ‘otherwise’
这是利用“else”语句来实现
which we say ‘else’ right, we’ll do this.
好了 我们继续吧
Okay if you want you can go ahead.
先将页面下拉 并在这儿执行输出语句
Do the output statement down here something scroll down a bit.
即通过“cout”来输出后面的内容
So we’ll do ‘cout’ and say
“蘑菇会长出来”
‘a mushroom will grow’.
最后加上换行符
okay, newline character.
好了 现在我们来检查土壤是否湿润
Alright so now we can check to see if the soil is wet,
输入“if”语句
so we can see ‘if’,
条件为土壤湿度较高
‘soil moisture is wet’.
接下来 对土壤湿度进行判断
So do that test and
若结果为真
that turns out to be true.
我们将使用“cout”语句输出内容
We will output using ‘cout’ and say
“蒲公英会长出来”
‘a dandelion will grow’.
好了 这里我们用同样的逻辑
Alright and the same sort of logic here.
通过“else”语句处理条件不成立的情况
Again we could use the ‘else’ here as opposed to using ‘if’
此外 若我们已知了土壤的干湿度
assuming that we knew that the user input wet or dry.
这段程序就不必进行输入验证
Again we’re not doing any sort of input validation in this program.
但对于程序5 我们应在程序4的基础上进一步改进
But maybe for program 5, we’ll come back and revise program 4,
并完善输入验证的功能
and show how to do some input validation.
这里我展示了程序4的编写方法
And I’ll probably show some alternatives just how to write program 4.
同时去改善其中的逻辑
And actually improve our logic and see what the difference is
分析土壤改变后的不同情况
whenever we change this stuff a bit. So soil moisture
首先 我们判断土壤湿度是否干燥
and test to see if it’s equal to dry.
接着在下面这里
and so will come down here,
输入大括号
put in a close brace,
并在里面使用“cout”语句
then do our ‘cout’,
输入“向日葵会长出来”
and say ‘a sunflower will grow’.
好了,这就是我们程序的基本框架
Okay, that’s the basics of our program,
下面来验证一下效果
we’ll just check not chest but check to see
如果我们有红色或蓝色的种子
if we have a red seed or a blue seed.
经过程序内的逻辑分析后
and follow the appropriate logic within those.
它可能会执行“else”语句下的操作
So you could even have an ‘else’ operation here,
因此 如果种子不是红色的
so if the seed wasn’t red,
我们假定它为蓝色
we may be able to assume that was blue.
你就可以用“else”来替代这条语句
So you can actually put here an ‘else’.
我们可以倒退回去 看看这两者的区别
We’ll come back and change that just to see what the difference is,
每当我们使用“else” 就会有对应的“if”语句
whenever we have the ‘else’ there versus having the if statement there.
然而 我还要指出另一件事
All right another thing that I will point out real quick
这里只有一条语句
is that if we only have a single statement
它与“if”关联 条件满足时执行
that we want to execute associated with ‘if’.
因此若条件为真 我们将输出相应内容
So if this is true we want to do this output here.
而这也是我们所需的唯一语句
so that’s the only statement that we want to have,
此时 我们无须添加左右括号
we do not have to have this open brace close brace
它们限定了条件为真时执行的代码块
defining the body of this ‘if’ statement being true.
这里是一样的道理
And the same thing goes here,
如果不为真 那么这儿也是一样
the same thing over here well that’s it that’s not true,
我们不需要这个大括号
I guess we do need to have… oh no we don’t have to have these
因为这块只有一条语句
these braces either because we only have that single statement.
但是 如果“if”或“else”下包含多条语句
But if you have multiple statements associated with the ‘if’ or ‘else’,
你就必须添加上述的大括号
then you definitely need to have those braces.
不过 我在编程的时候
For me and the way that I do my programming always,
总会把大括号加上
always put those braces in regardless if I have
不管里面是一条还是多条语句
a single statement or more than one statement.
这样将利于返回 并添加新的语句
So if i come back and actually add in additional lines,
比如这些地方 我都加入了大括号
I already have those braces there.
这样更便于阅读代码
And also find it’s just easier to read for me,
但也可能只是我的习惯而已
but maybe that’s just the what I’m accustomed to.
因此 如果代码块内只有一条语句
So you can make that decision if you only have a single statement.
你可以自行决定加不加花括号
Then you can just ignore putting in those open brace close brace.
啊 在这个“else”下 我们忘记写输出语句了
Oh we actually forgot to do our output statement here for this ‘else’,
先输入“cout”
so this should be ‘cout’ and then we have
然后写上 “蘑菇会长出来”
‘a mushroom will grow’.
好了 我们已经完成
Alright so I think that’s it.
这时可以保存工程项目
So I think we’re ready to save this program,
然后运行程序 观察结果
then run it and see how this program works.

发表评论

译制信息
视频概述

通过编写不同土壤下的植物生长程序,创作者讲述了条件语句的语法及具体使用

听录译者

真皮沙发

翻译译者

Being

审核员

审核团HN

视频来源

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

相关推荐