Scripting - Switch, For, While, Break and Continue - SWiSHMax Tutorials
 
  Buy Ebooks, Video Games, DVDs, Electronics and more !
Home | Help Forums | Web Design Tutorials | Free Swishmax Downloads Make Money Online Blog . Free Image and Photo Sharing and Hosting

 

 

Company

Home

Nework Details

Tutorials

SWiSH 2

SWiSHMax

Flash MX

PHP & MySQL

HTML / JScript

Adobe Photoshop

Corel Draw

Gimp

Miscellaneous

Downloads

Templates

Plugins

Fonts

Wallpapers

Free Images

Scripts and Codes

Products

E-Books

SWiSH BB

SWiSH Templates

SWiSH-DB Newsletter
Subscribe to our newsletter : GO
  Partners / Affiliates

   Free image hosting
   Web Design Pakistan
   Swish Templates
   SWiSH Climax
   Talkfreelance Community
   Template Desire
   Lite boards
   Script Sector
   GimpTalk
   13dots
   Idea Designs
   PHP Cafe
   Sposatoettore
   Free swishmax templates
   Swishzone

Manage your site with Flax Article Content Management System
Scripting - Switch, For, While, Break and Continue
Category : SWiSHMax | Level : Beginner | Language : English
Ask Question | Read Comments | Post tutorial | Previous | Next

Web www.swish-db.com

This is a run through of all the "conditional" scripting options, aside from the if/else which has been covered accordingly (and extremely well) already.

We'll start with the switch statement.

Switch
use this if you have a variable that you want to test for a series of different variables


CODE
a = some_number;
   switch (a) {
       case 1: a = 2;
       case 2: a = 3;
   
       default: a = 0;
   }


line 1. variable instantiation. create the variable and insert a value.

line 2. switch statement. this takes the variable in the parameter under subject.
(i.e. it looks at 'a' and uses it as a control)

line 3+4. case statement. this takes the variable 'a' and looks at its value. for
"case 1:" it looks at the value of 'a' and checks to see if the value is
indeed the numerical value 1. If the value of 'a' is 1, it continues on to the
statement "a = 2;" then exits the switch statement.

line 6. the default is merely what happens if 'a' doesn't have any of the "case"
statement values. if 'a' is neither the value 1 or 2, then it automatically
goes to "default"

In Brief:
The switch statement is basically what you do to avoid a series of if/else statements and all the wonderful coding it usually entails.

variations
If you're using characters, remember to use ' ' around the character itself in the "case" statements, and if you're using strings, remember to use " " around the string itself in the "case" statements.

most common problem The cases aren't labeled as "case number 1, case number 2". the cases are what values are being tested. When you see "case 1:" it doesn't mean the FIRST case being tested, it means the value in the switch statement is being compared to "1" or "if(a == 1)"


______________________________________________________________________



While
use this for automation and dynamic variable checking


CODE

   var_one = 0;
   x = 0;
   
   while (x != 9) {
       var_one++;
       x++;
   }


line 1+2. variable instantiation.

line 4. while statement. This takes the condition inside the parameters and checks it upon every iteration and upon entry. i.e. it checks the condition within the parameter first "x != 9" and if x does indeed not equal 9, then it enter the while statement.

line 5+6. the variables are both being increased by one.

iterations. this continues until x == 9 and the while( ... ) checks the condition upon every iteration. look at the while loop as a cycle, it checks the condition within the parameters, and if they're true it does everything within the brackets of the while loop and then checks the condition in the parameter again. It does this until the condition is false, then it skips the while loop and goes on to finish the scripting procedures.

WARNING This is where the infamous "infinite loop" originates. If your condition within the while loop parameters WILL NEVER BECOME TRUE then your program will be stuck looping through the body of your while statement FOREVER. This is no big deal if you're just adding numbers...but what if your creating sprites? This will bog down your CPU and slow your browser/movie down.

variations the "do-while" statements is the same thing as the while statement except if doesn't check the condition first. It simply does everything within the body of the do-while THEN checks the condition. So no matter what, it will run whatever is in the body of the do-while AT LEAST once.

CODE

   var_one = 0;
   x = 0;
   
   do{
       var_one++;
       x++;
   }while (x != 9)


most common problem remember, in a condition you use == to compare...and in a statement you use = to assign. if you can't run your while loop, chances are you have (something = something) as your condition instead of (something == something).

______________________________________________________________________



For
use this for automation


CODE

   b = 0;

   for (a =0; a < 20; a++ ) {
     b++;
   }


line 1. variable instantiation.

line 3. the for statement. Let's take a look at the parameters to understand this machine. the first parameter "a = 0" is the initialization of the variable 'a', nothing more. the second parameter "a < 20" is the condition, meaning upon entry and after every iteration 'a' will be checked to assure that it is still less than 20, if it is not it will break from the for statement and continue on to the rest of the code in the script. the third parameter is what happens after every iteration of the for statement. If 'a' is less than 20, it will increase 'b' by one (the body) THEN increase 'a' by one.

line 4. this is just increasing 'b' by one.


WARNING again, be wary of the "infinite loop" problem. it exists for for statements as well.

variations the only variation I know of is in Java 1.5.0 where you're allowed to use only two paramters instead of three

most common problem the for statement does NOT go through the first parameter every time. i.e. it does NOT set 'a' to 0 every iteration.

______________________________________________________________________



Break
use this to exit a loop


CODE

   b = 0;

   for (a =0;a<20;a++ ) {
     b++;
     if(b == 7) break;
   }



line 5. once 'b' equals the value of 7, the for loop will cease because control will "break" out of the for loop and continue on to the remainder of the script.

most common problem the "break" statement only gets you out of one loop, it does not exit the entire script altogether. so if you're within 5 loops and you break, you will go to the 4th loop.

______________________________________________________________________



Continue
is useless


CODE

   b = 0;

   for (a =0;a<20;a++ ) {
     b++;
     if(b == 7) continue;
   }



line 5. once 'b' equals the value of 7, the loop continues on.
______________________________________________________________________





I'm familiar with programming, but just starting "swishing", so if i'm off please, by all means, correct me.


© 2002-2005 Flaxweb Network | Article Manager | Gimp Tutorials Library | RAD C++ Library | Free C/C++ Sourcecode |
Sites of interest : Web Design Blog | Swish Templates | Swishmax Ebook | Photoshop Templates | Gimp Tutorials | Text Forum | Make Money Online