Wednesday, October 25, 2017

Conditionals



Using conditionals in a script is a way to execute different parts of a code depending of the result of a condition. What is a condition, the operators (comparison and logical)?


Updated for  ECMAScript2015+, it requires at least version ImageJ 1.51r and Java 9. See this post for update [Link].

1. The statement if-else

In programming languages, different blocks of code can be executed depending of a condition. The conditional is defined by the  keyword if [Wikipedia]. Fig.1 presents a sketch how the execution is done.

Fig.1: Sketch presenting a if statement. Depending of the value (True or False) of the condition symbolized as a question mark, the upper or lower blocks of code are respectively executed.

1.1. Block of code

In  JavaScript, a block of code is delimited by curly brackets (open { and closed }). This block contains one or several instructions and are used in many cases like the conditionals and loops.

In this example, a block of code is defined and creates a "protected" environment where a local variable k is defined and is not related to the outside k. That is why the inner k is equal to 5 and the outer k is equal to 7. The same happens in a conditional or in a loop.

let k = 7;
{
  let k = 5;
  IJ.log(`Inner block of code. k = ${k}`);
}
  IJ.log(`Outside of the block of code. k = ${k}`);
1.2. Comparison operators

A condition returns a boolean number: True or False. In JavaScript, the various operators are:
OperatorDescription
==equal to
!=different of
>greater than
<less than
>=greater than or equal to
<=less than or equal to

There are two additional operators...

OperatorDescription
===strictly equal to
!==strictly different of

IJ.log(3 == '3'); // True. No type checking - Dangerous. Do not use it
IJ.log(3 === '3'); // False. Strict equality

1.4. Logical operators
The conditions may be combined together with logical operators: and, or and not.

OperatorDescription
&&AND
||OR
!NOT
let j = 6;
IJ.log(j > 2 && j < 10); // True

1.4. Syntax of the if-else
The syntax is as follows:

<init>
if (<condition>) {
  instruction #1;
  instruction #2;
  ...
  instruction #N;
}
else {
  instruction #A;
  instruction #B;
  ...
  instruction #Z;

}

For example, to test if a number is less or greater than 5, we can write the following lines:

let i = 10; 
if (i  < 5) {
  IJ.log(`${i} is less than 5`};
}
else {
  IJ.log(`${i} is greater or equal to 5`};
}
 

2. The if-else-if-else statement


let i = 10; 
if (i  < 5) {
  IJ.log(`${i} is less than 5`};
}
else if (i === 5{
  IJ.log(`${i} is equal to 5`};
}
else {
  IJ.log(`${i} is greater than 5`};
}
 

3. The ternary operator


There is a more compact syntax of if-then-else statement...

<variable> = <condition> ? <evaluated when true> : <evaluated when false>

This is used when you want to assign a value to a variable depending of a condition.

let k = 7;
let result = ( k < 5) ? `${k} is less than 5` : `${k} is greater than 5`;
IJ.log(result); // 7 is greater than 5


Thank you for reading.

<<  Previous: Strings Next: Loops  >>

5. Other crazybiocomputing posts

Further readings are available in ...
  • Programming in JavaScript Series  [Link]
  • JavaScript/ECMAScript TOC [Link]

No comments:

Post a Comment