else if

Scala: How to add ‘if’ expressions (guards) to match/case expressions

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.13, “How to add 'if' expressions (guards) to case statements.”

Problem

You want to add qualifying logic to a case statement in a match expression, such as allowing a range of numbers, or matching a pattern, but only if that pattern matches some additional criteria.

Solution

Add an if guard to your case statement. Use it to match a range of numbers:

Scala if then else syntax (and returning a value from an if statement)

Scala FAQ: Can you share some examples of the Scala if/then/else syntax? Also, can you show a function that returns a value from an if/then/else statement?

In its most basic use, the Scala if/then/else syntax is very similar to Java:

Perl if, else, elsif ("else if") syntax

Summary: This tutorial shows a collection of Perl if, else, and else if examples.

Here are some examples of the Perl if/else syntax, including the “else if” syntax, which is really elsif. (I wrote this because after working with many different languages I can never remember the “else if” syntax for most languages, and elsif is pretty rare.)

The Perl if/else syntax

The Perl if/else syntax is standard, I don’t have any problems here:

Unix/Linux shell script reference page

Back to top

Linux shell script test syntax

All of the shell script tests that follow should be performed between the bracket characters [ and ], like this:

if [ true ]
then
  # do something here
fi

Very important: Make sure you leave spaces around the bracket characters.

I'll show more detailed tests as we go along.

Back to top

To perform tests on files use the following comparison operators:

Back to top