Jonathan's Corner
(Search & Sitemap)
> Writing >
Miscellaneous Nonfiction >
Tinkering with Perl
Skip Back
Previous
32
33
34
35
36
37
38
39
40
41
Next
Skip Forward
Printer-Friendly Version
A conditional clause is something that is either true or false. The computer needs to be able to decide if something is true or false.
There are a number of different types of conditional clauses. The one which I will cover here, to get started with, is equals. An expression like:
($UserResponse == "y")
is an example of a conditional clause.
You can build up conditional clauses by using and, or, and not, as well as parentheses. Here is an example of a more complicated clause:
(($MyPet == "dog") || ($MyPet == "cat"))
You should always use lots of parentheses with conditionals, just like you should use parentheses in arithmetic. Furthermore, the parentheses have more or less the same meaning.
Now, I can see a question. Why did I use two equals signs instead of one? The answer is that Perl uses one equals sign for assignment, and two equals signs for conditionals. If you use one equals sign, Perl will think you are doing an assignment. For example, if you type:
($UserResponse = "y")
what that will do is assign $UserResponse the value "y", instead of checking to see if $UserResponse is already "y". This is a very easy mistake to make; check for this when your program seems not to work.
If-then clauses, and loops, do different things depending on whether something is true. A conditional clause is something that can be true or false, which Perl can use to decide if something is true, and therefore run if-thens and loops.
Scalars can also serve as conditional clauses. A scalar that has a value of 0, or that is an empty string (i.e. a string that doesn't contain any characters, not even spaces -- it would be represented as ""), is considered false. Any other scalar is considered true. In general, we use a 1 to represent true, and a 0 to represent false.
Note: Conditional clauses don't have semicolons after them.
Statements - Arithmetic - Flow control - Blocks - If-then - If-then-else - If-then-else chains
Tinkering with Perl is a free book that provides an introduction to programming in Perl, as well as a basic reference for things like foreach in Perl, if-then, and if-then-else, in addition to providing a glossary where you can find definitions for concatenate and other terms.
Tinkering with Perl may be one of the most popular offerings on this site, but it's not the only attraction. You can read a tongue-in-cheek Game Review: Meatspace, read an even more offbeat customer service survey (whether or not you actually fill it out), and spend a few minutes wishing your boss would read, The Administrator Who Cried, "Important!" (Not to mention that there are other things you can read here besides tech stuff, from Janra Ball: The Headache to The Spectacles.)
Jonathan's Corner
(Search & Sitemap)
> Writing >
Miscellaneous Nonfiction >
Tinkering with Perl
Skip Back
Previous
32
33
34
35
36
37
38
39
40
41
Next
Skip Forward
Printer-Friendly Version