Sunday 9 June 2013

USE OF CONTINUE AND BREAK

continue statement is used to skip the remaining code that follows continue statement, and just continue iterating (means beginning the next new iteration). continue statement is used in loops.
break statement just breaks the execution from the loop and throws the program control out of the loop, or a switch case, whatever is the case. break statement is used in loops, and switch case statements.

Source code illustrating the use of continue statement for printing all the even numbers between 0 and 50.
/********************************************************************************/
<html>
                <head>
                                <title>continue</title>
                </head>
                <body>
                                <?php
                                                for($i=0;$i<=50;$i++)
                                                {
                                                                if($i%2!=0)
                                                                                continue;
                                                                echo $i."<br />";
                                                }
                                ?>
                </body>

</html>
/********************************************************************************/
Output:-
0
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50

Illustration for the break statement:-
/*****************************************************************************/

<html>
                <head>
                                <title>break</title>
                </head>
                <body>
                                <?php
                                                for($i=0;$i<=50;$i++)
                                                {
                                                                if($i==25)
                                                                                break;
                                                                echo $i."<br />";
                                                }
                                ?>
                </body>
</html>
/******************************************************************************/
Output:-
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

No comments:

Post a Comment