Looping and Branching (Day-4)

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 15

Hidaya Institute Of Science &

Technology

Looping and Branching


The Break Statement
Sometimes a situation arises where we want to exit from a loop
immediately without waiting to get back to the conditional statement.

The keyword break ends execution of the current for, foreach, while, do
while or switch structure.

When the keyword break executed inside a loop the control automatically
passes to the first statement outside the loop.

Syntax:
break;
The Break Statement

Example:

for( $a=10; $a < 20; $a++ ) {


echo “value of a: ”. $a;
echo “<br>”;
if($a == 15) {
break;
}

The Break Statement
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
The Continue Statement
Sometimes a situation arises where we want to take the control to the
beginning of the loop (for example for, while, do while etc.)

skipping the rest statements inside the loop which have not yet been
executed.

The keyword continue allow us to do this. When the keyword continue


executed inside a loop the control automatically passes to the beginning
of loop. Continue is usually associated with the if inside the loop.

Syntax:
continue;
The Continue Statement

Example:

for($a = 10; $a < 20; $a++ ) {


if($a == 15) {
continue;
}
echo “value of a: ”. $a;
echo “<br>”;

The Continue Statement
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: 16
value of a: 17
value of a: 18
value of a: 19
Nested Loop
A loop can be nested inside of another loop.

Syntax:

for ( initialization; condition; increment ) {

for ( initialization; condition; increment ) {


statement(s);
}

statement(s);
// you can put more statements.
}
Nested Loop

Example:

$a=1;
while($a<=3) {
for($b=1;$b<=3;$b++) {
echo “a = $a , b = $b”;
echo “<br>”;
}
echo “<br>”;
$a++;
}
Nested Loop
Output: The above code will produce the following result:
a=1,b=1
a=1,b=2
a=1,b=3

a=2,b=1
a=2,b=2
a=2,b=3

a=3,b=1
a=3,b=2
a=3,b=3
Levels in break and continue
We can define the levels for break and continue within the nested loop

break; break the inner most loop (exit from the loop)

break 2; break the 2 nesting level loops (exit from the 2 nested
loops)

continue; force loop for next iteration from where it is used


without executing rest of loop code

continue 2; force loop for next 2 iteration from where it is used


without executing rest of loop code
Nested Loop

Example:

<?php
for($i = 0; $i < 10; ++$i) {
for($j = 0; $j < 10; ++$j) {
break 2;
}
echo $i;
}
echo ”i value is: ”.$i.” and j value is: ”.$j;
Output:
i value is: 0 and j value is: 0
Assignments
1) GENERATE ALL TRIANGULAR SHAPES

A) STAR TRIANGULAR SHAPE

*
**
***
****
*****
B) ALPHABETICAL TRIANGULAR SHAPE
A
AB
ABC
ABCD
ABCDE

C) NUMERIC TRIANGULAR SHAPE


1
12
123
1234
12345
Assignments
D) NUMERIC TRIANGULAR SHAPE AND ITS MULTIPLICATION
1 =1
12 =2
123 = 6
1234 = 24
12345 =120

2) GENERATE STAR PYRAMID

*
***
*****
*******
*********
***********

3) Prime Number Series. Numbers that are self divisible are Prime Numbers.
 2 3 5 7 11 13
Assignments
4) Print a triangular multiplication table for 0 through 5.

0
0  1
0  2  4
0  3  6  9
0  4  8  12  16
0  5  10  15  20  25

5) GENERATE STAR DIAMOND SHAPE

*
***
*****
***
*

You might also like