Code Block, Indentation and Statement


Code blocks have been discussed earlier. this tutorial deals with the Code Block, Indentation and Statement.

Code Block

Code block in Java is used for grouping of two or more statements. The code in Java always start with curly braces and end with close curly braces.

A code block can be used inside a block. There is no restriction number of block and the level of nesting the block can be nested and can be included inside another Code block. It is generally used in loops statement (For, while loop, do while loop). Class and method content are blocks too.

Code Indentation

Indentation is part of style guide which is used to organize your source code in a good manner. An indentation is a group of style. This style should be followed by every programmer. If we follow proper style guide then the reader will be comfortable and understand the meaning of the program. The purpose of code indentation and style guide is to make the code:

  • Easy to read
  • Easy to understand
  • Easy to modify
  • Easy to maintain
  • Easy to enhance

A style guide provides a roadmap which should be followed by the developer which helps to make the consistency in all code. Sometimes it happens due to time presser programmer do not give more time and generate the code without indentation. But the good code is very necessary. If we give more time it will help to escape the trouble at the later stage. As we know the program is written only once during coding but it is read many times at a later stage.

Incorrect Indentation

Correct Indentation

Statement

In Java programming, we have statements and instruction.

The statement is a complete set of information which provides some information to a programming language. In the same way how we have a sentence in English. Now when we see a full stop in English, it means that the sentence has ended. Likewise, in order to make complete command understand that statement has ended, we have statement terminators. In Java programming language the statement terminator is a semi-colon;

Types of statement

  • Expression statement
  • Declaration statement
  • Control flow statement

Expression statement

The expression is any section of the code that is used to evaluate the condition.

Declaration statement

Statement of the declaration is used to declare a variable.

Control flow statement

Control flow statement is If block, for, while loop. It is used to execute a set of statements repeatedly for multiple times.

Contributed by: Poonam Tomar


Full stack Java Developer

   Buy me a coffee!

 

PayTM QR (INDIA)

Download file

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu