To Documents

Java Control Structures

if..else Statement

if (value == 1) {
    word = "one";
}
else if (value == 2) {
    word = "two";
}
else {
    word = "many";
}

switch Statement

switch (value) {
    case 1:
        word = "one";
        break;
    case 2:  
        word = "two";
        break;
    default: 
        word = "many";
        break;
}

Tertiary Conditional Operator

String word = (value == 1) ? "one" : "many";

while Loop

int n = 1;
while (n <= 100) {
    System.out.print(n + " ");
    n++;
}

do Loop

int = 1;
{
    System.out.print(n + " ");
    n++;
} while (n <= 100);

for Statement, Traditional

for(int n = 1; n <= 100; n++) {
     System.out.print(n + " ");
}

for Statement, Modern

for(int n : a) {
     System.out.print(n + " ");
}