Sunday 13 March 2016

The Java Language: if/else conditionals

We can define an if/else clause as follows:

 if ( condition )  
 statement;  
 [ else  
 statement; ]  
(The whole of the preceding example is itself a statement and could be nested within another if/else clause.) The if clause has the common functionality of taking two different forms: a “one-liner” or a block. The block form is as follows:
 if ( condition ) {  
 [ statement; ]  
 [ statement; ]  
 [ ... ]  
 } else {  
 [ statement; ]  
 [ statement; ]  
 [ ... ]  
 }  
The condition is a Boolean expression. A Boolean expression is a true or false value or an expression that evaluates to one of those. For example i == 0 is a Boolean expression that tests whether the integer i holds the value 0. In the second form, the statements are in code blocks, and all their enclosed statements are executed if the corresponding (if or else) branch is taken. Any variables declared within each block are visible only to the statements within the block. Like the if/ else conditional, most of the remaining Java statements are concerned with controlling the flow of execution. They act for the most part like their namesakes in other languages.

0 comments:

Post a Comment