Tutorial by Examples

if (i < 2) { System.out.println("i is less than 2"); } else if (i > 2) { System.out.println("i is more than 2"); } else { System.out.println("i is not less than 2, and not more than 2"); } The if block will only run when i is 1 or less. The else if...
for (int i = 0; i < 100; i++) { System.out.println(i); } The three components of the for loop (separated by ;) are variable declaration/initialization (here int i = 0), the condition (here i < 100), and the increment statement (here i++). The variable declaration is done once as if pl...
int i = 0; while (i < 100) { // condition gets checked BEFORE the loop body executes System.out.println(i); i++; } A while loop runs as long as the condition inside the parentheses is true. This is also called the "pre-test loop" structure because the conditional stateme...
The do...while loop differs from other loops in that it is guaranteed to execute at least once. It is also called the "post-test loop" structure because the conditional statement is performed after the main loop body. int i = 0; do { i++; System.out.println(i); } while (i &l...
Java SE 5 With Java 5 and up, one can use for-each loops, also known as enhanced for-loops: List strings = new ArrayList(); strings.add("This"); strings.add("is"); strings.add("a for-each loop"); for (String string : strings) { System.out.println(string); } For each ...
int i = 2; if (i < 2) { System.out.println("i is less than 2"); } else { System.out.println("i is greater than 2"); } An if statement executes code conditionally depending on the result of the condition in parentheses. When condition in parentheses is true it will ...
The switch statement is Java's multi-way branch statement. It is used to take the place of long if-else if-else chains, and make them more readable. However, unlike if statements, one may not use inequalities; each value must be concretely defined. There are three critical components to the switch...
Sometimes you have to check for a condition and set the value of a variable. For ex. String name; if (A > B) { name = "Billy"; } else { name = "Jimmy"; } This can be easily written in one line as String name = A > B ? "Billy" : "Jimmy&quo...
The break statement ends a loop (like for, while) or the evaluation of a switch statement. Loop: while(true) { if(someCondition == 5) { break; } } The loop in the example would run forever. But when someCondition equals 5 at some point of execution, then the loop ends. If mult...
The try { ... } catch ( ... ) { ... } control structure is used for handling Exceptions. String age_input = "abc"; try { int age = Integer.parseInt(age_input); if (age >= 18) { System.out.println("You can vote!"); } else { System.out.println(...
It's possible to break / continue to an outer loop by using label statements: outerloop: for(...) { innerloop: for(...) { if(condition1) break outerloop; if(condition2) continue innerloop; // equivalent to: continue; } } There is no ...
The continue statement is used to skip the remaining steps in the current iteration and start with the next loop iteration. The control goes from the continue statement to the step value (increment or decrement), if any. String[] programmers = {"Adrian", "Paul", "John&quot...

Page 1 of 1