Please enable JavaScript.
Coggle requires JavaScript to display documents.
Understanding Java Statements (tips (e.g int x = 1; if(x){}//Does not…
Understanding Java Statements
if-then statement
if(){}
if - keyword
() boolean expression
{} branch if true
tip
e.g if(hourOfDay < 11)
Sysout("greeting");
count++;
count++ will be executed every time, because if don't have braces
if-then-else statement
if(){}else{}
{} branch if true
else {} branch if false
tips
e.g int x = 1;
if(x){}//Does not compile
x is not boolean
e.g int x = 1;
if(x = 5){}//Does not compile
= its not ==
variable modified only right-hand side of ternary operator
Exam has switch without break statements
see if initialization variable in for is outside of loop
Ternary operator
boolean expression ? expresssion1 : expression2;
e.g Sysout((y >5) ? 21 : "Zebra");//ok
e.g int x = (y > 5) ? 9 : "Horse"; //Does not compile
Switch statement
switch(){ case:}
switch keyword
(variableTest)
can be:byte, short, char, int, Byte, Short, Character, Integer, String e Enum.
case constantExpression: branch for case; break;
default: branch default
only use constants in case
parameter as final is not a constant
While Statement
while(booleanExpression){}
do-while statement
do{}while(booleanExpression);
perform at least once
for statement
basic statement
for(initialization; booleanExpression; updateStatement){//body}
for(int x = 0; i < 10; i++){Sysout(x + " ");}
for-each statement
for(datatype instance : collection){//body}