logo for matrixlab-examples.com
leftimage for matrixlab-examples.com


The  IF statement in Scilab 

This is what the online help says about this statement... The IF statement in Scilab evaluates a logical expression and executes a group of statements when the expression is true. This is the calling sequence

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


The “expr_i” are expressions with numeric or boolean values. 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 given above 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.

That’s the oficial version of the explanation. Let’s see the unofficial version and some explained examples...

The IF statement in Matlab  

 
The IF statement lets you evaluate a condition and works together with the THEN clause to take a course of action based on the evaluation. In their simplest form, IF and THEN form a single statement. The syntax of a single-line conditional statement using if and then is as follows:  

if condition then statement, end

The condition portion of the statement is one of the conditional expressions you have already learned in other pages in this site. The statement portion is another Scilab statement that is executed only if condition is true. If condition is false, Scilab ignores statement and moves on to the next line in your program. Here's an example: 
 

if a > 1000 then disp(‘The number is too big!’), end


If the condition a > 1000 is false, Scilab ignores the rest of the line and executes the next statement in your program. 

The statement following THEN can be any Scilab statement. But remember that Scilab executes the statement following THEN only if the condition following IF is true. 
 

Even if the condition in an IF statement is false, Scilab executes the rest of the program as it normally would. Only the statement that follows the THEN portion of an IF statement is ignored if the condition isn’t true.

 

Using More Than One Condition  

You can specify multiple conditions in an IF statement by using the logical boolean variables and operators & (and), | (or) and ~ (not). You use logical operators in conditional expressions much as you use math operators in numeric expressions.   

Boolean Operators in Matlab

The & (and) logical operator  

The & operator lets you specify multiple conditions that must be true before an action can be taken. Here's the syntax line for an IF statement that uses the & operator: 
 

if condition1 & condition2 then statement, end

Both condition1 and condition2 must be true before Scilab can execute statement. Here's an example: 

if num1 > 10 & num2 < 20 then disp('Correct!'), end 

Note that the IF statement contains two conditions and because they're connected by the logical operator &, both num1 > 10 and num2 < 20 must be true before Scilab can execute the DISP statement following THEN. 
 

These three lines of code 

num1 = 15;
num2 = 17;
if (num1 > 10) & (num2 < 20) then disp('Correct!'), end
 

would produce the line 

Correct!

         

The | (or) logical operator 

The | (or) operator lets you create a more flexible set of conditions that must be met before an action can take place. The syntax line for an IF statement using the | operator is as follows: 

if condition1 | condition2 then statement

Note that the IF statement contains two conditions. Only one of these conditions needs to be true before Scilab can execute the statement following THEN. Scilab also executes the statement following THEN if both conditions are true. Here's an example:  

if quota > 10 | sales > 1000 then disp('Good job!')

If both of these conditions are false, Scilab ignores the rest of the line and executes the next statement in your program. 

These three lines of code
 

quota = 15;
sales = 459;
if quota > 10 | sales > 1000 then disp('Good job!'), end 

would produce the line 

Good job!

The ~ (logical not) operator 

The ~ (not) operator lets you negate a condition: if a condition is false, the ~ operator makes the condition true; if a condition is true, ~ makes it false. Here's the syntax for an IF statement using the ~ operator: 

if ~condition then statement 

~ (not) is useful when you want to execute a statement when a condition is not true. Here's an example:
 

age = 17;
if ~( age >=18 ) then disp('Sorry, you cannot vote!'), end
 

Scilab would produce a line reading “Sorry, you cannot vote!


Using ELSE with IF and THEN 

Now you know how to make Scilab evaluate a condition and take an action if the condition is true or if the condition is false. But what if you want Scilab to choose between two actions based on the condition?

When compared with IF and THEN, ELSE lets you specify two separate actions for Scilab: one action (following THEN) if the condition is true, and a different action (following ELSE) if the condition is false. 

Here's the syntax for an IF statement using THEN and ELSE (remember that the keyword THEN can be replaced by a carriage return or a comma): 

if condition
   statement1
else
   statement2
end 

where condition is the logical condition you want Scilab to evaluate, statement1 is the statement that Scilab executes if condition is true, and statement2 is the statement Scilab executes if condition is false.

The ELSEIF Keyword 

ELSEIF is similar to ELSE in that it provides an alternative course of action if condition is false. With ELSEIF, however, you supply another condition for Scilab to evaluate. Here's the syntax for an IF statement that uses ELSEIF:
 

if conditionl
   statements
elseif condition2
   statements
elseif condition3
   statements
else
   statements
end

 
There’s no limit to the number of ELSEIF statements and associated conditions you can use.


 From 'If statement Scilab' to home

 From 'If statement Scilab' to Scilab Examples


Top


footer for matlab page