• No results found

Transfer Statements

In document Java Programming (Page 35-40)

Java provides six language constructs for transferring control in a program:

break

continue

return

try-catch-finally

throw

assert.

Note that Java does not have a goto statement, although goto is a reserved word.

break Statement

The break statement comes in two forms: the unlabeled and the labeled form.

break; // the unlabeled form

Written by : Shishav Jain Mob:8290280309 Page Number :36 break <label>; // the labeled form

The unlabeled break statement terminates loops (for, while, do-while) and switch statements which contain the break statement, and transfers control out of the current context (i.e., the closest enclosing block). The rest of the statement body is skipped, terminating the enclosing statement, with execution continuing after this statement.

In Below Example , the break statement at (1) is used to terminate a for loop. Control is transferred to (2) when the value of i is equal to 4 at (1), skipping the rest of the loop body and terminating the loop.

Example also shows that the unlabeled break statement only terminates the innermost loop or switch statement that contains the break statement.

The break statement at (3) terminates the inner for loop when j is equal to 2, and execution continues in the outer switch statement at (4) after the for loop.

Written by : Shishav Jain Mob:8290280309 Page Number :37

A labeled break statement can be used to terminate any labeled statement that contains the break statement. Control is then transferred to the

statement following the enclosing labeled statement. In the case of a labeled block, the rest of the block is skipped and execution continues with the statement following the block:

Like the break statement, the continue statement also comes in two forms:

the unlabeled and the labeled form.

continue; // the unlabeled form continue <label>; // the labeled form

The continue statement can only be used in a for, while, or do-while loop to prematurely stop the current iteration of the loop body and proceed with the next iteration, if possible. In the case of the while and do-while loops, the rest of the loop body is skipped, that is, stopping the current iteration, with execution continuing with the <loop condition>. In the case of the for loop, the rest of the loop body is skipped, with execution continuing with the

<increment expression>.

In Example , an unlabeled continue statement is used to skip an iteration in a for loop. Control is transferred to (2) when the value of i is equal to 4 at (1), skipping the rest of the loop body and continuing with the <increment

expression> in the for statement.

Written by : Shishav Jain Mob:8290280309 Page Number :38 // (2). Continue with increment expression.

} // end for

} }

Output from the program:

1 1.0

2 1.4142135623730951 3 1.7320508075688772 5 2.23606797749979 return Statement

The return statement is used to stop execution of a method and transfer control back to the calling code (a.k.a. the caller). The usage of the two forms of the return statement is dictated by whether it is used in a void or a non-void method. The first form does not return any value to the calling code, but the second form does. Note that the keyword void does not represent any type.

The <expression> must evaluate to a primitive value or a reference value, and its type must be assignable to the return type in the method prototype.

A void method need not have a return statement —in which case control normally returns to the caller after the last statement in the method's body has been executed.

return Statement

Form of return Statement In void Method In Non-void Method

return; optional not allowed

return <expression>; not allowed mandatory

2.4 Classes

• Classes is a concept through which we can represent real world entity, Suppose if we want to represent house then class is used to describe the features of house

• Classes acts like a blueprint of the object. Through classes , we can define properties and behaviours of the object which is used to differentiate one object from other object .

In java , properties of an object of a class, also called attributes, are defined using variables in java. Behaviours of an object of the class also known as operations , are defined using methods /functions in java.

Written by : Shishav Jain Mob:8290280309 Page Number :39

• Class made the distinction between the contract and implementation provides for its object .

Contract defines what services , and implementation defines how these services are provided by the classes.

• Through classes , we can achieve the abstraction in java . Abstraction is one of the fundamental way to handle the complexity . Abstraction denotes the essential properties and behaviours of an object .

Class syntax:

class [className]

{

//Propeties of class by defining the variables in class , also called class //variables or instance variables ;

//Behaviours of class by defining the methods in class , also called class //methods

}

Ex: Suppose we want to implement the student ,so we create a student class having student_id and student_name as properties of the class and methods that set the student_id and student_name and methods that return the student_id and student_name on request.

class student

Written by : Shishav Jain Mob:8290280309 Page Number :40 }

}

• Both class variables and methods constitutes the class Members.

• UML Notation

2.5 Objects

• An objects is an instance of the class .

• Objects are something that have real existence . Classes without objects are not worth creating . Objects are the handler for accessing the class data members and member functions.

• An object must be explicitly created before it can be used in a program

• In java , objects are manipulated through object references.

• Process of creating object in java involves

1. Declaration of reference variable

In document Java Programming (Page 35-40)