|
|
|
Published : April 29, 2006 |
Author : Ali Roman
Category : Scripting languages | Total Views
: 663 | Rating :     
|
|
|
|
Th IF Structure
The if structure is used when you want to perform a specific/indicated action. It is activated when the condition becomes true. For example you have assigned a value to variable x=0 and you use if strutcutre like this
if(x==10) { perform any action; }
in this case this statement will be activated when the value of x becomes 10. If the value of x becomes 10 it means the conidtion is true. Basically we use it for comparing two or more statements.
How to use IF Structure ------------------------------- It is very simple to use, for example you are moving a sprite on x-axis and you are continously increasing its _X position. e.g the initial _X of your sprite was 0 and you want that when it reaches 200 then you want your sprite go back on _x=0 and start. What you will do is
Example 1 ------------- 1. Make a rectangle and convert it to sprite. Rename "sprite" as "bar". From transform tab place it on X=0 2. On the frame number 1 paste this code
| CODE |
onEnterFrame() { bar._X+=1; if(bar._X==100){ bar._X=0; } } | Now in this line bar._X+=1; we are only changing the position continously but in code
if(bar._X==100){ bar._X=0; }
we are comparing the X with 100, here we have stated that if X reaches 100 then again assign a new value to X which is 0.
else-if Structure The [b]else is used when you also want to perform an action when the statement is flase. For example in the above Example we have only used if structure. Ok now I will make the same example using if else
Example 2 ------------- 1. Make a rectangle and convert to sprite. Rename "sprite" as "bar". Place it on X=0 2. On the frame number 1 paste this code
onEnterFrame() { if(bar._X==100){ bar._X=0; } else bar._X+=1; }
Now this example will also perform the same action as Example 1. But the only difference is that we have used if else. Concentrate on the code, you have seen when the movie starts the bar._x=0 because we have palced it at 0, it means the if is activated.
This is how if and if else statements works. You can make as many comparisons you want. In the above example 2 we have made only one if structure
What is a typical else if The word Typical in this case, is in the sense, you might have seen codes like this
| CODE |
|
on (release) {
if(inputVar=="Ali"){ dynVar="Administrator"; } else if(inputVar=="Charly"){ dynVar="Moderator"; } else if(inputVar=="rob88wells"){ dynVar="Co-Admin"; } else if(inputVar=="johno"){ dynVar="Member"; } else dynVar="Guest";
} | In the above code you have made one input box and its variable name is inputVar and one dynamic text box and its variable name is dynVar. Made on buttons and on its action we used the aboce code. Now this code will check the string (means the text you write) if it is "Ali" then in Dynamic TEXT box (dynVar) it will display "Administrator", the same if the string is "Charly" it will display "Moderator". After comparing all else if structure, if there are no matches then it will display "Guest" in the dynamic text box.
So now you can make and use else if and simple if structures. Ok - here is the list of operators you will be using in these structures.
Operator Description < relational less than <= relational less than or equal to > relational greater than >= relational greter than or equal to = assignment operator (for assignin any value to any variable/expression) == is equal to != is not equal to
&& logial AND || logical OR
some other operator which may be used in other codings. += addition asignment -= subtraction asignment *= multiplication asignment += division asignment
I think its enough to understand for an intermediate or basic level user. If you have any further questions, feel free to ask.
best regards - Ali Roman...
|
|
|
Visitor's Comments ! |
|
|
|
Random Pick |
well not sure what to write since I am just setting the site up and posting some of my old work, but I will keep it updated. I was just working on the custom CMS which I am developing for fanslogon.com there were some minor errors... |
|
|
Statistics |
| » Total Articles |
72
|
| » Total Authors |
96
|
| » Total Views |
116332
|
| » Total categories |
6
|
|