menu

Java Control Statements - Important Points

Java Control Statements - MCQ


Control flow statements are used in programming to control the flow of execution of the program. In Java, there are three types of control flow statements: conditional statements, loop statements, and branching statements.

Conditional statements

Conditional statements, as the name suggests, execute a block of code based on a condition. The most commonly used conditional statement in Java is the if statement. The if statement takes a boolean expression and executes the statement block if the expression is true. For example:

if (a > b) {

    System.out.println("a is greater than b");

}

If a is greater than b, the statement block inside the if statement will be executed and the message "a is greater than b" will be printed.

The if statement can also be followed by an else statement, which executes a different statement block if the boolean expression is false. For example:

if (a > b) {

    System.out.println("a is greater than b");

} else {

    System.out.println("b is greater than a");

}

If a is not greater than b, the statement block inside the else statement will be executed and the message "b is greater than a" will be printed.

Loop statements

Loop statements are used to execute a block of code repeatedly. The most commonly used loop statement in Java is the for loop. The for loop takes three expressions: an initialization expression, a boolean expression, and an increment expression, and executes the statement block as long as the boolean expression is true. For example:

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

    System.out.println(i);

}

This code will print the numbers 0 to 4, because the for loop initializes i to 0, checks if i is less than 5, executes the statement block, increments i, checks again if i is less than 5, executes the statement block again, and so on, until i is not less than 5.

The while loop and the do-while loop are also loop statements in Java. The while loop executes the statement block as long as the boolean expression is true, and the do-while loop executes the statement block at least once, and then checks the boolean expression.

Branching statements

Branching statements are used to transfer the control flow of the program to a different part of the code. The most commonly used branching statement in Java is the break statement, which terminates the current loop or switch statement. The continue statement is also a branching statement in Java, which skips the remaining statements in the current iteration of the loop and moves on to the next iteration.

Complete list of control flow statements in Java

if statement: Executes a block of code based on a boolean condition.

if-else statement: Executes one block of code if the condition is true, and a different block of code if the condition is false.

if-else-if statement: Executes different blocks of code depending on multiple conditions.

switch statement: Executes a block of code based on the value of an expression.

while loop: Executes a block of code repeatedly as long as the boolean expression is true.

do-while loop: Executes a block of code once, and then repeatedly executes the block as long as the boolean expression is true.

for loop: Executes a block of code repeatedly for a fixed number of times.

foreach loop: Executes a block of code for each element in an array or collection.

break statement: Terminates the current loop or switch statement.

continue statement: Skips the remaining statements in the current iteration of the loop and moves on to the next iteration.

return statement: Exits a method and returns a value to the caller.

throw statement: Throws an exception, which can be caught and handled by an exception handler.

try-catch block: Tries to execute a block of code, and catches any exceptions that occur.

try-catch-finally block: Tries to execute a block of code, catches any exceptions that occur, and executes a block of code regardless of whether an exception was thrown.

assert statement: Tests whether a boolean expression is true, and throws an AssertionError if it is false.

These control flow statements are the building blocks of any Java program, and mastering them is essential for writing efficient and effective code.

Subscribe for Latest Career Trends
Subscribe Now
Use AI and ChatGPT for Career Guidance

Unlock Your Future

Join Now
Worried for Placements in 2024?

Join FAST TRACK Course

Join Now
Supercharge Your SUCCESS

Join All in One Placement Mock Tests-2024

Join Now