Iteration Loops of Java and PHP Part 6

Iteration statements of Java and PHP

This is the sixth part of the Introduction to Java and PHP series. Statements that are repeatedly executed in Java and PHP will be discussed. Whenever possible, code examples will be made in a similar manner for both Java and PHP.

Loops in Java and PHP are very similar. While loops and for loops in Java are exactly the same in PHP.

In this tutorial, Java and PHP will be used to create a program consisting of loops. The program will be compiled and executed. In order to make the work-flow faster, please use a programming editor.

    Tools are required:

  • Text editor for creating and modifying the Java and PHP files.
  • Java.
  • PHP.

Optional Download and install suitable text editor

Any text editor will work. A bare minimum should be syntax-highlighting for Java and PHP. To select a reviewed lightweight programming editor read the Ojambo.com Lightweight Programming Editors.

Lesson 22: While Loop

Java PHP
while (counter < 10) { // While Loop
System.out.println(“counter is” + counter); // Print counter variable
counter++; // Increment the counter by one
}
while ($counter < 10) { // While loop
echo “counter is” . $counter; // Print counter variable
$counter++; // Increment the counter by one
}

While loops for Java and PHP are the same. While loop conditions are tested before each iteration. In order to avoid an infinite loop, an incrementing counter is used.

Lesson 23: Do While Loop

Java PHP
do { // Do while Loop
System.out.println(“counter is” + counter); // Print counter variable
counter++; // Increment the counter by one
}
while (counter < 10);
do { // Do while Loop
echo “counter is” . $counter; // Print counter variable
$counter++; // Increment the counter by one
}
while ($counter < 10);

While loops for Java and PHP are the same. Do while loop conditions are tested after each iteration. In order to avoid an infinite loop, an incrementing counter is used.

Lesson 24: For Loop

Java PHP
for (int counter = 0; counter < 10; counter++) { // For Loop
System.out.println(“counter is” + counter); // Print counter variable
}
for ($counter = 0; $counter < 10; $counter++) { // For Loop
echo “counter is” . $counter; // Print counter variable
}

For loops for Java and PHP are the same. For loop expressions are separated by a semi-colon “;”. Counter initialization, conditional statement, and counter increment are all entered as the loop statement.

Lesson 25: For Each Loop

Java PHP
for (int value : numArray) { // Foreach Loop
System.out.println(“Number is” + value); // Print Array value
}
foreach ($numArray as $value) { // Foreach Loop
echo “Number is” . $value; // Print Array value
}

Arrays are the discussed in the next part of the Introduction to Java and PHP series. Arrays are a perfect example of using foreach loops. Java and PHP for each statements work the same way but have different syntax. Java uses the for loop and colon while PHP uses the foreach loop.

IterationLoops.java file

/**
 * Ojambo.com Iteration Loops Java Tutorial
 * IterationLoops.java Copyright 2011 Edward
 * http://www.ojambo.com
 * 
 */
 
// This class displays Iteration Loops
public class IterationLoops { //This class is called IterationLoops
    public static void main(String[] args) { // Main Method
        // Declare variables and assign values immediately 
	    int counter = 0; // Integer
            
        // While Loop
        while (counter < 10) { // While Loop 
			System.out.println("counter is" + counter); // Print counter variable 
			counter++; // Increment the counter by one 
		}
        
        // Do While Loop
		do { // Do while Loop 
			System.out.println("counter is" + counter); // Print counter variable 
			counter++; // Increment the counter by one 
		} 
		while (counter < 10);  

		// For Loop
		for (int counter = 0; counter < 10; counter++) { // For Loop 
			System.out.println("counter is" + counter); // Print counter variable 
		} 
		
		// Array
		int[] numArray = {1,2,3,4}; // Integer Array
		
		// Foreach Loop
		for (int value : numArray) { // Foreach Loop 
			System.out.println("Number is" + value); // Print Array value 
		}
		
    }
}

The IterationLoops.java file can be compiled as javac IterationLoops.java and executed with java IterationLoops.

IterationLoops.php file

<?php
/*
 * Ojambo.com Iteration Loops PHP Tutorial
 * IterationLoops.php Copyright 2011 Edward
 * http://www.ojambo.com
 * 
 */

	// Declare variables and assign values immediately 
	$counter = 0; // Integer
		
	// While Loop
	while ($counter < 10) { // While Loop 
		echo "counter is" . $counter; // Print $counter variable 
		$counter++; // Increment the $counter by one 
	}
	
	// Do While Loop
	do { // Do while Loop 
		echo "counter is" . $counter; // Print $counter variable 
		$counter++; // Increment the $counter by one 
	} 
	while ($counter < 10);  

	// For Loop
	for ($counter = 0; $counter < 10; $counter++) { // For Loop 
		echo "counter is" + $counter; // Print $counter variable 
	} 
	
	// Array
	$numArray = array(1,2,3,4); // Integer Array
	
	// Foreach Loop
	for ($numArray as $value) { // Foreach Loop 
		echo "Number is" + value); // Print Array value 
		}

?>

The IterationLoops.php can be compiled as php -l IterationLoops.php and compiled with php IterationLoops.php

Conclusion:

Java variables must begin with a lowercase letter. In Java, every variable must be specifically declared while PHP only requires a dollar sign. Sometimes Java and PHP share the same Iteration Loops such as the while loops and for loops.

Java and PHP have the same foreach loops with different syntax. For example, java uses the colon symbol “:” while PHP uses “as”. Loops are used when content needs to be repeated or the code is the same with only minor changes.

    Recommendations:

  1. This tutorial uses an optional lightweight programming editor with syntax-highlighting features.
  2. The concepts taught in the first twenty-five lessons can be applied to other programming languages.
  3. When selecting a programming language to master, pay close attention to license restrictions.