Monday, September 29, 2008

BRANCHING STATEMENTS

Branching Statements:
Break statements:
The break statement is a branching statement that contains two forms: labeled and unlabeled. The break statement is used for breaking the execution of a loop (while, do-while and for) . It also terminates the switch statements.
Syntax:
break; // breaks the innermost loop or switch statement.
break label; // breaks the outermost loop in a series of nested loops.
Example: When if statement evaluates to true it prints "data is found" and comes out of the loop and executes the statements just following the loop.
Continue statements:
This is a branching statement that are used in the looping statements (while, do-while and for) to skip the current iteration of the loop and resume the next iteration .
Syntax: continue;Example:
Return statements:It is a special branching statement that transfers the control to the caller of the method. This statement is used to return a value to the caller method and terminates execution of method. This has two forms: one that returns a value and the other that can not return. the returned value type must match the return type of method.
Syntax:
return;
return values;
return; //This returns nothing. So this can be used when method is declared with void return type.return expression; //It returns the value evaluated from the expression.
Example: Here Welcome() function is called within println() function which returns a String value This is printed to the screen.

No comments: