Perl: Conditional Stetments (if, elsif, and else) - by Eun Bae Kim (07/26/2018)
 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
  
use strict;

# Creating an array and adding elements to the array
my @aStudent = ("EBKim", "HCNoh", "IHYoo", "GDJin");


# Conditional Statement 1
print "--------------------------------------------\n";
if ($aStudent[3] eq "IHYoo") {          # String comparison: eq or ne
	print "Index 3 ... IHYoo"."\t"."TRUE\n";
}


# Conditional Statement 2
print "--------------------------------------------\n";
if ($aStudent[3] eq "IHYoo") {
	print "Index 3 ... IHYoo"."\t"."TRUE\n";
} else {
	print "Index 3 ... IHYoo"."\t"."FALSE\n";
}


# Conditional Statement 3
print "--------------------------------------------\n";
if ($aStudent[3] eq "IHYoo") {
	print "Index 3 ... IHYoo"."\t"."TRUE\n";
} elsif ($aStudent[3] eq "GDJin") {
	print "Index 3 ... IHYoo"."\t"."FALSE\n";
	print "Index 3 ... GDJin"."\t"."TRUE\n";
} else {
	print "Index 3 ... ?????"."\t"."Who is Index 3?\n";
}





my $iCnt1 = 0;
my $iCnt2 = 1;

# Conditional Statement 4
print "--------------------------------------------\n";
if ($iCnt1 == 0 && $iCnt2 == 1) {     # Comparison of Numerical Values: == or !=
	print "iCnt1 = 0 and iCnt2 = 1\n";
} elsif ($iCnt1 == 0 || $iCnt2 == 2) {
	print "iCnt1 = 0 or iCnt2 = 2\n";
}

# Combination of conditions: and(&&), or(||)
if ($iCnt1 == 0 || $iCnt2 == 2) {
	print "iCnt1 = 0 or iCnt2 = 2\n";
}

# Conditional Statement 5
print "--------------------------------------------\n";
unless ($iCnt1 == 0) {     # unless = not if
	print "iCnt is not 0\n";
}
unless ($iCnt1 == 1) {     # unless = not if
	print "iCnt is not 1\n";
}






# For more information, refer to the following link
# https://www.tutorialspoint.com/perl