Saturday, May 10, 2008

Some more statements (The break Statement )

Some more statements

The break Statement

We have already met break in the discussion of the switch
statement. It is used to exit from a loop or a switch,
passing control to the first statement beyond the loop or a switch.

With loops, break can be used to force an early exit from
the loop, or to implement a loop with a test to exit in
the middle of the loop body.

A break within a loop should always be protected within
an if statement which provides the test to control
the exit condition.

An Example program to determine whether a number
is prime or not:


output:


The continue Statement

This is similar to break but is encountered less
frequently. It only works within loops where its effect
is to force an immediate jump to the loop control statement.

In a while loop, jump to the test statement.

In a do while loop, jump to the test statement.

In a for loop, jump to the test, and perform the iteration (looping).

Like a break, continue should be protected by an if
statement. You are unlikely to use it very often.

An example of program using continue statement:



output:



The goto Statement

C has a goto statement which permits unstructured jumps
to be made. It requires a label in order to identify the
place where the branch is to be made.

A label is any valid variable name,and must be followed
by a colon(:).

Syntax:

goto label;
..................
label:
statement;

The label can be any where in the program either before
or after the goto label; statement.

An example program using goto statement:



output:



Explanation of the program

Here the program will ask untill you give negative
value at the time when you give positive value then
instantly it will show you the square root value of the given number.

Another use of the goto statement is to transfer the
control out of a loop (or nested loops) when certain
particular conditions are encountered.

Note: We should try to avoid using goto as far as
possible because, it is not good for readability of
the program or to improve the program execution speed.

The exit() function

This function is used for terminating the execution of C program.

Syntax:

exit(int status);
--------------------------------------------------------------

0 comments:

Copyright

Copyright © 2008 C Tutorial, All rights are reserved