0% found this document useful (0 votes)
39 views13 pages

Looping and Branching (Day-3)

The document discusses different types of loops in computer programming including while loops, for loops, and do-while loops. It provides the syntax and examples of using while and for loops. The while loop repeatedly executes a block of code as long as a given condition is true. The for loop allows code to be executed a specified number of times and includes initialization, condition, and increment/decrement sections.
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)
39 views13 pages

Looping and Branching (Day-3)

The document discusses different types of loops in computer programming including while loops, for loops, and do-while loops. It provides the syntax and examples of using while and for loops. The while loop repeatedly executes a block of code as long as a given condition is true. The for loop allows code to be executed a specified number of times and includes initialization, condition, and increment/decrement sections.
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/ 13

Hidaya Institute Of Science &

Technology

Looping and Branching


What Is Loop?
In computer programming, a loop is a sequence of instructions that is
continually repeated until a certain condition is reached.

Typically, a certain process is done, such as getting an item of data and


changing it, and then some condition is checked such as whether a
counter has reached a prescribed number.

Loops execute a block of code a specified number of times, or while a


specified condition is true.

A loop lets you write a very simple statement to produce a significantly


greater result simply by repetition. 
What Is Loop?
In general, statements are executed sequentially: The first statement in a
script is executed first, followed by the second, and so on.

A loop statement allows us to execute a statement or group of statements


multiple times.

Types of loop:

The While Loop 


The For Loop 
The Do...While Loop 
The foreach Loop
The While Loop
The while loop statement repeatedly executes a target statement as long
as a given condition is true.

Syntax:

while(condition)
statement;

Here:
• Statement is a single statement.
• The condition may be any expression, and true is any non-zero value.
• The loop iterates while the condition is true. When the condition becomes
false, program control passes to the line immediately following the loop.
The While Loop
Like with the if statement, you can group multiple statements within the same
while loop by surrounding a group of statements with curly braces.
Syntax:

while(condition) {
statement(s);
}

or by using the alternate syntax:

while(condition) :
statement(s);
endwhile;
The While Loop

Example:

// Local variable declaration:


$a = 10;
// while loop execution
while( $a < 20 ) {
echo “value of a: ”. $a;
echo “<br />”;
$a++;

The 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
The For Loop
The for loop allows a statement(s) to be executed a specified number of times.
Syntax:
 for ( initialization ; condition; increment /decrement) {
statement(s);
}

or by using the alternate syntax:

for ( initialization ; condition; increment /decrement) :


statement(s);
endfor;

The initialization step is executed first, and only once. This step allows you to
declare and initialize any loop control variables. You are not required to put a
statement here, as long as a semicolon appears.
The For Loop
Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is
false, the body of the loop does not execute and flow of control jumps to the next
statement just after the for loop.

After the body of the for loop executes, the flow of control jumps back up to the
increment statement. This statement allows you to update any loop control
variables. This statement can be left blank, as long as a semicolon appears after the
condition.

The condition is now evaluated again. If it is true, the loop executes and the process
repeats itself (body of loop, then increment step, and then again condition). After
the condition becomes false, the for loop terminates.
The For Loop

Example:

// for loop execution


for( $a = 10; $a < 20; $a = $a + 1 ) {
echo “value of a: ”. $a;
echo “<br />”;
}
The For 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
1) GENERATE TABLE
generate any number table in for loop with its start and ending point

2) Find the sum of the first 10 natural numbers


The sum of the first 10 natural numbers should be look like on browser:

1+2+3+4+5+6+7+8+9+10=55

3) Factorial Of A Number
The factorial of a positive integer n is equal to 1*2*3*...n.

4) GENERATE GIVEN SERIES (WHILE/FOR)

1) 1 4 9 16 25

2) 1 3 6 10 15 21

3) 50 40 30 20 10

4) 6 12 20 30 42
Assignments
5) Make even odd series b/w 1 to 100 by using single loop
Output should be like:
1 3 5 7 9 11….99
2 4 6 8 10 12…..100

You might also like