Control Flow in Java

Block ScopeControl StatementsLoops

In the previous blog, we learned about Input and Output in Java. If you want to know more about it, visit the Input and Output in Java. In this blog, we will learn about Control Flow in Java. In Java, code executes in sequential order(top to bottom). The statements in the code are executed in the order that the code/program appears. In order to control the flow of Java code, some statements are provided, such statements are called Control Flow Statements. To determine the control flow of code, Java provides control statements and loops.

Block Scope

Blocks consist of a number of Java statements that are enclosed within the pair of curly braces. Variables are defined in the block and the scope of variables is within the block. There can be blocks inside the other blocks that are called nested blocks.

e.g

public static void main(String args[]) {
  int value;
  String string;
  ...
  {
      int numValue;
      ...

      /* variable atrowelValue has the scope with this block only */
  }
}

We cannot declare the same variables within the two nested blocks. If we do so, then we will get an error saying cannot redefine the variable in the inner block.

e.g

public static void main(String args[]) {
  int atrowel;
  String string;
  ...
  {
      int atrowelValue;
      int atrowel;    // error
      ...
  }
}

Control Statements

The conditional statement has the following syntax:

if(condition) statement;

The condition should be surrounded by the parenthesis(( )). Just like in other languages, in Java, we can execute multiple statements when the condition is true. In order to execute multiple statements, we use block statements. It has the following syntax:

Syntax

{
  statement1;
  statement2;
  ...
}

In order to check the conditions, control statements are used. The following are the control statements:


if statement

if statement is used to check the condition. If the condition satisfies, then the if statement is executed, but if the condition does not satisfy, then the if statement is not executed. The syntax is:

Syntax

if(condition){
  statement1;
  ...
}
...

Flowchart

...

e.g

public class IfEx {
  public static void main(String args[]) {
      int numValue = 15;
      if(numValue > 10) {
          System.out.println("Value is greater than 10");
      }
  }
}

Output

Value is greater than 10

if-else statement

if-else statement is used for testing the condition. If the condition in the if block satisfies, then it executes the if statement, otherwise executes the else statement. if-else has the following syntax:

Syntax

if(condition) {
  statement1;
  ...
} else {
  statement2;
  ...
}

In the if-else statement the else part is not mandatory(optional). An else statement groups with the if, which is nearest to it.

Flowchart

...

e.g

public class IfElseEx {
  public static void main(String args[]) {
      int value1 = 20;
      int value2 = 30;

      if(value1 < value2){
          System.out.println("Value1 is greater");
      } else {
          System.out.println("Value2 is greater");
      }
  }
}

Output

Value1 is greater

if-else if Statement

if-else if statement is used for executing one block of code among the multiple blocks. The general syntax is:

Syntax

if(condition){
  statement1;
  ...
} else if(condition){
  statement2;
  ...
}
...
} else {
  statement3;
  ...
}

Flowchart

...

e.g

public class IfElseEx {
  public static void main(String args[]) {
      int value1 = 10;
      int value2 = 20;
      int value3 = 30;
      if(value1 > value3) {
          System.out.println("value1 is greater than value3");
      } else if(value2 > value3) {
          System.out.println("value2 is greater than value3");
      } else {
          System.out.println("value3 is greater");
      }
  }
}

Output

value3 is greater

Core Java Tutorial

Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.