Compound Statements
A compound statement in Java defines a local scope for variables. Variables declared inside this scope are only accessible within the compound statement itself.
public class FlowControlBasics {
public static void main(String[] args) {
int primaryNum = 15;
{
int secondaryNum = 30;
System.out.println(secondaryNum);
int tertiaryNum = 180;
boolean isValid;
{
isValid = secondaryNum < tertiaryNum;
System.out.println(isValid);
}
}
String greeting = "welcome to java";
System.out.println(greeting);
}
}
Conditional Sttaements
If Statements
- If there is only one statement following the condition, curly braces
{}can be omitted, but it's recomended to include them for clarity. - If there are no statements after the condition, you can use a semicolon
;or just empty curly braces{}.
If...Else Statements
These can be simplified using the ternary operator.
If...Else If and Switch Statements
public class ConditionalExamples {
public static void main(String[] args) {
boolean flag = true;
if (flag) {}
if (flag);
int a = 50;
int b = 20;
if (a > b) {
System.out.println("Variable a is greater than variable b");
}
if (a < b) {
System.out.println("Variable a is less than variable b");
}
int physics = 88;
int chemistry = 45;
if (physics > 60) {
System.out.println("Physics passed");
} else {
System.out.println("Physics failed");
}
if (chemistry > 60) {
System.out.println("Chemistry passed");
} else {
System.out.println("Chemistry failed");
}
int absoluteValue;
int input = -12;
if (input > 0) {
absoluteValue = input;
} else {
absoluteValue = -input;
}
System.out.println(absoluteValue);
// Simplified with ternary operator
absoluteValue = input > 0 ? input : -input;
System.out.println(absoluteValue);
if (physics >= 60 && physics < 90) {
System.out.println("Physics: Pass");
} else if (physics >= 90) {
System.out.println("Physics: Excellent");
} else {
System.out.println("Physics: Fail");
}
String userChoice = "2";
switch (userChoice) {
// Case expressions must be integer, character, or string types
case "1":
System.out.println("1. Withdraw money");
break;
case "2":
System.out.println("2. Deposit money");
break;
case "exit":
break;
// Default is optional
default:
System.out.println("Please enter 1 or 2, enter exit to quit");
}
}
}
Loop Statements
While Loop
Do...While Loop
Executes the loop body first, then checks the while condition.
For Loop
Structure: for(initialization; condition; increment/decrement)
- Initialization: Sets the initial value of loop vairables
- Condition: Determines if the loop should continue
- Increment/decrement: Modifies the loop variable after each iteration
Foreach Statement
Primarily used for convenient traversal of arrays and collections.
public class LoopExamples {
public static void main(String[] args) {
int count = -3;
while (count <= -1) {
count++;
System.out.println("While loop running");
}
do {
count++;
System.out.println("Do while loop running " + count + " times.");
} while (count <= -4);
for (int k = 1; k <= 3; k++) {
System.out.println("For loop running " + k + " times.");
}
int numbers[] = {5, 12, 3};
System.out.println("Items in numbers array:");
for (int num : numbers) {
System.out.println(num);
}
}
}
Loop Control
Break Statement
Breaks out of the current loop body. It can only break out of one level of loop. To break out of outer loops, use labeled loops.
Continue Statement
Skips the remaining statements of the current loop iteration and starts the next iteration. Also supports labeled usage.
public class LoopControlExamples {
public static void main(String[] args) {
System.out.println("---------Break Example---------");
for (int m = 1; m <= 4; m++) {
System.out.println("Running outer loop " + m + " times");
for (int n = 1; n <= 4; n++) {
System.out.println("Running inner loop " + n + " times");
if (n >= 1) {
break;
}
}
}
System.out.println("-------Breaking Outer Loop-------");
OuterLoop: for (int m = 1; m <= 4; m++) {
System.out.println("Running OuterLoop " + m + " times");
for (int n = 1; n <= 4; n++) {
System.out.println("Running inner loop " + n + " times");
if (n >= 1) {
break OuterLoop;
}
}
}
System.out.println("--------Continue Example--------");
for (int m = 1; m <= 4; m++) {
System.out.println("Running loop " + m + " times");
if (m > 1) {
continue;
}
System.out.println("Executing after if condition in the " + m + "th iteration");
}
System.out.println("-------Continuing Outer Loop-------");
OuterLoop: for (int m = 1; m <= 2; m++) {
System.out.println("Running OuterLoop " + m + " times");
for (int n = 1; n <= 4; n++) {
System.out.println("Running inner loop " + n + " times");
if (n > 1) {
continue OuterLoop;
}
System.out.println("Executing after if condition in the " + m + "th iteration");
}
}
}
}