Java Notes (2)
来源:互联网 发布:http端口号设置 编辑:程序博客网 时间:2024/05/16 10:56
转载请注明出处:http://blog.csdn.net/cxsydjn/article/details/71513567
The note introduces how to use control flow and conditional statements in Java.
Java notes of open courses @Codecademy.
Boolean Operators
&&
: and operator- It returns a boolean value of
true
only when the expressions on both sides of&&
are true.
- It returns a boolean value of
||
: or operator- It returns a Boolean value of true when at least one expression on either side of
||
is true.
- It returns a Boolean value of true when at least one expression on either side of
!
: not operator- It will return the opposite of the expression immediately after it. It will return
false
if the expression is true, andtrue
if the expression is false.
- It will return the opposite of the expression immediately after it. It will return
Boolean Operator Precedence
!
>&&
>||
- Every expression within parentheses is evaluated first.
- Expressions are also read from left to right.
Conditional Expressions
if
statement- In Java, the keyword
if
is the first part of a conditional expression. - It is followed by a Boolean expression and then a block of code. If the Boolean expression evaluates to
true
, the block of code that follows will be run. - The
if
statement is not followed by a semicolon (;
). Instead it uses curly braces ({
and}
) to surround the code block.
- In Java, the keyword
if
/else
statement- The
if
/else
conditional will run the block of code associated with theif
statement if its Boolean expression evaluates totrue
. - Otherwise, if the Boolean expression evaluates to
false
, it will run the block of code after theelse
keyword.
- The
if
/elseif
/else
statement- If the Boolean expression after the
if
statement evaluates totrue
, it will run the code block that directly follows. - Otherwise, if the Boolean expression after the
else if
statement evaluates totrue
, the code block that directly follow will run. - Finally, if all previous Boolean expressions evaluate to
false
, the code within theelse
block will run.
- If the Boolean expression after the
Ternary Conditional Statement
- It can write
if
/else
statements in a single line of code. - It from a Latin word that means “composed of three parts”:
- A Boolean expression
- A single statement that gets executed if the Boolean expression is true
- A single statement that gets executed if the Boolean expression is false
- E.g.,
(Boolean expression) ? 'T' : 'F';
- It can write
Switch Statement
Java also provides a way to execute code blocks based on whether a block is equal to a specific value.
int restaurantRating = 3;switch (restaurantRating) { case 1: System.out.println("This restaurant is not my favorite."); break; case 2: System.out.println("This restaurant is good."); break; case 3: System.out.println("This restaurant is fantastic!"); break; default: System.out.println("I've never dined at this restaurant."); break;}
The
break
statement will exit theswitch
statement after a condition is met. Without thebreak
statement, Java will continue to check whether the value ofrestaurantRating
matches any other cases.The
default
case is printed only ifrestaurantRating
is not equal to an int with the value of1
,2
, or3
.
Review
- Boolean Operators:
&&
,||
, and!
are used to build Boolean expressions and have a defined order of operations - Statements:
if
,if
/else
, andif
/else if
/else
statements are used to conditionally execute blocks of code - Ternary Conditional: a shortened version of an
if
/else
statement that returns a value based on the value of a Boolean expression - Switch: allows us to check equality of a variable or expression with a value that does not need to be a Boolean
External Resources
- More Java Operators
- Control Flow Statements
- Java Notes-2
- Java Notes-2
- Java Notes (2)
- Java - Notes
- notes[java]
- java notes
- java notes
- Java Notes
- Java Notes
- 【Notes】Effective Java 2nd -- Concurrency
- Effective Java, 2nd - Concurrency - Notes
- Java 游戏开发Notes
- Java Thread - Notes
- Java 游戏开发Notes
- Java-Notes Documents
- java 访问notes domino
- java 读取notes数据库
- Java Container Learning notes
- 各级缓存的使用场景以及限制
- 河南第十届ACM省赛-E-八纵八横
- Java Notes (1)
- 七.shell的图形化工具。
- [css]switch开关按钮,适用于移动端和IE9火狐谷歌
- Java Notes (2)
- Java Notes (3)
- 51Nod 1179 最大的最大公约数
- 话说apm的sitl(simulation in the loop)开发环境的搭建
- Java Notes (4)
- 安卓视图树
- 资源|TensorFlow的71个使用教程与案例(资源汇总)
- 应用统计学与R语言实现学习笔记(七)——拟合优度检验
- ds 线性表 原地置换