1 Logical expressions

#!/usr/local/bin/perl -w
# if statement
print "Input first value:";
$a = <STDIN> ;
chomp $a;
print "Input second value:";
$b = <STDIN> ;
chomp $b;
if ($a and $b){
      print "True\n";
} else {
      print "False\n";

1.2 Exercises

1) In the following expressions, input '1' (for 'true') and '0' (for 'false') as values for $a and $b. Which expressions are true? Which expressions are logically equivalent? (If you are very familiar with Boolean logic, you can skip this exercise. Otherwise, you can use either truth tables and/or the program from above.)

a) ($a and $b)
b) (not $a and $b)
c) (not ($a and $b))
d) ($a or $b)
e) ($a or not $b)
f) (not ($a or $b))
g) (not (not $a or not $b))
h) ($a and ($a or $b))   Does this really depend on $b?
i) ($a and $b and $c)
j) ($a and $b or $c)
k) ($a and ($b or $c))
l) (($a and $b) or $c)

2a) Write a script that asks someone to input their first name, last name and phone number. If the user does not type at least some characters for each of these, print "Do not leave any fields empty" otherwise print "Thank you". (Hint: if a variable is empty, its value will be "false".)
2b) Change the script so that the script prints "Thank you" if either the first name or the last name or the phone number is supplied. Print "Do not leave all fields empty" otherwise.
2c) Change the script so that only first name and last name are required. The phone number is optional.

1.3 Other logical expressions for if statements

$a eq $b # Is $a equal to $b?
$a ne $b # Is $a not equal to $b?
$a == $b # Is $a numerically equal to $b?
$a != $b # Is $a numerically unequal to $b?
$a <= $b # Is $a smaller or equal to $b?
$a >= $b # Is $a larger or equal to $b?
$a < $b # Is $a smaller than $b?
$a > $b # Is $a larger than $b?
$a le $b # Does $a come before $b in the alphabet or is equal to $b?
$a ge $b # Does $a come after $b in the alphabet or is equal to $b?
$a lt $b # Does $a come before $b in the alphabet?
$a gt $b # Does $a come after $b in the alphabet?

Note: usually "eq" and "ne" work even for numbers. So, you can use them instead of "==" and "!=".

1.4 Exercises

3) Write a program that asks a user to input a color. If the color is black or white, output "The color was black or white". If it starts with a letter that comes after "k" in the alphabet, output "The color starts with a letter that comes after "k" in the alphabet". (Optional: consider both capitalized and non-capitalized words. Note: the order of the alphabet in Unix and Perl is: symbols, numbers, upper case letters, lower case letters.)

4) (Optional Exercise) Perl evaluates expressions from left to right. Consider the following expressions:

a) if ($a++ and $b++) {}
b) if ($a++ or $b++) {}

If you print the values of $a and $b after the if statement is finished then in some cases only the value of $a is increased, in some cases both are. Try it! What is the underlying rule? The conclusion is that programming languages can behave somewhat differently than plain logic would suggest.

2 Control structures: if

#!/usr/local/bin/perl -w
# if statement
print "Do you like Perl? ";
$answer = <STDIN>;
chomp $answer;
if ($answer eq "yes"){
      print "That is great!\n";
} else {
      print "That is disappointing!\n";

2.1 Exercises

5) Modify the program so that it answers "That is great!" if the answer was "yes", "That is disappointing" if the answer was "no" and "That is not an answer to my question." otherwise. Use "if ... elsif ... else ...". Note that "if" and "elsif" are followed by an expression in parenthesis () and then a statement in curly brackets {}, "else" is followed only by a statement in curly brackets.

2.2 While

#!/usr/local/bin/perl -w
# while statement
$answer = "no";
while ($answer ne "yes"){
   print "Do you like Perl? ";
   $answer = <STDIN>;
   chomp $answer;
   if ($answer eq "yes") {
      print "That is great!\n";
   } else {
     print "That is not the right answer! Try again.\n";

2.3 Exercises

6) Modify the program from above so that it asks users to "guess the lucky number". If the correct number is guessed the program stops, otherwise it continues forever.

7) Modify the program so that it asks users whether they want to guess again each time. Use two variables, $number for the number and $answer for the answer to the question whether they want to continue guessing. The program stops if the user guesses the correct number or answers "no". (In other words, the program continues as long as a user has not answered "no" and has not guessed the correct number.)

2.4 Using a counter

8) Write a program that asks five times to guess the lucky number. Use a while loop and a counter, such as

$counter = 1;
while ($counter <= 5) {
   print "Type in the $counter number\n";

The program asks for five guesses (no matter whether the correct number was guessed or not). If the correct number is guessed, the program outputs "Good guess!\n", otherwise it outputs "Try again!\n". After the fifth guess it stops and prints "Game over.\n".

2.5 Last

9) In the previous example, insert "last;" after the "Good guess!\n" print statement. "last" will terminate the while loop so that users do not have to continue guessing after they found the number.

2.6 Setting a flag

10) Let users again guess five numbers but this time do not tell them immediately whether they were successful or not. After the while loop is finished print "The correct number was guessed\n" if the number was among the five guesses and "The correct number was not among them\n" otherwise. To implement this use a variable called $flag that is set to true (or 1) as soon as the number is guessed. After the while loop is finished, the truthvalue of $flag is evaluated in an if statement.

2.7 For

#!/usr/local/bin/perl -w
# for statement
for ($i =1; $i <= 10; $i++){
      print "$i ";

2.8 Exercise

11) Modify the counter program from above using a for loop so that it asks the user for five guesses and then stops. Use "last" to terminate the for loop as soon as the correct number is guessed.