Java Conditionals and Loops

167 阅读1分钟

Conditional Statements

If statement

Syntax

if (coondition) {
    // Executes when the condition is true
}

Any of the following comparison operators may be used to form the condition:
< less than
> greater than
!= not equal to
== equal to
<= less than or equal to
>= greater than or equal to

Instance

int x = 7;
if (x < 42) {
    System.out.println("Hi");
}

if...else Statements

Instance

int age = 30;
if (age < 16) {
    System.out.println("Too Young");
} else {
    System.out.println("Welcome!");
}

Nested if Statements

Instance

int age = 25;
if (age > 0) {
    if (age > 16) {
        System.out.println("Welcome!");
    } else {
        System.out.println("Too Young");
    }
} else {
    System.out.println("Error");
}

else if Statements

Instance

int age = 25;
if (age <= 0) {
    System.out.println("Error");
} else if (age <= 16) {
    System.out.println("Too Yonung");
} else if (age < 100) {
    System.out.println("Welcome!");
} else {
    System.out.println("Really?");
}

Logical Statements

The AND Operator

Instance

if (age > 18 && money > 500) {
    System.out.println("Welcome!");
}

The OR Operator

Instance

int age = 25;
int money = 100;
if (age > 18 || money > 500) {
    System.out.printlb("Welcome!");
}

The NOT Operator

int age = 25;
if (!(age > 18)) {
    System.out.printlb("Too Young");
} else {
    System.out.printlb("Welcome!");
}

The switch Statement

Syntax

switch (expression) {
    case value1:
        // statements
        break; // optional
    case value2:
        // statements
        break; // optional
        // You can have any number of case statements.
    default: // optional
        // statements
}

Instance

int day = 3;
switch(day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println("Wednesday");
        break;
}

Instance 2

String dayType = switch(day) {
    case 1,2,3,4,5 -> "Working day";
    case 6,7 -> "Weekend";
    default -> "Invalid day";
}

Notice the -> shorthand after the case.

while Loops

Instance

int x = 3;
while (x>0) {
    System.out.println(x);
    x--;
}

for loops

Syntax

for (initialization; condition; increment/decrement) {
    statement(s)
}

Initialization: Expression executes only once during the beginning of loop
Condition: Is evaluated each time the loop iterates.The loop executes the statement repeatedly, until this condition returns false.
Increment/Decrement: Executes after each iteration of the loop.

Instance

for (int x=1; x <= 5; x++) {
    System.out.println(x);
}

do...while Loops

A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.

Instance

int x = 1;
do {
    System.out.println(x);
    x++;
} while (x<5);

Loop Control Statements

Instance

int x = 1;
while (x>0) {
    System.out.println(x);
    if (x==4) {
        break;
    }
}

Instance

for (int x=10; x<=40; x=x+10) {
    if(x == 30) {
        continue;
    }
    System.out.println(x);
}