#!/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";
}
1a) ($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?
2a) ($a and $b and $c)
b) ($a and $b or $c)
c) ($a and ($b or $c))
d) (($a and $b) or $c)
3a) 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".)
3b) 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.
3c) Change the script so that only first name and last name are required.
The phone number is optional.
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?
Exercises:
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.)
Write a program that asks a user to input a number. If the number equals "5", output "My lucky number". If the number is larger than 10, output "What a large number!". In all other cases, output "That's not my lucky number."
Optional Material:
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 conlusion is that programming languages can
behave somewhat differently than plain logic would suggest.