Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2024.0.0 - English


if

keyword for conditional execution

Syntax

if expr1 then
    statements
elseif expri then
    statements
    ....
else
    statements
end

Arguments

expr1, expri

logical expressions

statements

blocks of valid statements.

Description

The if statement evaluates a logical expression and executes a group of statements when the expression is true.

The expri are expressions with numeric or boolean values. If expri is a non-scalar value then the condition is true only if all entries of this value are true or different from zero.

The optional elseif and else provide for the execution of alternate groups of statements. An end keyword, which matches the if, terminates the last group of statements. The line structure of if is not significant, the only constraint is that each then keyword must be on the same line as its corresponding if or elseif keyword.

The keyword then can be replaced by a carriage return or a comma.

According to the Code Conventions for the Scilab Programming Language it is recommended:

  • Start each statement on a new line.

  • Write no more than one simple statement per line.

  • Break compound statements over multiple lines.

For example, use:

if rand(1,1) > 0.5 then
    disp("True");
else
    disp("False");
end

rather than

if rand(1,1) > 0.5 then disp("True"); else disp("False"); end

Examples

i=2
for j = 1:3,
  if i == j then
    a(i,j) = 2;
  elseif abs(i-j) == 1 then
    a(i,j) = -1;
  else a(i,j) = 0;
  end,
end

See also

  • then — keyword in control flows 'if' and 'select'
  • else — keyword in if-then-else and select-case-then-else
  • elseif — keyword in if-then-else
  • end — end keyword
  • while — Opens a block of instructions iterated on a heading condition
  • select — select keyword
  • boolean — Scilab Objects, boolean variables and operators & | ~
  • comparison — comparison, relational operators
  • |, || — Binary OR between integers. Logical OR over/between booleans and numbers
  • &, && — Binary AND between integers. Logical AND over/between booleans and numbers

History

VersionDescription
6.0.0
  • if is now protected: Assignments like if=1 are no longer possible.
  • The number of characters used to define the body of any conditional instruction (if, while, for, or select/case) is no more limited to 16k.
Report an issue
<< halt Control flow pause >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Tue Oct 24 14:30:01 CEST 2023