Control Statements In Java

Java Control Statements are instructions that are used to control the flow of execution in a Java program. They help to make programs more efficient, logical, and structured. Java provides three types of control statements: conditional statements, looping statements, and branching statements. Let's take a closer look at each of these control statements.

 

Java provides three types of control flow statements.

  1. Conditional Statements

    • if statements
    • switch statement
  2. Loop statements
    • do while loop
    • while loop
    • for loop
    • for-each loop
  3. Jump statements/Branching Statements
    • break statement
    • continue statement

 

 

  1. Conditional Statements: Conditional statements in Java are used to test whether a particular condition is true or false. If the condition is true, a set of statements is executed; otherwise, another set of statements is executed. The most commonly used conditional statement in Java is the 'if-else' statement. It has the following syntax:

    a) If Statement -  The "if" statement in Java is used to assess a condition. Depending on the unique circumstance, the control of the programmed is changed. The If statement's condition returns a Boolean value, either true or false. The four different sorts of if-statements in Java are listed below.

    1. Simple if statement: The simple if statement is the most basic conditional statement in Java. It allows you to execute a single statement if the condition is true. The syntax of the simple if statement is:

    if (condition) { // statement to be executed if the condition is true }

    Example:


    int x = 10; if (x > 5) { System.out.println("x is greater than 5"); }

    Output: x is greater than 5

  2. if-else statement: The if-else statement allows you to execute one set of statements if the condition is true and another set of statements if the condition is false. The syntax of the if-else statement is:

if (condition) { // statement to be executed if the condition is true } else { // statement to be executed if the condition is false }

Example:


int x = 2; if (x % 2 == 0) { System.out.println("x is even"); } else { System.out.println("x is odd"); }

Output: x is even

  1. if-else-if ladder: The if-else-if ladder statement allows you to evaluate multiple conditions and execute the corresponding code block if the condition is true. The syntax of the if-else-if ladder statement is:

if (condition1) { // statement to be executed if condition1 is true } else if (condition2) { // statement to be executed if condition2 is true } else if (condition3) { // statement to be executed if condition3 is true } else { // statement to be executed if all conditions are false }

Example:


int x = 50; if (x < 0) { System.out.println("x is negative"); } else if (x < 50) { System.out.println("x is less than 50"); } else if (x < 100) { System.out.println("x is less than 100"); } else { System.out.println("x is greater than or equal to 100"); }

Output: x is less than 100

  1. Nested if statement: A nested if statement is an if statement inside another if statement. It allows you to evaluate multiple conditions and execute the corresponding code block. The syntax of the nested if statement is:

if (condition1) { // statement to be executed if condition1 is true if (condition2) { // statement to be executed if both condition1 and condition2 are true } }

Example:


int x = 10; if (x > 5) { if (x < 15) { System.out.println("x is between 5 and 15"); } }

Output: x is between 5 and 15

In conclusion, Java provides several conditional statements that allow you to control the flow of execution based on certain conditions. Simple if statements, if-else statements, if-else-if ladder statements, and nested if statements are some of the most commonly used conditional statements in Java.


  1. Looping Statements: Looping statements are used to repeat a set of statements multiple times. Java provides three types of looping statements: 'for' loop, 'while' loop, and 'do-while' loop. The syntax of each loop is as follows:

    I ) Java for loop

    Java's for loop is comparable to those in C and C++. Using only one line of code, we can initialize the loop variable, verify the condition, and increment or decrement. Only when we are certain of the number of times we want to run the block of code do we utilize the for loop. 

    Java for Loop (With Examples)

a. 'for' loop:


for(initialization; condition; increment/decrement) { // code to be executed }

Example:


for(int i=0; i<5; i++) { System.out.println("The value of i is: " + i);
 
 }
 
 
  

 Java while and do...while Loop 

 

b. 'while' loop:


while(condition) { // code to be executed }

Example:


int i=0; while(i<5) { System.out.println("The value of i is: " + i); i++; }
 
 
 

 
 

c. 'do-while' loop:


do { // code to be executed } while(condition);

Example:


int i=0; do { System.out.println("The value of i is: " + i); i++; } while(i<5);
  1. Branching Statements: Branching statements are used to transfer the control of the program to a different part of the program. The most commonly used branching statements in Java are 'break' and 'continue' statements. The syntax of each statement is as follows:

a. 'break' statement:

break;

Example:


for(int i=0; i<5; i++) { if(i==3) { break; } System.out.println("The value of i is: " + i); }

b. 'continue' statement:

continue;

Example:


for(int i=0; i<5; i++) { if(i==3) { continue; } System.out.println("The value of i is: " + i); }

In conclusion, Java Control Statements are crucial in programming as they enable developers to control the flow of execution in a program. They help to make programs more efficient, logical and help in controlling how and when particular tasks are executed.