0% found this document useful (0 votes)
17 views11 pages

Looping and Branching

Uploaded by

dallawais543
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views11 pages

Looping and Branching

Uploaded by

dallawais543
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Hidaya Institute Of

Science & Technology

Looping and Branching


The goto Statement
The goto operator can be used to jump to another section in the program.

 The target point is specified by a label followed by a colon, and the


instruction is given as goto followed by the desired target label.

The target label must be within the same file and context.

 Meaning that you cannot jump out of a function or method, nor can you
jump into one.

You also cannot jump into any sort of loop or switch structure. You may
jump out of these.

 a common use is to use a goto in place of a multi-level break.


The goto Statement

Example:

<?php
goto a;
echo ‘hello';

a:
echo ‘hi';
?>

The above example will output:


hi
The goto Statement

Example:

<?php
for($i=0,$j=50; $i<100; $i++) {
while($j--) {
if($j==17) goto end;
}
}
echo "i = $i";
end:
echo 'j hit 17';
?>

The above example will output:


j hit 17
The goto Statement

Example: This will not work

<?php
goto loop;
for($i=0,$j=50; $i<100; $i++) {
while($j--) {
loop:
}
}
echo "$i = $i";
?>
The above example will output:
Fatal error: 'goto' into loop or switch statement is disallowed in script on line 2
The DO-WHILE LOOP
Unlike for and while loops, which test the loop condition at the top of the
loop, the do...while loop checks its condition at the bottom of the loop.

A do...while loop is similar to a while loop, except the truth expression is


checked at the end of each iteration instead of in the beginning.

Syntax: There is just one syntax for do-while loops:


do {
statement(s);
} while( condition );

Notice that the conditional expression appears at the end of the loop, so
the statement(s) in the loop execute once before the condition is tested.
The DO-WHILE LOOP

Example:

<?php
$i = 0;
do {
echo $i;
} while ($i > 0);
?>

The above loop would run one time exactly, since after the first iteration,
when truth expression is checked, it evaluates to FALSE ($i is not bigger than
0) and the loop execution ends.
So output will be just: 0
The DO-WHILE LOOP

Example:

// Local variable declaration:


$a = 10;
// do loop execution
do {
echo "value of a: " . $a;
$a = $a + 1; // updation
} while( $a < 20 );
The DO-WHILE LOOP
Output: The above code will produce the following result:

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Assignments
Generate tables up to a given number
eg. Declare a variable $limit and give it a number (value can be
change at any time)
if we give a value: $limit = 3; then output should be like

2*1=2
2*2=4
.
.
2 * 10 = 20

3*1=3
3*2=6
.
.
3 * 10 = 30
Assignments
Generate Fibonacci Series

Fibonacci series is the one in which you will get your next term by adding previous two
numbers.

For example,
0 1 1 2 3 5 8 13 21 34

Here, 0 + 1 = 1
1+1=2
3+2=5
and so on.

You might also like