Topics Index

Control Statements > Blocks of code >
Siva Nookala - 12 Apr 2016
Although the terms expression, statement, line, block, condition, and body represent code (or program), there are small differences between them. It is required to know those differences to understand the concepts better. Each of them represent different levels of code and have different importance to programmers and to the Java compiler.

The detailed explanation of each term is given below:
  • expressions - This is the smallest form of code in the programming language. Examples of expression are a + b, 3 * 7, z - 100, 7 > 3, a || b etc.,. When an expression is evaluated it will result in a value and that value has a data type. So all expressions have a datatype. For example 3 + 7 is of data type int, 7.5 / 6.3 is of double data type where 7 > 3 is a boolean. Any expression evaluating to a boolean is also called as condition. These conditions are used in if, while, for as controlling expressions.
  • statement - A statement contains multiple expressions in it and is terminated by a semicolon (;). e.g., int a = (7 + 3) * (5 - 2); System.out.println("Hello"); A statement does not have any data type. They are used in the body of various control statements. They perform certain action like printing, or assigning the result of an expression to a variable etc.,.
  • line of code - Since Java is free form language and the spaces, new lines and tabs in the code are not significant, there is no real importance of 'lines of code' for compiler. But this term is more significant for human users who read, write and edit the code. Usually we put one statement in one line, but there is no restriction on the number of statements in a line or the number of lines a statement can take.
One statement in one line:
int a = 7;
int b = 8;
Multiple statements in one line:
int a = 7; float b = 5.3; double c = b * a;
One statement in multiple lines:
String longText = "This " +
        "statement " +
        "spans " +
        "multiple " +
All the above ways writing the statements is valid. To make the program human readable it is suggested that we write one statement per line. If we have very long statement which takes more than 80 characters it is suggested to wrap (or split) into smaller lines so that no line is longer than 80 characters.
  • block of code - A block of code comprises of one or more statements enclosed in curly braces { }. Similar to statements, a block of code can be spread across multiple lines or it could be in a single line or a single line could contain multiple blocks. The compiler does not have problems with this, but to make code human readable it is suggested to include them in multiple lines. A block of code can be used every where a statement is used. For e.g., the bodies of while, for, do-while, if and switch are all blocks of code.
  • body - All the control statements have body. This body could be a statement or a block of code. For e.g., if-else has two bodies one for if and one for else. for, while, do-while have one body each. switch also has only one body. Although statements can be used as bodies for all control statements except switch, it is suggested to use blocks of code enclosed in curly braces instead of statements. This will increase program readability and reduces errors.
if (x > y) a = b; b = c;
Here even though we have written two statements, assuming that both will be part of the if body, it is not true. Since only the first statement after the if condition is included in the if body. The other statement b = c; is not part of the if body and hence it is executed where the boolean expression x > y is true or false.

Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.