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

《迪哥Java教程》#17 Java的If语句教程 – 译学馆
未登陆,请登陆后再发表信息
最新评论 (0)
播放视频

《迪哥Java教程》#17 Java的If语句教程

The Java If Else tutorial - 017

在成长过程中 我是个书呆子
Growing up I was pretty nerdy.
我喜欢电脑 游戏和漫画
I loved computers, games, and comic books.
在我刚开始编程的时侯 我就想做游戏
When I first started out programming, Iwanted to make games.
10岁的时侯 我做出了我的第一款游戏
I made my first game ever when I was
它是基于我最喜欢的漫画绿灯侠制作的
10 years old based on my favorite comic-book Green Lantern.
游戏非常简单 它是一个基于文本的游戏
The game was really simple. It was a text based game
你需要做出选择
where you were presented with a choice.
如果你帮助主角做出正确的选择
If you made the correct choice for our hero,
游戏就会继续进行
you would continue in the game.
如果选择错误 绿灯侠就会死亡
Make thewrong one, Green Lantern died.
如果三个选择都正确 你就赢得了本场游戏
If you passed all three choices, you wonthe game.
整个游戏就是简单的输出语句
The full game was simple print statements
以及if then语句
and the if then statement.
这也下面我要介绍的
This is whatI’m gonna show you next.
这节课的目标
The goals for this lesson
是理解if-then-else控制语句
are to understand the if then elsecontrol statement.
和之前的课程一样
As we have in past lessons,
我们将会看到代码出错的方式
We’ll look at ways that code can go wrong.
这样你就会知道 出现错误时
This way you know how to identify the
怎样在代码中把它们找出来
errors in your code when they happen.
我们会接触到短路求值
We’ll touch upon short-circuit evaluationand when we’re done
然后是标准形式的if then语句
looking at the normal if thens,
我们先快速看一下非常简洁的三元运算
we’ll quickly look at the very succinct
不用写出整个if then语句
ternary version. Now that’s great for settingsingle values
而只需要设置单个值非常棒 我们开始吧
without writing the whole if then. So let’sdive in!
if-then是Java中的控制语句
An if then is a Java controlstatement that
它的功能就是它的字面意思
behaves exactly how it sounds in English.
如果语句为真
If the statement is true,
那么就执行 if-then有一个关键字和条件
then do this. The if-then has a key wordand condition.
关键字“if”是控制语句的开始
The keyword”if” starts the controlstatement. It is immediately followed by
它后面紧跟着需要测试的条件
the condition to test.
而条件总是写在一对圆括号里
The condition is always enclosed inparentheses.
没有圆括号的if-then就会出错
It’s an error to try to create the if-thenwithout parentheses.
如果条件为真 这个Java语句块就会执行
If your condition is true, the Java blockis executed.
条件为假 就会跳过这个Java块 我们可以测试一下
False and the Java block is skipped. We can test
如果“power>5” 射击这个坏家伙
if”power is greater than 5″ shoot the badguy.”Power
power如果小于等于5 代码就不会执行
less than or equal to 5″, the code won’trun.
条件可以是布尔表达式或者布尔变量
The condition can be a booleanexpression or boolean variable.
唯一的要求就是圆括号里的值必须是真或是假
The only requirement is anything inside the parentheses must evaluate to true
如果你以前写过C/C++
or false. If you’re coming from a C/C++background,
一定要记住这里的条件不能是0或1
it’s important to note the condition does not evaluate to 0 or 1.
它必须是Java中的布尔类型
It must be a Java boolean type.
因为它可以是一个布尔表达式
Since itcould be a boolean expression,
所以你可以这样做
you can do something like this.”If ring ischarged”
如果isRingCharged为真并且power大于5 就执行这个代码块
is”true” and the”power is greater than 5″ run this block.
两个条件必须都为真
Both conditions must be true.
在条件表达式中使用括号是一种好的做法
It’s a good idea to use parentheses
它使表达式更清晰了
inside the condition expression to make this clear. Larger conditions
为了提高可读性 应该压缩较长的条件表达式
should be compressed for readability.
你可以创建一个布尔值来表示这个复杂的表达式
You can create a boolean value from a complicated expression,
然后只需要测试这个值就可以了
and then just test the value.
在这个例子中 我们创建一个叫做“myConditon”的变量
In this casewe’re creating a variable called “myCondition”
并使它等于这个表达式的结果
and setting it to the result of theexpression.
测试一下if-then语句
Let’s play with
我们就明白了
if then statements so we get this idea clear. Okay
在这里的代码中 我创建了一个变量power
here in our code, I’ve created a variablepower
并给它赋值为6
and set it equal to 6. I’ve also got acondition
还有一个条件来表示power大于5
where power is greater than 5. Now
如果power大于5
if power is greater than 5,
它是6 所以显然大于
which it is since it’s 6,
我们会看到它输出“power is greater than 5”
we should see this line printed out,”power is greater than 5″.
运行一下
So we run it
它输出了“power is greater than 5” 如果把power改为4
and it prints out”power is greater than 5″.Let’s go back and change power equal
会发生什么呢
to 4 and see what happens.
在这个例子中 4不大于5
In this case, 4 isnot greater than 5
所以它不会输出任何东西
so it should not print out anything.
确实什么都没输出
And nothing’s printed out. Okay.
如果power等于5会发生什么呢
What do you think will happen in thiscase when power is equal to 5. Well
运行一下 还是什么都没输出
we run it, and nothing’s printed outagain
因为power不大于5
because power is not greater than 5.
它恰恰等于5
It’s just equal to 5.
回到这个例子中
Let’s go to the
看一下power如果等于5会怎么样
case where we want to check if power is equal to 5.
比如说
Say for example
我们会遇到只有power等于5才会运行的情况
we have some case where it’ll only run when power is 5.
我们来测试一下 正如我们在运算符那一节
We would test that as we saw in theoperators
看到的那样使用双等号 运行 输出了“power is greater than 5”
video with the double equals. We run it, and it printed out”power is greater than 5″,
实际上输出结果应该是“power equal to five”
but really the test was power equal tofive.
有时候我们会说
Sometimes we want to say
如果条件为真就这样做 否则就那样做
if something is true do this, otherwisedo something else.
Java中就可以这样做 可以再加一个关键字“else”
We have that ability in Java. We can addanother keyword called”else”.
在这个例子中 如果power大于5
In this example we’re saying, if”power isgreater than 5″,
就执行这段Java代码 否则执行另一段Java代码
the do this Java block, otherwise dothe other Java block.
你也可以把if-then语句写的更长
You can also chain these into longer
比如说 这段代码
if then statements. For example, thisblock tests
测试power是否大于5和power是否小于等于5这和之前用else语句
if”power is greater than 5″ and tests if”power is less than equal to 5″. This is really the same thing as the else
表达的意思相同
statement from before.
我们刚搞清楚了第二个测试 如果你使用无条件的else语句
We’re just making the second test explicit. Ifyou use this keyword”else”
那么在所有if条件都为假的情况下
without a condition, it will run thefollowing code
它会运行下面这段代码
block all other if’s are false.
你也可以根据需要添加多个else-if语句
You can add as many else-if’s as you needas well.
也不要疯狂地加啦 不然代码会很难读懂
Just don’t go crazy or it gets really hardto read!
如果没有包括所有的情况会怎样呢
So what happens when not everything is accounted for? For example
比如在例子中 大于5会执行这段代码
in this case there’s a block for”greater than 5″ and
小于5会执行这段代码 power等于5时会怎样呢
a block for”less than 5″. What happenswhen”power
那么这两段代码都会被跳过
equals 5″? Well this just means bothcode blocks are skipped.
什么都不会执行 这就说明了
Nothing is run. This illustrates theimportance of
使用数值和不等运算时检查边界条件的重要性
checking all the boundaryconditions when working with numbers
如果你想要忽略相等的情况
and inequalities. If you’re ignoring theequals case,
这里就很适合添加注释来说明原因
this would be a really good place to add a comment explaining why.
否则你会忘了为什么
Otherwise your going to forget,
或者别人看到代码时会纠结于这是不是个错误
or somebody else looking at your code is going to be
好的 回到我们的例子
very confused by your error. Okay goingback to our example
我们来创建第二个布尔值
let’s have a second boolean value.
创建第二个布尔值“myCondition2”
Let’s create a second boolean value “myCondition2”,
来表示power小于或等于5
where power is less thanor equal to 5.
我们再创建一个测试语句 如果myCondition2为真
We can create a second test that is, ifmyCondition2
就输出“power is less than/equal to 5”
then print out”power is less than/equal to 5″.
但是我们并不需要这样做
Now we really don’t need to do this.
我们没有必要
We won’t necessarily need to be doing
去测试每个条件 在这个例子中
a test for every condition that we want.In this case
只有两种可能 它要么大于5
there’s only two possible options. Iteither greater than 5
要么小于等于5
or it’s less than or equal to 5.
所以我们可以用else代替第二个条件
So wecan replace the second test with
它会得到同样的结果 这个例子中
“else” and it will result in the samething. So in this case
myConditon为真 它会输出“power is greater than 5”
myCondition is true, it’ll print”thepower is greater than 5″.
如果条件为假
If it’s false,
就会输出“power is less than or equal to 5”
then it’ll print out”power is less than orequal to 5″.
它和有两个if-then语句的代码
It has the exact same effect as
产生完全相同的结果
the code with the two if then statements.
这个例子中要么是“大于”要么是
Now this example does”greater than” and thenit does
“小于或等于” 但如果我们想分别要每一种情况
“less than or equal”, but if we wanted onecase for each,
一种情况是power大于5
one case for”if power is greater than 5″, onecase for
一种是power等于5 一种是power小于5
“power is equal to 5″ and one case for”power is less than 5”.
我们可以用多个if-than语句
Well we can replace that code
来代替这段代码
with multiple if then statements.
在这个例子中 看是否满足第一个条件
So in this case we have it”if it’s
否则看是否满足第二个 否则再看第三个
this” otherwise”if it’s this” otherwise
if-then语句可以写的很长
“if it’s this”. You can create really longif then statements chains,
这是一种糟糕的做法
but it’s considered bad practice and there isprobably a better
如果你需要测试的代码中
way to do it if you find that you
if-then语句多于4个
for testing and have a case for
有一种更好的方法
more than four if then statements.
另一种常见的错误是忘记使用大括号
Another common source of errors is forgetting braces. As we saw
在前面的代码块课程中 我们说过 如果你想使用大括号的话
earlier in the blocks lesson, a statementcan be a block.
一条语句也可以是一个代码块 这里有一个很棒的例子
Still you want to use braces. Here’s agood example
你可能无法得到预期的结果
where you may not get the behavior you’reexpecting.
在这个例子中移除大括号就意味着
In this example removing the braces means,
第二行代码每次都会执行
the second line will always be printed.
这里的缩进被忽略了
The indentation is ignored. Going backto the IDE,
在IDE中 我们可以更直观的了解这个问题
we can see this problem in action.
这段代码和之前代码块课程中的代码很相似
So this code is similar to what we saw in
之前的课程里 你可以不用大括号创建一个语句块
the blocks lesson where you can create a Java block without brace.
然而 你不加括号的话会显得很奇怪
However if you don’t include the braces it makes it kinda weird.
在这个例子中 如果power大于5
In this case, we have if”power isgreater than 5″, printout
就输出“power is greater than 5” 下一行会输出
“power is greater than 5”. And the nextline we have
“power is less than 5”
“power is less than 5”.
代码像这样缩进
Now the code is indented like this,
某些人看到时就会感到困惑
it’s going to be confusing for someone reading it.
他们看到这段代码 就会认为
They’re going to look at this and think
这两行代码位于同一语句块
that these two lines are part of the same
我们运行它
Java block. So when we run it,
输出了“power is less than 5”
it prints out”power is less than 5″.
我们可能会预料
We might have expected
这行代码属于
that that particular line was part of the” power is
power大于5这个条件
greater than 5″
你就会困惑于为什么它会执行
and would be really confused on why thatline ran.
原因是
The reason is
只有“power greater than 5”这一句是一个语句块
“power greater than 5” is it’s own Java block.
我们应该加上大括号
We really should be putting in braces tobe really clear
使我们的表达意图更清晰
what we intend to happen.
有时 在测试中
Sometimes you’llhave a test that takes
一个条件的计算花费的时间比另一个要长的多
much longer to evaluate compared to theother tests.
比如说你想测试一下
For example you might test if 5 is somewhere
一个有一百万个数字的列表中是否包含5
in a list of a million numbers,
以及b是否大于4
and test if b is larger than 4. The test
对b的测试要比在一百万个数中查找快的多
for b is much easier and quicker than searching through list a million numbers.
如果需要两个条件都为真 一定要把简单的放在第一个
If both need to be true always test the easier test first!
这个例子中 交换这两个布尔测试要好的多
In this case, it would be much better to switch the boolean test.
它之所以高效是因为“短路布尔运算”
This works because of”short circuitboolean evaluation”.
也就是说如果它知道无论后面的表达式是真是假
This means it will stop evaluating theboolean expression
整个表达式都会为假
if it knows it will evaluate to false regardless
它就会停止计算
of the rest of the expression.
因为两项都需要为真
Since both need to be true,
所以如果b不大于4
there’s no point searching a million numbers
搜索一百万个数就没有意义了
if b is not greater than 4. So
如果b为2 Java就不会尝试去搜寻
b is 2, Java won’t even try to search.
如果你需要基于布尔表达式
When youneed to set
来设置一个值 比起写完整的if-then表达式
a value based on a boolean expression,there is much simpler
这种方式代码更简单
code than the full if then.
它被称作三元运算符
This is calledthe ternary operator.
我们没有讲过这个运算符
It’s an operator we didn’t cover inoperators
因为它是一个控制运算符
because this operator is a control operator,
我们还没有具体讲
and we really hadn’t covered
它表示什么
what that meant yet.
我们可以把它写在一行语句里
We can write it in a one-line statement.
你可以很简单地把表达式的值赋予变量
You simply set your variable to expression.
第一部分是需要测试的表达式
The first part is the expression totest. Next
然后用问号来表示这是一个if-then测试
we use the question mark to note that this is an if then test.
最后是两个可能的取值
Finally we put the two possible values
它们用分号隔开 如果条件为真
separated by a colon. If the condition istrue
变量就被设为第一个值
the variable set to the first value,
否则被设为第二个值
otherwise it’s set to the second value.
该表达式无法处理else的情况
There are no possible ways to handle an else in this expression.
我们在编辑器中试一下
Let’s go play with this in the editor.
最后我们来看三元运算符
The last thing to look at is the ternary operator.
我们有一个变量b
So if we had a variable b
我们想为它赋值
that we wanted to set,
如果power大于5
and we wanted to set it to 42
我们想把它赋值为42
if” power is greater than 5”
如果小于或等于5 赋值为24 我们可以用一行代码实现
and 24 if it is”less than or equal to 5″, wecould write that all on one line.
不需要用完整的if-then语句
We don’t have to do a full if then in this case.
这里我们可以使用三元运算符
We have our ternary operator where”power
“power > 5”是条件
greater than 5″ is our condition,
然后是三元运算符
and then with the ternary operator we’ll set
如果条件为真 就设为42
it to 42 when it’s true,
为假就设为24
and 24 when it’s false.
就像其它的条件一样
Now like with other conditions
我们也可以用一个真正的条件
we can also replace our power is greaterthan 5 with
来代替“power>5”
the actual condition.
运行它
When we run it,
这里例子中 b等于24
in this case B is equal to 24
因为power等于5 不大于5
since power is equal to 5. It’s notgreater than five.
一定要谨记这一点
Again it’s important to note
这里无法处理else的情况
that you can not handle an else condition here,
因为你只能写两个可能的值
because you can only have two possiblevalues.
你无法用多个分号
You can’t chain with multiple colons or
或其它的语法来表示if-then-else
with some other syntax to say
它是固定表达 如果条件为真
if then else. It’s always if it’s true
取第一个值 为假取第二个值
first condition, false if it’s the secondcondition.
有很多种可能性 你会胡乱使用if-then语句
There are ways you can really
从而写出一些糟糕的代码
abuse the if-then statement and create some obnoxious code.
比如说你会用if-then语句来设置一个布尔值
For example you can create this if then code to set a boolean value.
这些代码很糟糕
This is bad code!
因为它用了6行代码
The reason is this issix lines of code,
来设置一个布尔值
all to set a boolean value.
你知道更好的方法吗
Can you guess thebetter form?
给你个提示 不用三元运算符
I’ll give you a hint. It’s not the ternary operator.
对于布尔值 这才是正确的做法
This is what you should do for booleans.In this case
你要计算的表达式本身就是一个布尔值
the boolean expression you’re evaluating isa boolean value.
对于这种形式的赋值 不必用if-then来实现
Instead of using an if then to do the logic,
而只是把该值赋给它就可以
just set it to the value
就做完了 最好的代码
and you’re done. The best code ever
是你没写的代码 本节课程
is the code that you don’t write. So in thislesson
我们学习了
we learned about the
if-then语句以及如何使用它
if then statement and how we use it to control
来控制程序中哪些代码运行
what code is run in our application.
我们学习了Java是如何“短路”条件的
Welearned how Java short-circuits your
以及它是如何加快程序运行的
conditions and how this can speed upyour program.
还学习了三元运算符
We also learned about the ternaryoperator. And with that,
下次课见
I’ll see you in the next lesson.
感谢观看本视频
Hey thanks for watch the video!
DeegeU.com上有一个本课程的快速测试
There a quick quiz for this on DeegeU.
你可以测试自己学到了多少东西
com for you to gauge how much you learned.
如果你喜欢本期视频
If you like the videos you are seeing,
记得在YouTube上
please let me know by liking the video
为DeegeU点赞
and hitting the subscribe button
并关注DeegeU频道
for the DeegeU channel on YouTube.
非常感谢
I’d reallyappreciate that.
如果你有任何问题
If you have concerns or questions,
请在下方评论区留言
please leave them in thecomments below
或在DeegeU.com上评论
or on DeegeU. com.
在DeegeU.com的主页上有一个投票
There’s a poll onthe front page of DeegeU.
你可以选择来决定下次讲什么
com so you can let me know thetopic covered next.
感谢收看 下期再见
Thanks for watching, and see you in the next video!

发表评论

译制信息
视频概述

本节课程介绍了Java中的if-then-else语句,“短路”运算以及三元运算符。

听录译者

收集自网络

翻译译者

[B]hugue

审核员

审核员1024

视频来源

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

相关推荐