最新评论 (0)

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

Using Conditional Statements in C++ Part 2

好了 我们现在继续 开始编辑源代码
All right. So let’s go ahead and start putting in our source code.
And we ‘ll start with our Pound Includes. We have
(insert)”#(pound) include
还需要输入 “#include
And we need to have “#(pound) include “.
OK. Since we are gonna have some variables and literals of type string
所以需要输入“using namespace” 空格 “std” 分号
and then “using namespace” space “std(standard);(semicolon)”.
OK. So now we’re ready to start typing in the main function.
我们回车几行 输入 “int main”
So let’s come down a couple of lines and type in “int main”,
左括号 右括号
open paren, close paren,
回车 输入左大括号
and come down a line and type in an open brace.
如果你想的话 你可以全选这里的伪代码
And then if you want to, you can select the rest of the pseudocode here
and we’ll just tab it over.
选中伪代码 按下“Tab”键
Once you select it, you can hit the tab key you know
tab all of that pseodo code over,
since we want to have all of these inside the main function.
在最下面 输入右大括号
And also at the very bottom here, you can put in a close brace.
好啦 我们现在要在main函数里编写内容了
All right. So now we already start typing in our main
回到下面 在这里
function here. So let’s come down and
do Get the seed color.
为了获取种子颜色 我们需要一个变量来存储
So in order to get the seed color or the color, we need to have a variable just capable of holding
whatever color is typed in by the user.
所以 我们声明一个 string变量 命名为“seedColor”
So, we’ll declare a string variable, and we’ll call it seedColor.
And we’ll set it to the empty string.
So we get our decleration statement here.
Now, one thing I wanna point out that I haven’t pointed out before
is that whenever we
声明一个变量 变量名由多个单词连结组成的时候
declare variables and there are multiword variable name,
命名习惯 或者说我们当前用的命名习惯就是
the convention is, or the convention that we’re using,
第一个单词小写 之后的单词
is to have the first word lowercase and any subsequent words
全都首字母大写 这种叫做 驼峰命名法
they’ll be what we call camel case, meaning the first letter’s capitalized,
and then the rest of the letters are lowercase.
And this actually aids and been able to see
that we do in fact have a multiword variable.
你可以看到单词间明显的区分 因为“seed”后面是大写
So you can actually see the delineation cause we have the upper case seed there.
当然 这只是一个命名习惯 并不是编译器要求的规范
Again this is just a convention, it is not required by the compiler,
so you could name it x, but that’s not really
indictive of what this variable does or means.
So we wanna have a variable to hold the seed color, so I wanna name it seedColor.
接下来我们给用户一个操作提示 利用“cout”输出显示
So then once we have that, we can then have a prompt to the user using our “cout”.
输入“cout” 输出内容是“Enter the seed color”
So do “cout” and say, “Enter the seed color”.
加上 圆括号 输入“red or blue”
And we’ll put in parentheses here, “red or blue”.
假设用户行为良好 按照提示操作
And we’ll assume that we have good users right now. They type in
输入红色(red)或蓝色(blue) 而不是紫色、绿色、汉堡或其他的什么东西
red or blue. They don’t type in purple or green, or hamburger, or something like that.
现在有了提示 我们可以读取用户在键盘上的输入
So once we have the prompt there, then we can read it read in whatever they type on the keyboards
So you could use a “cin”,
我们要做的就是 把用户输入的值存储到变量“seedColor”里
and then we wil want to do is store whatever they type in into the variable “seedColor”.
以上都是基础简明的操作 没有什么新的知识点
And that’s basically it, so this is nothing new. We’re just using
some of the things we have learned from our previous programs.
接下来 获取温度 就获取温度而言
So, Get tempreture. And in the case of tempreture, we’ll
want to have that is some sort of numeric value
cause we will want to compare it to other numeric values.
So we will store this into an integer value since we’re
I guess you could store it into a double.
Our tempreture may have a fractional part. For right now, we are gonna say that our
不过现在 我假定 温度没有小数部分
tempreture is not gonna have a fractional part. So we’ll just put in
所以我把它放到整型变量里 输入“int temp”
an integer variable. So we do int, temp,
赋值为0 这样就初始化为0了
and set it to zero. So we initialize it to zero,
and then we’ll do cout.
输入“Enter the temperature”
We’ll say, “Enter the temperature”.
或许在这可以输一个“F” 表明是华氏温度
Maybe we’ll put “F” here indicate Fahrenheit.
Even though we are really not keeping track of units.
单位是很重要的 但在这个程序中
Units are important for, but for this program we are gonna
我会忽略 温度的相关单位
ignore the units related to temperature, and we just
add that in there for fun, I guess.
下一句 用“cin”和“>>”输入操作符
So then we’ll do “cin”. And then use extraction operator
输入变量名 这个变量存储用户输入的温度值
And type in the variable name, where we wanna store the temperature that they type in,
接下来要做的就是 获取土壤湿度
And then, the next thing we need to do is Get the soil moisture.
一样也是用 string字符串变量
This is also gonna be a string variable.
We’ll just call it “soilMoisture”.
那么 把它赋值为空 或是初始化为空
Ok and we’ll assign it to the empty string or initialize it to the empty string
用“cout”输出 内容是
And “cout”. And then we’ll say,
“Enter the soil moisture”.
设置选择为 湿 或 干 写在小括号里
And we’ll put “wet or dry”, and parentheses here,
这是提示用户 他们需要输入什么内容
sort of prompting the user what we expect them to enter in.
All right.
Now if the user goes in and enters in something besides
这种情况 我们的程序目前还没法处理
what we are expecting, our program is not gonna be able to handle that.
At least not what the way we write it now.
不过在之后的视频 我们会回过头来
We may come back and change… later videos will
进行修改 检查输入的有效性
modify this, what actually does the validation of the input.
好了 这里要写入变量—“soilMoisture(土壤湿度)”
So see when you have the variable “soilMoisture” here,
那么 用户进行了输入之后
OK. So now, once we have our input,
for seed color, tempreture and soil moisture,
我们可以做一些判断 这样就涉及到了一些新的语法
We can start makimg some decisions. So we are getting into some new syntax here.
目前为止 在我们的工程1、工程2、工程3、工程4中
So so far, in our programs, program one, two, three and four,
所有的语句指令都是一条接一条 顺序执行的
All over our instructions have been one right after the other, in sequence.
现在 我们要实现的是
So now we’re looking at having some sort of
在多个选项里 进行判断选择
choice alternative, we’re making decisions.
基于可能的选择 我们会有一系列的分支语句
And we’ll branch off to one set of code based off of what decision is made here
也就是 如果种子颜色是红色 我们会有这一些选项
So if it’s red we’ll look at these set of choices and if it’s blue
如果是蓝色 我们就看下面的这一部分选项
we’ll look at these set of choices down here.
那么 检查某项是否为真 的语法就是
So the syntax for testing to see if something is true,
一个简单的 “if”语句
is just a simple if.
和这里的伪代码很像 我们输入“if”
So almost like our pseudocode, we can say if,
使用 变量“seedColor(种子颜色)”
and use our variable called “seedColor”,
and check, using our Relational operator Equal,
请注意 进行比较要用“==”两个等号
So, remember, to test for equivalence we have to use “equal equal”s.
不能只用一个等号 一个等号是赋值 两个等号是比较
It’s not just a single equal. Single equal means assignment, double equal means equavalence.
这里 我们先看 种子颜色是否是红色
So here we wanna see if the seed color is red.
你要确保把 red(红色) 写到双引号里
So make sure you put red inside of the
double qoute, otherwise it will not be seen as a string.
这里 我们就是 将一个字符串
So we are trying to compare one string
这个字符串 是一个名为“seedColor”的变量
which is a one variable here, called “seedColor”, which is of type string,
将它和一个 字符串字面量 进行比较 这个字面量是“red”
to a string literal. And the string literal says “red”.
好了 在这里输入左大括号
All right. So I’m gonna put open brace.
And then I’m going to put close brace right above
放在伪代码“if blue seed”的上方
the pseudocode for “If blue seed”.
那么 接下来我们就要
So what we’re trying to
define here is the body of the if statement.
So the body is defined by the open brace and the close brace. We’re saying that if
我们设定 如果 种子颜色是红色
the seed color turns out to be red,
then we wanna look at testing these set of actions here,OK.
另外一步要做的 就是下滑到这儿 测试 如果
And the other thing that we wanna do is come down below and test to see if
如果 种子颜色等于蓝色
Say if seed color is equal to blue.
We’ll do these set of actions down here.
在下面 确保我们输入了右大括号
So come down to make sure we have our close brace.
关于Eclipse有一个好处 它实际上把我们的大括号排列了起来
And the nice thing about Eclipse it actually aligns up our braces.
有了左花括号和对应的右花括号 它们应该会自动对齐
If you have an open brace and a close brace, it should automatically align those braces for you.
在这里 我需要指出 关于if语句的另一个要点
The other thing I should point out about these if statements here,
they have to test a
一个布尔值 所以不管圆括号里是什么内容
Boolean expression here. So whatever we have inside parentheses,
“if” 左括号 某某内容 右括号
If, parenthesis, something, close parenthesis,
that thing inside the parentheses has to be a boolean expression,
即 最终括号里的返回值是 真/假
meaning it has to resolve down to a true/false value,
但不一定必须是“真/假”值 也可能是一条语句
but doesn’t resolve down to a true/false value, then we’ll end up with a syntax here.
好了 现在我们来处理 种子颜色是红色时的操作
All right. So I’m gonna just focus on the red for right now.?
If the tempreture is greater than or equal to 75.
查看这里的源代码 C++代码
So check out how this source code here. The c++ code
is very similar to the actual pseudocode.
所以 如果 温度
So if, temp,
is greater than or equal to 75,
then we want to do these set of actions,
Put the close brace at here.
否则 我们就执行这些步骤
Otherwise we do this set of actions.
我们执行“否则”操作 的方式是
And the way we go about doing otherwise
is use the keyword else.
输入“else” 左大括号
So we say else, open brace,
下来几行 在这里输入 右花括号
And then we come down here and type in the close brace, and make sure
确保 包含了 输出蘑菇 的伪代码部分
it’s inclusive of our pseudocode for outputting mushroom.
在这里 对应伪代码 用了关键词“else”
So we could have use else here for
但仅在 “否则”的情况下使用
our pseudocode, but I just use the term otherwise.
所以是这么说的 如果我们有一个if语句 而且if语句不成立
So all that saying is, if we have an if statement, and that if statement fails to be true,
and the only other alternative we have
在这个实例里 另外一个选择是
so in this case the only other alternative is,
如果这是假的 如果温度小于75(度)
if that’s not true, if the tempreture is not greater than or equal to 75,
and we have to determine that we have a red seed,
Then we know we end up with a mushroom.
So we don’t need to have another condition here.
So you don’t have to go in here and write
“if tempreture is less than 75”.
因为这是仅有的另一个——哦 我忘记小括号了
’cause that’s the only other – oops, I forgot my open paren.
So we don’t have to go in there and write this expression.
我们可以这么做 在这里这样表达在语法上是没问题的
Now we could do that. There’s nothing wrong syntactically with doing that expression there.
It’s basically doing the same thing that we had before.
But there’s no reason to test to see if the tempreture is less than 75,
因为如果我们失败了 如果这是假的
because if we fail here, if this turns out to be false,
那么 我们就知道温度低于75(度)了
then we know the tempreture is less than 75.
所以我们可以只说“else” 好了
So we can just say else. OK.
好了 那如果土地是湿的
All right. So what about if the soil is wet,
so we can do a similar test as we did before with the seed color,
and this time we’ll do “soilMoisture”
and test to see if the soil moisture is equal to “wet”.
If that turns out to be true,
then we’ll do our output statement,
and if the “soilMoisture” turns out to be dry,
we will do this output statement.
事实上 你可以做相同的事——做两个if语句
And you could actually do the same thing here as opposed to doing two if statements,
you could actually do an if, and then you can do an else,
assuming that the user, you knew, that the user was inputting wet or dry,
so if they didn’t input wet then you knew that they input input a dry.
所以如果你想 我们就可以把它变成else语句
So we could in fact make this an else statement if you wanted to.
I’m going to leave it in here as a, as an if statement for now, maybe we’ll come back and change it,
为了输出 我们应该写上“cout”并输出“会长出向日葵”
so in order to do the the output, we would do “cout” and then just output “A sunflower will grow”.
I will say “A sunflower will grow.”
And down here we’ll do “cout” and do a dandelion, let’s see.
好了 有了 我们也许需要一个换行符
All right, so we have that, we may want a new line character in there
so I’ll going to go back in here and put a new line character.
好了 有了
All right, so we have that.