Learning MEL – Step #6 – Using Conditionals in MEL

In this tutorial I am going to make some output using conditionals. It is very easy and indeed essential to learn. However, for further advanced uses, it turns to be somehow complex. After all,  make sure that you have understood the previous section if you are an absolute beginner. At the bottom of this tutorial, there are several important notes about conditionals which must be read.

In this project I would tell you if X=1 then do this and if not do other thing. Very easy. But have in mind the syntax based on which the conditional is provided. Now I move to explain our little project.

  • What is/are the function of the script?
  • What is/are the design, order and/or segment of execution?
  • Command and Functions

What is/are the function of the script?

The script sees what the conditional include, and get the variable or place to which conditional is attached, then checks the value of variable etc., if it meets the condition then the given statement(s) is/are execute respectfully, if not it skips over the other following statement(s) or steps over to next part of conditional (optional). It is too vague now, I know. Let’s see it in practice in a few moments.

What is/are the design, order and/or segment of execution?

It includes basically three parts:

The conditioning, the checking, and then executing or skipping. (don’t pay attention to the // Mel Script , it is simple a comment, when there are two // may wont execute that line since it is considered (and is) to be a comment and nothing more. I will go over it later.

// MEL script

if($x==”Childhood”)

{

print (“This is a true word.”);

}

Look at the above script. It says that if $x is equal to the word “Childhood” then please print the phrase “This is a true word.”

But something is missing. Where the script goes to check $x, or let’s ask where is the $x? It must be before the conditional so that conditional checks something already read by execute (Maya). If it be after conditional then it means there is no $x and so you would get an error reporting undeclared variable. Look at the following part, now it is complete:

// MEL script

$x = “Manhood”;

if($x==”Childhood”)

{

print (“This is a true word.”);

}

With the above script what you get is NOTHING. For two big reasons:

1- You have not stated in the script that in case of FALSE condition, what to do.

2- You script ends by the conditional, for instance if you had lines after it, Maya would just like always execute them, after checking the conditional, regardless of its FALSE or TRUE.

But if $x = “Childhood” the phrase would have been outputted. Now it is the time of dissection:

if($x==”Childhood”)

The above line is the conditional statement. I haven not read anywhere but I call it the header of conditional. It is here that the trueness or falseness of the result is achieved. In IF-Statement you MUST only and only use “==” to check equality, and not “=” which is “assignment” in programming language.

{

print (“This is a true word.”);

}

In order for the conditional to work you must use opening curly bracket “{” and write you statements and the close it with closing curly bracket “}”. If you miss to put them you encounter syntactical error.

You could have done millions of jobs if the conditionals is true.

Note that with true I mean “meeting the standard of your conditional”. It means what you have stated is checked and is confirmed. For instance you could have told if $x is not equal to 5 then do something. Then if $x be 2, it means that it is not 5, and your conditional is true however.

Now what to do if the condition is false. You may decide to do something in case of FALSE condition.

It is easy, just next after the opening parenthesis put a “!” (exclamation sign) and then write the rest of your conditional.  For instance if you want to tell MEL if $x is not equal to zero then do something, you just state it as so:

if(!$x==0)

{

print(“The variable X is not equal to zero.”);

}

Now Maya checks if $x is equal or not, and if it is not, then it execute the statement within the body of conditional, and if not goes with the rest of script.Now it is the time of real application. Let’s say if $x is equal to 2 print something, and if equals to 3 print something else. But I don’t go with one-time print execution, I want to do something so line by line our phrases get printed. Look here:

int $x = 2;
if($x == 2)
{
print (“Number 2 is printed.”);
$x = 3;
}
if($x == 3)
{
print (“Number 3 is printed.”);
}

In the above code you see two conditionals coming after each other. One following the other one. You can do this with unlimited number of conditionals. No two If-Conditionals in anyway could be related, and they are fully performing independently. Now, since each conditional is part of a script, it can modify, change or use variables and other accessible elements. In the first conditional we see,as regular  in case of true, it goes into the body of conditional where statements are stored. I have put the print function and then have changed the variable’s value to 3, so the executor moves forward and reaches the next conditional, here it checks for it and due to the fact that I have changed the variable’s value to 3,  it accounts for true and execute the insiders. You see nice it is. Much use in you future acts. Below you see a nice list of important points about conditionals:

  • Conditionals must be composed within two sections: One the parenthesis and another curly brackets.
  • Parenthesis are for condition to be checked while curly brackets are for the statements to be executed if the conditions is met as true.
  • Every statement within the body of conditionals must be followed by a semi-column.
  • If you want to check for true/false of your condition and if not met, run another condition, you must use “else”, as already described.
  • Else does not need (and must not) be compiled with condition-definition (parenthesis containing the condition just like if-statement), since it is simple the opposite of if-statement.
  • There are other types of conditionals which would be covered later, but fundamentally we have ended one important conditionals.
  • A variable declared within a conditional, could not be accessed/used from outside of it. Therefore the following code would report “undeclared variable” error. Because you have not declared the variable from outside of it, and it is a rule in MEL that variable declared within a conditional are only usable during the same conditionals. Thus, it is strongly recommended to declared variable before conditionals to avoid such problems.

Incorrect Code:

int $x = 2;
string $z = “This is regular variable”;
if($x == 2)
{
print (“Number 2 is printed.”);
$x = 3;
string $y = “This is condition-restricted variable”;
}
if($x == 3)
{
print (“Number 3 is printed.”);
}
print $y;

Correct Code:

int $x = 2;
string $z = “This is regular variable”;
if($x == 2)
{
print (“Number 2 is printed.”);
$x = 3;
string $y = “This is condition-restricted variable”;
}
if($x == 3)
{
print (“Number 3 is printed.”);
}
print $z;
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s