If, Else If, Else Control, For Loops and Ternary Operator


If / Else If / Else Control

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 condition is checked only if all the conditions before it (in previous else if constructs, and the parent if constructs) have been tested to false. In this example, the else if condition will only be checked if i is greater than or equal to 2.

If its result is true, its block is run, and any else if and else constructs after it will be skipped.

If none of the if and else if conditions have been tested to true, the else block at the end will be run.


If / Else If / Else Control

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 placed just inside the { on the first run. Then the condition is checked, if it is true the body of the loop will execute, if it is false the loop will stop. Assuming the loop continues, the body will execute and finally when the } is reached the increment statement will execute just before the condition is checked again.

The curly braces are optional (you can one line with a semicolon) if the loop contains just one statement. But, it's always recommended to use braces to avoid misunderstandings and bugs.

The for loop components are optional. If your business logic contains one of these parts, you can omit the corresponding component from your for loop.

int i = obj.getLastestValue(); // i value is fetched from a method
 
for (; i < 100; i++) { // here initialization is not done
     System.out.println(i);
}
 
The for (;;) { function-body } structure is equal to a while (true) loop.

Nested For Loops

Any looping statement having another loop statement inside called nested loop. The same way for looping having more inner loop is called 'nested for loop'.

for(;;){
	//Outer Loop Statements
	for(;;){
		//Inner Loop Statements
	}
	//Outer Loop Statements
}

Nested for loop can be demonstrated to print triangle shaped numbers.

for(int i=9;i>0;i--){//Outer Loop
	System.out.println();
	for(int k=i;k>0;k--){//Inner Loop -1
		System.out.print(" ");
	}
 
	for(int j=i;j<=9;j++){//Inner Loop -2
		System.out.print(" "+j);
	}
}

Ternary Operator

Sometimes you have to check for a condition and set the value of a variable.

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";

The value of the variable is set to the value immediately after the condition, if the condition is true. If the condition is false, the second value will be given to the variable.

Basic Programs