More Transfer Control Statements (Returning & Labeling)

In the previous tutorial I discussed about 2 of the min transfer control statements named break and continue.

Now we are going to look further into control statements.

RETURN

The return statements is related to functions and it is used to exit a method and optionally return a value to an expression.

If a function does not return a value, it must have the keyword void in the method declaration.

For example :

6

This line of code explain the way to use the return statement with an integer returning to the called expression.

7

Whenever you do not need to return you have to used void keyword in the function declaration.

 

LABEL

Labelled statement is used in conjunction with break and continue statements. This can be used simply to jump over loops.

Check this out labeling with break statement:

capture2
With output
3
More zoomed image

You can observe that, at the break statement, it breaks the loop and jumps on to the top of the code and runs the code until all the conditions are unsatisfied.

Now see this example for labeling with continue statement :

4
With output
5
More zoomed view

Similar to break , the code acts the same with the continue.

Thank you.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s