Statements in Java Part-1

overview of statements:
A statement may change the computer's state: the value of variables, fields, array elements, the
contents of files, and so on. More precisely, execution of a statement
  • terminates normally (meaning execution will continue with the next statement, if any); or
  • terminates abruptly by throwing an exception; or exits by executing a return statement (if inside a method or constructor); or
  • exits the current iteration of a loop and starts a new iteration by executing a continue statement (if inside a loop); or
  • does not terminate at all, for instance, by executing while (true) {}.
Expression Statements:
An expression statement is an expression followed by a semicolon:
expression ;

It is executed by evaluating the expression and ignoring its value. The only forms of expression that may be legally used in this way are assignment expressions , increment and decrement expressions , method call expressions , and object creation expressions.

For example, an assignment statement x=e; is an assignment expression x=e followed by a semicolon. Similarly, a method call statement is a method call expression followed by semicolon. The value returned by the method, if any, is discarded; the method is executed only for its side effect.

Block Statements:
A block-statement is a sequence of zero or more statements or variable-declarations or class declarations, in any order, enclosed in braces:


Choice Statements:

The if Statement
An if statement has the form
if (condition)
true branch

The condition must have type boolean, and true branch is a statement. If condition evaluates to true, then true branch is executed, otherwise not.curly braces are not mandatory.after if statement without curly braces we can allowed only one statement,but that statement is not declarative statement.

For Example int a=10;
int c=20; //compile time error

int a=10;
int b=20; //run successfully

The if-else Statement
An if-else statement has the form:
if (condition)
true branch

false branch

The condition must have type boolean, and true branch and false branch are statements. If condition evaluates to true, then true branch is executed; otherwise false branch is executed.

For Example static int wdaynol(String wday) {

if (wday.equals("Monday")) return 1;
else if (wday.equals("Tuesday")) return 2;
else if (wday.equals("Wednesday")) return 3;
else if (wday.equals("Thursday")) return 4;
else if (wday.equals("Friday")) return 5;
else if (wday.equals("Saturday")) return 6;
else if (wday.equals("Sunday")) return 7;
else return -1; // Here used to mean 'not found'

The switch Statement:
a switch can work only on integer types (including char):
A switch statement has the form
switch (expression) {
case constant1: branch1
case constant2: branch2
default: branch n

The expression must have type int, short, char, or byte. Each constant must be a compile-time
constant expression, consisting only of literals, final variables, final fields declared with explicit field initializers, and operators. No two constants may have the same value. The type of each constant must be a subtype of the type of expression.

Each branch is preceded by one or more case clauses and is a possibly empty sequence of
statements, usually terminated by break or return (if inside a method or constructor) or continue (inside a loop). The default clause may be left out.

The switch statement is executed as follows: The expression is evaluated to obtain a value v. If v
equals one of the constants, then the corresponding branch is executed. If v does not equal any of the constants, then the branch following default is executed; if there is no default clause, nothing is executed. If a branch is not exited by break or return or continue, then execution continues with the next branch in the switch regardless of the case clauses, until a branch exits or the switch ends.

Here we could have used a sequence of if-else statements, but a switch is both faster and clearer:
static String findCountry(int prefix) {
switch (prefix) {
case 1: return "North America";
case 44: return "Great Britain";
case 45: return "Denmark";
case 299: return "Greenland";
case 46: return "Sweden";
case 7: return "Russia";
case 972: return "Israel";
default: return "Unknown";


Block Statements
All method bodies and constructor bodies are block statements.



No comments:

Post a Comment