|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
|
|
|
Notion of a Block
This seems to be an appropriate time to introduce anorganisational construct which is integral to today's programming languages - the notion of a block. We will start with introducing a simple view of a block and expand it gradually later on.
As you may have observed, our programs are getting larger in the sense of number of statements and more complex. When you write long documents, you dont string every sentence one after another in a series in a flat list; we gather sentences into paragraphs, paragraphs into subsections, subsections into sections, sections into chapters, and so on. This helps us to comprehend the structure of a document relatively easily. Imagine figuring out where is something if a 200 page book was just one paragraph. A block in a program is like a paragraph.
Normally a paragraph denotes one idea. Though there are no laws in this regard, it is the recommended practice. Good documents follow this. Similarly, what you put in a block must be related set of statements. Neither the programming language, nor the compiler would do anything to enforce this. But programs that dont follow this tend to be nightmares for those who need to deal with it later on - including the developer who wrote it!
More than convenience, there are situations where such grouping is necessary. Let us look back at our if-statement. Suppose, there are more than one action to do when the condition is true/false. For example, if k > 10, then set i and j to 1. We may write this as:
if (k > 10) i = 1; j = 1;
This actually won't work, because this does not indicate that j = 1 is to be done when i = 1 is done, and not otherwise. The system assumes that i = 1 is to be done when (k > 10) is true. But j = 1 is assumed to be a statement following the if-statement.
In other words, the 'then' part of the if-statement is assumed to be a single statement. To put multiple statements there, we need a grouping device. A block is best suited for this.
What does a block look like? It is a collection of statements, surrounded by a start and end indicator. Java uses '{' and '}' as the block start and end indicators. So,
{ i = 1; j = 1; }
is a block. In general, in Java, as well as in most languages, a block is allowed wherever a single statement is allowed. So, our if-statement should be written as:
if (k > 10) { i = 1; j = 1; }
The choice of where to put the braces is left to you. Choose a convenient convention, and use it consistently. Readability, space usage, etc are some of the considerations in choosing a convention. Here are some common variants, in the case of an if-statement:
if (k > 10) { i = 1; j = 1; }
if (k > 10) { i = 1; j = 1; }
if (k > 10) { i = 1; j = 1; }
Coming back to our if-statement, with the block structure, the statement does what we wanted it to do. See the two code segments together and understand the difference.
if (k > 10) { if (k > 10) i = 1; i = 1; j = 1; j = 1; }
There is more to block structure, that we will see later.
|
|
| | |
|
|
|
|
|
|
|