Perl's if statement is similar to C's if statement

Perl's if statement is similar to the C language if statement.

Perl if statement

Perl if.

# Perl if statement
if ($flag) {
  
}
elsif ($flag) {
  
}
else {
  
}

C language if statement

It is a C language if.

// C language if statement
if (flag) {
  
}
else if (flag) {
  
}
else {
  
}

Comparison of Perl if statement and C language if statement

Let's compare the if statement in Perl with the if statement in C language. The usage of "()" and "{}" is the same. The scope is also in the same range. The keywords if and else are the same.

There is one difference. Perl is "els if" and C language is "else if". Please note that this is easy to make a mistake.

The answer to why it's not "else if" is because Perl author Larry Wall decided so.

If you learn a conditional branch in a C language if statement, you can use it in Perl, and you can use its grammar and concept in Perl

With the exception of elsif, Perl and C if statements are exactly the same. This is because Perl was created with reference to the C language grammar.

Engineers who have learned C language in basic information processing tests and development of Unix / Linux environment, and students who have learned C language in software classes in the first year of university can make use of that knowledge as it is. The learning cost of Perl is low. Being able to divert knowledge means that you don't have to remember much.

Other similarities between Perl and C

In this article, I wrote that Perl's if statement is similar to C, but Perl's for statement, while statement, increment / decrement, operator type and priority, scope, and reference concept are also C. Similar to language.

Associated Information