Thursday, 6 June 2013

4th day of traning


Java Statements


Methods and constructors are sequences of statements, along with variable definitions. The statements specify the sequence of actions to be performed when a method or constructor is invoked. They can alter the value of variables, generate output, process input, or respond to user mouse or keyboard actions.
Different types of statements are described in the following sections.

Assignment Statements

An assignment statement has the following form.
    variable = expression;
This statement changes the value of the variable on the left side of the equals sign to the value of the expression on the right-hand side. The variable is often just specified by a variable name, but there are also expressions that specify variables.
Java treats an assignment as both an expression and as a statement. As an expression, its value is the value assigned to the variable. This is done to allow multiple assignments in a single statement, such as
    a = b = 5;
By treating b = 5 as an expression with value 5, Java makes sense of this statement, assigning the value 5 to both a and b.



Statement Blocks

In Java, any sequence of statements can be grouped together to function as a single statement by enclosing the sequence in braces. These groupings are called statement blocks. A statement block may also include variable declarations.
Statement blocks are used to define methods and to allow multiple statements in the control structures described in the following sections.

Control Statements

Normally, statements in a method or constructor are executed sequentially. Java also has control statements that allow repetitive execution of statements and conditional execution of statements. Java has the following types of control statements.

Conditional Execution and Selection

Java has three kinds of statements that permit execution of a nested statement based on the value of a boolean expression or selection among several statements based on the value of a boolean expression or a control variable. These statements are the if statement, the if-else statement, and the switch statement.

If Statements
The if statement has the following form.
    if (boolean-expression) {
 then-clause
    }
Here,
  • boolean-expression is an expression that can be true or false.
  • then-clause is a sequence of statements. If there is only one statement in the sequence then the surrounding braces may be omitted. The then-clause statements are executed only if the boolean-expression is true.

If-Else Statements
The if-else statement has the following form.
    if (boolean-expression) {
 then-clause
    } else {
 else-clause
    }
Here,
  • boolean-expression is an expression that can be true or false.
  • then-clause and else-clause are sequences of statements. If there is only one statement in a sequence then the surrounding braces may be omitted. The then-clause statements are executed only if the boolean-expression is true. The else-clausestatements are executed if the boolean-expression is false.

No comments:

Post a Comment