Java Blocks Code

Introduction

We can group two or more statements to into blocks of code by enclosing the statements between opening and closing curly braces {}.

It is also called code blocks.

The block of code is a logical unit that can be used any place that a single statement can.

For example, a block can be a target for Java if and for statements.

The following program uses a block of code as the target of a for loop.

/*                                                                                              
  Demonstrate a block of code.                                                                  
*/  /*w  ww  . ja va2 s  .  c om*/
public class Main {  
  public static void main(String args[]) {  
    int x, y;  
  
    y = 5;  
  
    // the target of this loop is a block  
    for(x = 0; x<10; x++) {  
      System.out.println("This is x: " + x);  
      System.out.println("This is y: " + y);  
      y = y - 2;  
   }  
  }  
} 



PreviousNext

Related