Things to Know Part 2
Operators:
source: http://www.cs.bilkent.edu.tr/~guvenir/courses/CS101/op_precedence.html
Precedence | Operator | Type | Associativity |
15 | () [] · | Parentheses Array subscript Member selection | Left to Right |
14 | ++ -- | Unary post-increment Unary post-decrement | Right to left |
13 | ++ -- + - ! ~ ( type ) | Unary pre-increment Unary pre-decrement Unary plus Unary minus Unary logical negation Unary bitwise complement Unary type cast | Right to left |
12 | * / % | Multiplication Division Modulus | Left to right |
11 | + - | Addition Subtraction | Left to right |
10 | << >> >>> | Bitwise left shift Bitwise right shift with sign extension Bitwise right shift with zero extension | Left to right |
9 | < <= > >= instanceof | Relational less than Relational less than or equal Relational greater than Relational greater than or equal Type comparison (objects only) | Left to right |
8 | == != | Relational is equal to Relational is not equal to | Left to right |
7 | & | Bitwise AND | Left to right |
6 | ^ | Bitwise exclusive OR | Left to right |
5 | | | Bitwise inclusive OR | Left to right |
4 | && | Logical AND | Left to right |
3 | || | Logical OR | Left to right |
2 | ? : | Ternary conditional | Right to left |
1 | = += -= *= /= %= | Assignment Addition assignment Subtraction assignment Multiplication assignment Division assignment Modulus assignment | Right to left |
if Statements:
if(boolean condition){
expression
}else if(second boolean condition){
}else if(third boolean expression){
}else{
}
//if there are no brackets {}, if/else statement applies to the first following expression
switch Statements:
switch(variable){
case value 1: expression
break; //if no break, all cases execute until a break is reached
case value 2:
case value 3:
}
while Loops:
while(boolean condition){
expression
//repeats as long as condition stays true
}
do while Loops:
do{
expression
//same as while loop but guaranteed to run at least once
}while(boolean condition); //<--don't forget semicolon
for Loops:
for(start expression; boolean condition; expression to run at end of loop){
expression
}
Labels, Breaks, and Continues:
labels:
can be used before any block of code
LABEL: while(boolean condition){
//loop code
continue LABEL; //<-- code jumps to label tag
}
breaks:
jumps out of a block of code. Works with if statements, switch statements, while loops, do-while loops, and for loops.
continue:
skips code to next iteration of loop. Works with while loops, do-while loops, and for loops.
while(true){
int x = getInput();
if(x > 4){
continue;
}else{
break;
}
}
source:
Boyarsky, Jeanne, and Scott Selikoff. OCA Oracle Certified Associate. Study Guide. John Wiley & Sons, 2015.