Differences between version 2 and previous revision of TernaryOperator.
Other diffs: Previous Major Revision, Previous Author, or view the Annotated Edit History
Newer page: | version 2 | Last edited on Saturday, October 26, 2002 1:49:43 pm | by SamCannell | Revert |
Older page: | version 1 | Last edited on Friday, October 25, 2002 2:26:42 pm | by SamCannell | Revert |
@@ -1,5 +1,5 @@
-The "?:", or Ternary operator in PHP, Java, C and other languages is essentially shorthand for a block of code like:
+The "?:", or Ternary operator in PHP, Java
, Java, C and possibly
other languages is essentially shorthand for a block of code like:
if ($a==1) {
print "A is 1!";
} else {
@@ -7,9 +7,9 @@
}
Using the ternary operator, it becomes:
- print ($a==1)?"A is 1!":"A is not 1!";
+ print ($a==1) ? "A is 1!" : "A is not 1!";
Cool, huh?
Or, in java:
@@ -35,9 +35,17 @@
new !CondOpTest(new Integer(args[[0]));
}
public !CondOpTest(Integer a) {
- System.out.println((a.intValue()==1)?"A is 1!":"A is not 1!");
+ System.out.println((a.intValue()==1 ) ? "A is 1!" : "A is not 1!");
}
}
-Note
: It's great for making spaghetti
code!
+In Perl, however, things are slightly different.
+
+This piece of code is valid, and is similar to the examples above
:
+
+ print ( ($a==1) ? "A is 1!" : "A is not 1!" );
+
+However the following
code is also valid:
+
+ ($a==1) ? print "A is 1!" : print "A is not 1
!";