Control Structures of Java and PHP Part 5

Control structures of Java and PHP

This is the fifth part of the Introduction to Java and PHP series. Control structures of Java and PHP will be discussed. Whenever possible, code examples will be made in a similar manner for both Java and PHP.

Control structures in Java and PHP are very similar. Logical operators and conditional statements in Java are exactly the same in PHP.

In this tutorial, Java and PHP will be used to create a program consisting of if statements and switch statements. 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 19: Logical Operators

Java PHP
Or
OR // Or, || OR // Or, ||
And
AND // And, && AND // And, &&
Not
NOT // Not, ! NOT // Not, !
Less Than
< // Less than, < < // Less than, <
Greater Than
> // Greater than, > > // Greater than, >
Equals To
== // Equals to, == == // Equals to, ==
Not Equals To
!= // Not equals to, != != // Not equals to, !=
Less Than Or Equals To
<= // Less than or equals to, <= <= // Less than or equals to, <=
Greater Than Or Equals To
>= // Greater than or equals to, >= >= // Greater than or equals to, >=

Java and PHP share the same logical operators and shortcuts. The shortcuts for “and”, “or”, and “not” are the same in Java and PHP.

Lesson 20: If Statements

Java PHP
If statements
if (num1 == 2) { // if statement
System.out.println(“num1 is equal to 2”); // Print valid result
}
if ($num1 == 2) { // if statement
echo “num1 is equal to 2”; // Print valid result
}
If-then-else statements
if (num1 == 2) { // if statement
System.out.println(“num1 is equal to 2”); // Print valid result
}
else { // else statement
System.out.println(“num1 is not equal to 2”); // Print invalid result
}
if ($num1 == 2) { // if statement
echo “num1 is equal to 2”; // Print the variable num1
}
else { //else statement
echo “num1 is not equal to 2”; // Print invalid result
}
Else if statements
if (num1 == 2) { // if statement
System.out.println(“num1 is equal to 2”); // Print valid result
}
else if (num1 > 2) { // else if statement
System.out.println(“num1 is greater than 2”); // Print valid result
}
else { // else statement
System.out.println(“num1 is not equal to 2”); // Print invalid result
}
if ($num1 == 2) { // if statement
echo “num1 is equal to 2”; // Print valid result
}
else if (num1 > 2) { // else if statement
echo “num1 is greater than 2”; // Print valid result
}
else { // else statement
echo “num1 is not equal to 2”; // Print invalid result
}

For both Java an PHP, the else statement is optional but is mandatory if using and else if statement. If statements can be compiled without blocks when the required code is immediately following. If statements require opening and closing parenthesis “( )” for the conditions.

Lesson 21: Switch statements

Java PHP
switch (num1) { // switch statement
case ‘2’ :
System.out.println(“num1 is equal to 2”); // Print valid result
break;
case ‘3’ :
System.out.println(“num1 is greater than 2”); // Print valid result
break;
default :
System.out.println(“num1 is not equal to 2”); // Print invalid result
break;
}
switch ($num1) { // switch statement
case ‘2’ :
echo “num1 is equal to 2”; // Print valid result
break;
case ‘3’ :
echo “num1 is greater than 2”; // Print valid result
break;
default :
echo “num1 is not equal to 2”; // Print invalid result
break;
}

Java and PHP switch statements are the same. Switch statements are based on condition being equal to.

ControlStructures.java file

/**
 * Ojambo.com Control Structures Java Tutorial
 * ControlStructures.java Copyright 2011 Edward
 * http://www.ojambo.com
 * 
 */
 
// This class displays Control Structures
public class ControlStructures { //This class is called ControlStructures
    public static void main(String[] args) { // Main Method
        // Declare variables and assign values immediately 
	    int num1 = 2; // Integer
            
        // If statement 
        if (num1 == 2) { // if statement 
			System.out.println("num1 is equal to 2"); // Print valid result 
		} 
		else if (num1 > 2) { // else if statement 
			System.out.println("num1 is greater than 2"); // Print valid result 
		} 
		else { // else statement 
			System.out.println("num1 is not equal to 2"); // Print invalid result 
		}
        
        // Switch statement
		switch (num1) { // switch statement 
			case '2' : 
				System.out.println("num1 is equal to 2"); // Print valid result 
				break; 
			case '3' : 
				System.out.println("num1 is greater than 2"); // Print valid result 
				break; 
			default : 
				System.out.println("num1 is not equal to 2"); // Print invalid result 
				break; 
		}

    }
}

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

ControlStructures.php file

<?php
/*
 * Ojambo.com Control Structures PHP Tutorial
 * ControlStructures.php Copyright 2011 Edward
 * http://www.ojambo.com
 * 
 */

	// Declare variables and assign values immediately 
	$num1 = 2; // Integer
		
	// If statement 
	if ($num1 == 2) { // if statement 
		echo "num1 is equal to 2"; // Print valid result 
	} 
	else if ($num1 > 2) { // else if statement 
		echo "num1 is greater than 2"; // Print valid result 
	} 
	else { // else statement 
		echo "num1 is not equal to 2"; // Print invalid result 
	}
	
	// Switch statement
	switch ($num1) { // switch statement 
		case '2' : 
			echo "num1 is equal to 2"; // Print valid result 
			break; 
		case '3' : 
			echo "num1 is greater than 2"; // Print valid result 
			break; 
		default : 
			echo "num1 is not equal to 2"; // Print invalid result 
			break; 
	}

?>

The ControlStructures.php can be compiled as php -l ControlStructures.php and compiled with php ControlStructures.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 Control Structures such as the if statements and switch statements.

Java and PHP have the same logical operators. For example, equals to is two equals signs in Java and not is expressed as the exclamation mark. Two ampersand signs express “and”. The else statement is optional but is required if else if statements are used.

    Recommendations:

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