PHP Loops

LOOPS IN PHP
Often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several code-lines in a script, we can use loops to perform a task like that.
By using loops statements we can decrease the number of code lines.
In PHP, there are following looping statements:

while - loops through a block of code as long as the specified condition is true

do...while - In this  loop a statement is repeats the loop as long as the specified condition is true

for - loops through a block of code a specified number of times

foreach - loops through a block of code for each element in an array

while Loop- The while loop executes a block of code untill the specified condition is true.
Syntax:
while (condition) {
code ;
}
Example:
$a = 5;
while($x <= 10) {
echo "The number is: $a
";
$a++;
}
?>
OUTPUT:
The number is:5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
do...while Loop-The do...while loop will always execute the block of code  atleast once, and then check the condition, and repeat the loop till the specified condition is true.

Syntax:
do {
code ;
} while (condition);
Example:
$a= 5;
do {
   echo "The number is: $a
";
$a++;
} while ($a <= 5);
?>
OUTPUT:
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9

for Loop: for loop is used when the user knows in advance that how many times the code should be executed.

Syntax:
for (initialization; condition; increment) {
code ;
}
Example:
for ($a= 0; $a <= 5; $a++) {
echo "The number is: $a
";
}
?>
OUTPUT:
0
1
2
3
4
5

foreach Loop: It is used to in arrays and is used to loop through each key/value pair in an array.
Syntax:
foreach ($array as $value) {
code ;
}
In each loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element.

$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value
";
}
?>
OUTPUT:
red
green
blue
yellow
Content writer:
Er. Nikita Mahajan
CSE 6th Sem
TSPC, Burhanpur

Python Programming ↓ 👆
Java Programming ↓ 👆
JAVA EasyExamNotes.com covered following topics in these notes.
JAVA Programs
Principles of Programming Languages ↓ 👆
Principles of Programming Languages
EasyExamNotes.com covered following topics in these notes.

Practicals:
Previous years solved papers:
A list of Video lectures References:
  1. Sebesta,”Concept of programming Language”, Pearson Edu 
  2. Louden, “Programming Languages: Principles & Practices” , Cengage Learning 
  3. Tucker, “Programming Languages: Principles and paradigms “, Tata McGraw –Hill. 
  4. E Horowitz, "Programming Languages", 2nd Edition, Addison Wesley

    Computer Organization and Architecture ↓ 👆

    Computer Organization and Architecture 

    EasyExamNotes.com covered following topics in these notes.

    1. Structure of desktop computers
    2. Logic gates
    3. Register organization
    4. Bus structure
    5. Addressing modes
    6. Register transfer language
    7. Direct mapping numericals
    8. Register in Assembly Language Programming
    9. Arrays in Assembly Language Programming

    References:

    1. William stalling ,“Computer Architecture and Organization” PHI
    2. Morris Mano , “Computer System Organization ”PHI

    Computer Network ↓ 👆
    Computer Network

    EasyExamNotes.com covered following topics in these notes.
    1. Data Link Layer
    2. Framing
    3. Byte count framing method
    4. Flag bytes with byte stuffing framing method
    5. Flag bits with bit stuffing framing method
    6. Physical layer coding violations framing method
    7. Error control in data link layer
    8. Stop and Wait scheme
    9. Sliding Window Protocol
    10. One bit sliding window protocol
    11. A protocol Using Go-Back-N
    12. Selective repeat protocol
    13. Application layer
    References:
    1. Andrew S. Tanenbaum, David J. Wetherall, “Computer Networks” Pearson Education.
    2. Douglas E Comer, “Internetworking with TCP/IP Principles, Protocols, And Architecture",Pearson Education
    3. KavehPahlavan, Prashant Krishnamurthy, “Networking Fundamentals”, Wiley Publication.
    4. Ying-Dar Lin, Ren-Hung Hwang, Fred Baker, “Computer Networks: An Open Source Approach”, McGraw Hill.