Java control flow cheat sheet

Judith 12 Published: 10/15/2024

Java control flow cheat sheet

I'll provide the Java control flow cheat sheet in English.

Control Flow Statements

if-statement: Used to execute a block of code if a certain condition is met. Syntax: if (condition) { code } Example:
int x = 5;

if (x > 10) {

System.out.println("x is greater than 10");

} else {

System.out.println("x is not greater than 10");

}

else-statement: Used to specify an alternative block of code to execute if the condition in the if-statement is not met. Syntax: if (condition) { code } else { code } Example:
int x = 5;

if (x > 10) {

System.out.println("x is greater than 10");

} else {

System.out.println("x is not greater than 10");

}

switch-statement: Used to execute a block of code based on the value of an expression. Syntax: switch (expression) { case constant: code; break; [case constant: code; break;] ... } Example:
int dayOfWeek = 2;

switch (dayOfWeek) {

case 1:

System.out.println("Monday");

break;

case 2:

System.out.println("Tuesday");

break;

default:

System.out.println("Unknown day of the week");

}

for-loop: Used to execute a block of code repeatedly for a specified number of iterations. Syntax: for (initialization; condition; increment) { code } Example:
int sum = 0;

for (int i = 0; i < 10; i++) {

sum += i;

}

System.out.println("The sum is " + sum);

while-loop: Used to execute a block of code repeatedly while a certain condition is met. Syntax: while (condition) { code } Example:
int i = 0;

while (i < 10) {

System.out.println("i is " + i);

i++;

}

do-while-loop: Used to execute a block of code repeatedly while a certain condition is met. Syntax: do { code } while (condition); Example:
int i = 0;

do {

System.out.println("i is " + i);

i++;

} while (i < 10);

break-statement: Used to terminate the execution of a loop or switch statement. Syntax: break; Example:
for (int i = 0; i < 10; i++) {

if (i == 5) {

break;

}

System.out.println("i is " + i);

}

continue-statement: Used to skip the rest of the loop and go back to the beginning. Syntax: continue; Example:
for (int i = 0; i < 10; i++) {

if (i == 5) {

continue;

}

System.out.println("i is " + i);

}

return-statement: Used to exit a method and return a value. Syntax: return value; Example:
public int add(int x, int y) {

int result = x + y;

return result;

}

throw-statement: Used to throw an exception. Syntax: throw exception; Example:
public void checkAge(int age) throws AgeException {

if (age < 18) {

throw new AgeException("You must be at least 18 years old");

}

}

try-catch-finally-statement: Used to handle exceptions. Syntax: try { code } catch (exception) { code } finally { code }; Example:
public void checkAge(int age) {

try {

if (age < 18) {

throw new AgeException("You must be at least 18 years old");

}

} catch (AgeException e) {

System.out.println(e.getMessage());

} finally {

System.out.println("The age is " + age);

}

}

This Java control flow cheat sheet provides a comprehensive guide to the various control flow statements available in Java, including if-statement, else-statement, switch-statement, for-loop, while-loop, do-while-loop, break-statement, continue-statement, return-statement, throw-statement, and try-catch-finally-statement.

What are the two types of controls in Java?

I'm happy to respond in English!

In Java, there are two primary types of control structures: Conditional Control and Loop Control.

1. Conditional Control

Conditional control statements, also known as decision-making statements or selection statements, allow your program to make choices based on conditions you define. They execute a specific block of code depending on whether the condition is true or false. Java has several conditional control statements:

if statement: Executes a block of code if the condition is true. if-else statement: Executes one block of code if the condition is true and another block of code if it's false.

switch statement: Executes different blocks of code based on the value of an expression.

Here's a simple example:

int age = 25;

if (age >= 18) {

System.out.println("You are an adult.");

} else {

System.out.println("You are a minor.");

}

2. Loop Control

Loop control statements, also known as iteration statements, allow your program to repeat a block of code multiple times. There are several loop control statements in Java:

while statement: Executes a block of code while the condition is true. do-while statement: Similar to while, but executes the code at least once before checking the condition. for statement: Used for iterating over an array, collection, or range.

Here's a simple example:

int count = 0;

while (count < 5) {

System.out.println("Looping...");

count++;

}

In this example, the loop will execute five times, printing "Looping..." each time and incrementing count by one.

These two types of control structures enable you to write more robust and flexible programs in Java. You can use conditional statements to make decisions and loop control statements to repeat tasks or iterate over data.

I hope this explanation helps!