Java Notes (2) - Conditionals and Control Flow
2017-05-10 01:36
337 查看
转载请注明出处: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.
It returns a boolean value of
It returns a Boolean value of true when at least one expression on either side of
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.
In Java, the keyword
It is followed by a Boolean expression and then a block of code. If the Boolean expression evaluates to
The
The
Otherwise, if the Boolean expression evaluates to
If the Boolean expression after the
Otherwise, if the Boolean expression after the
Finally, if all previous Boolean expressions evaluate to
Ternary Conditional Statement
It can write
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.,
Switch Statement
Java also provides a way to execute code blocks based on whether a block is equal to a specific value.
The
The
Statements:
Ternary Conditional: a shortened version of an
Switch: allows us to check equality of a variable or expression with a value that does not need to be a Boolean
Control Flow Statements
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
trueonly when the expressions on both sides of
&&are true.
||: or operator
It returns a Boolean value of true when at least one expression on either side of
||is true.
!: not operator
It will return the opposite of the expression immediately after it. It will return
falseif the expression is true, and
trueif the expression is false.
Boolean Operator Precedence
!>
&&>
||
Every expression within parentheses is evaluated first.
Expressions are also read from left to right.
Conditional Expressions
ifstatement
In Java, the keyword
ifis 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
ifstatement is not followed by a semicolon (
;). Instead it uses curly braces (
{and
}) to surround the code block.
if/
elsestatement
The
if/
elseconditional will run the block of code associated with the
ifstatement if its Boolean expression evaluates to
true.
Otherwise, if the Boolean expression evaluates to
false, it will run the block of code after the
elsekeyword.
if/
elseif/
elsestatement
If the Boolean expression after the
ifstatement evaluates to
true, it will run the code block that directly follows.
Otherwise, if the Boolean expression after the
else ifstatement evaluates to
true, the code block that directly follow will run.
Finally, if all previous Boolean expressions evaluate to
false, the code within the
elseblock will run.
Ternary Conditional Statement
It can write
if/
elsestatements 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';
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
breakstatement will exit the
switchstatement after a condition is met. Without the
breakstatement, Java will continue to check whether the value of
restaurantRatingmatches any other cases.
The
defaultcase is printed only if
restaurantRatingis not equal to an int with the value of
1,
2, or
3.
Review
Boolean Operators:&&,
||, and
!are used to build Boolean expressions and have a defined order of operations
Statements:
if,
if/
else, and
if/
else if/
elsestatements are used to conditionally execute blocks of code
Ternary Conditional: a shortened version of an
if/
elsestatement 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 OperatorsControl Flow Statements
相关文章推荐
- Conditionals and Control Flow 条件控制
- Python Notes (2) - Conditionals and Control Flow
- Reactive Programming with Rxjava-Chapter6:Flow Control and Backpressure(1)
- SSIS ->> Control Flow And Data Flow
- RUDP之四 —— Reliability and Flow Control(后附所有源码下载)
- Exceptions and control flow
- TCP Flow Control and Data Transfer
- Javascript异步编程 Asynchronous JS: Callbacks, Listeners, Control Flow Libs and Promises
- clisp 记录:5. 数据和控制流(Data and Control Flow)
- TinyOS论文12:Inter-Context Control-flow and Data-FlowText
- iSCSI Flow Control and QoS
- Asynchronous JS: Callbacks, Listeners, Control Flow Libs and Promises
- 评一篇文章(Experiments of the effectiveness of dataflow- and controlflow-based test adequacy criteria)
- 蓝牙LL数据包交互【More Data多数据传输】【Acknowledgement and Flow Control】
- llvm:Call Graph And Control Flow Graph
- Reliability and Flow Control
- 6.087 Practical Programming in C, lec3: Control flow. Functions and modular programming. Variable s
- Reactive Programming with RxJava-Chapter6:Flow Control and Backpressure(2)
- 评一篇文章(Experiments of the effectiveness of dataflow- and controlflow-based test adequacy criteria)
- 课程 Java程序设计4:Expression and Flow Control (怀旧版)