logo

Programming Languages - if / else

Depending on the language, if may be a statement or an expression, check out Statement vs Expression

if / else

C++ / Java

if (a < 0) {
    // ...
} else if (a == 0) {
    // ...
} else {
    // ...
}

Go

() is optional, {} is required.

if a < 0 {
	// ...
} else {
  // ...
}

Variables declared by the statement are only in scope until the end of the if. This is a common pattern to deal with errors, err is limited to the if statement:

if err := foo(); err != nil {
  // ...
}

Rust

if a < 0 {
    // ...
} else if a == 0 {
    // ...
} else {
    // ...
}

Python

if a < 0:
  # ...
elif a == 0:
  # ...
else:
  # ...

Bash

if [ ... ]
then
  ...
elif [ ... ]
then
  ...
else
  ...
fi

Examples

Test if file exists

if [ -e "/path" ]
then
  echo "path exists."
fi

As One-liner

$ if [ 1 -eq 1 ]; then echo "yes"; fi
yes

Ternary

Rust

let result = if var > 0 { true } else { false };

Python

>>> "a" if 1 + 1 == 2 else "b"
'a'

JavaScript

result = condition ? 'a' : 'b';

Appendix: Bash Conditions

condition True if ...
[ -a FILE ] if FILE exists.
[ -b FILE ] if FILE exists and is a block-special file.
[ -c FILE ] if FILE exists and is a character-special file.
[ -d FILE ] if FILE exists and is a directory.
[ -e FILE ] if FILE exists.
[ -f FILE ] if FILE exists and is a regular file.
[ -g FILE ] if FILE exists and its SGID bit is set.
[ -h FILE ] if FILE exists and is a symbolic link.
[ -k FILE ] if FILE exists and its sticky bit is set.
[ -p FILE ] if FILE exists and is a named pipe (FIFO).
[ -r FILE ] if FILE exists and is readable.
[ -s FILE ] if FILE exists and has a size greater than zero.
[ -t FD ] if file descriptor FD is open and refers to a terminal.
[ -u FILE ] if FILE exists and its SUID (set user ID) bit is set.
[ -w FILE ] if FILE exists and is writable.
[ -x FILE ] if FILE exists and is executable.
[ -O FILE ] if FILE exists and is owned by the effective user ID.
[ -G FILE ] if FILE exists and is owned by the effective group ID.
[ -L FILE ] if FILE exists and is a symbolic link.
[ -N FILE ] if FILE exists and has been modified since it was last read.
[ -S FILE ] if FILE exists and is a socket.
[ FILE1 -nt FILE2 ] if FILE1 is newer than FILE2, or if FILE1 exists and FILE2 does not.
[ FILE1 -ot FILE2 ] if FILE1 is older than FILE2, or is FILE2 exists and FILE1 does not.
[ FILE1 -ef FILE2 ] if FILE1 and FILE2 refer to the same device and inode numbers.
[ -o OPTIONNAME ] if shell option "OPTIONNAME" is enabled.
[ -z STRING ] if the length of "STRING" is zero.
[ -n STRING ] or [ STRING ] if the length of "STRING" is non-zero.
[ STRING1 == STRING2 ] if the strings are equal.
[ STRING1 != STRING2 ] if the strings are not equal.
[ STRING1 < STRING2 ] if "STRING1" sorts before "STRING2" in the current locale.
[ STRING1 > STRING2 ] if "STRING1" sorts after "STRING2" in the current locale.

[ ARG1 OP ARG2 ]: "OP" is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if "ARG1" is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to "ARG2", respectively. "ARG1" and "ARG2" are integers.

Combining expressions

Operation Effect
[ ! EXPR ] True if EXPR is false.
[ ( EXPR ) ] Returns the value of EXPR. This may be used to override the normal precedence of operators.
[ EXPR1 -a EXPR2 ] True if both EXPR1 and EXPR2 are true.
[ EXPR1 -o EXPR2 ] True if either EXPR1 or EXPR2 is true.

e.g.

if $SPARK_HOME is not set

if [ -z "${SPARK_HOME}" ]; then
  export SPARK_HOME="$(cd "`dirname "$0"`"/..; pwd)"
fi