IFs, Loops & Logic

From COD Modding & Mapping Wiki
Revision as of 02:38, 24 October 2008 by Novemberdobby (talk | contribs)
Jump to navigation Jump to search

IFs, Loops & Logic

This section will go into a little more detail of how to use 'if' statements and loops.

An 'if' statement is used to verify whether some data satisfies certain conditions, and then to execute code depending on the outcome.

To understand this section, you must first know the operators used to compare data:

== :: Equal To
!= :: Not Equal To
!  :: Negation (Not equal to)
<  :: Less than
>  :: Greater than
<= :: Less or Equal to
>= :: Greater or Equal to
&& :: And
|| :: Or

Ok, now that we have some operators, lets get started on the statement.

An 'if' statement, requires a minimum of one arguement and usually one operator.

Here are some examples...notice the lack of semicolons at the end of the statements.

 if(variable) //If variable is true
 if(!variable) //If variable is not true
 if(variable1 == variable2) //If variable1 is equal to variable2
 if(variable1 != variable2) //If variable1 is not equal to variable2
 if(integer1 < integer2) //If integer1 is less than integer2
 if(integer1 > integer2) //If integer1 is greater than integer2
 if(integer1 <= integer2) //If integer1 is less than or equal to integer2
 if(integer1 >= integer2) //If integer1 is greater or equal to integer2
 if((var1 == var2) && (var3 != var4)) //If var1 is equal to var2 AND var3 is not equal to var4
 if((int1 > int2) || (var1 == var2)) //If int1 is greater than int2 OR var1 is equal to var2

To use an if statement to determine the movement of the script, you need to use the arguements to move the script in certain directions...

 if(var1 == var2)
 {
   //if var1 equals var2, do this code
   //everything inside these curly braces
 }
 //then do this code regardless of the result
 

If the statement above is true, the code inside the curly brackets is processed, if it is not true, the code inside the brackets are skipped.

Whether or not the statement is true, the code outside of the brackets is going to be processed. If this is not what you want, you need to use "Else" after the statement, for example...

 if(var1 == var2)
 {
   //if it's true then do this
 }
 else
 {
   //if it's false then do this
 }

You can also use an "else if" in the statement. This is used in a scenario where you want to check multiple comparisons.

 if(var1 == var2)
 {
   //if above arguement is true
 }
 else if(!var1 && var3)
 {
   //if var1 is false but var3 is true
 }
 else
 {
   //if all other if statements were false
 }

Thats the basics of if's, so let move on to loops.

Loops come in different forms...

While :: A while loop is a loop that keeps looping WHILE the arguement is true. For :: A for loop is a loop that loops a set amount of times

To use a while loop, a condition/some conditions must be stated: "while(conditions)" Often, this loop is used for infinite loops, which last forever unless specifically stopped. This is done using the arguement of 1 or true (1 is the integer of true)

while(1)
while(true)

A while loop can also be used as a normal loop that loops while the arguement is true, when the arguement becomes false the loop exits automatically (or rather, doesn't begin executing the commands in the loop again but just finishes the loop in progress).

 int = 0;

 while(int < 10)
 {
  wait 1;
  int++;
 }

The above code will loop while 'int' is less than 10. The loop waits 1 second, and then the loop increments 'int'. Once 'int' is not less than 10, the loop breaks.

The same applies for FOR loops.

A FOR loop requires 3 arguements.

for(declare; while; do)

Declare is the section which declares a variable for the loop to use.

While is what determines when the loop breaks

Do is what the loop should do after each loop.

A common FOR loop looks like this...

 for(i = 0; i < int; i++)

The above code means: (i is 0, while i is smaller than int, add one to i).

Let's replace int with an actual number:

 for(i = 0; i < 10; i++)
 {
  wait 1;
  thread function();
 }

This is the sequence of events...

- 'i' = 0 - while i is less than 10, continue the loop - perform code (wait 1; and function()) - increment 'i' (i++)

The FOR loop can also be used as an "infinite loop" using the "forever loop"

for(;;)

The above will simply keep repeating the code until manually terminated.

The problem with infinite loops is that they will give you an error if you don't allow them to pause at any point - so all infinite loops require a wait statement somewhere in the executed code.

To finish off, we need to know how to manually exit these loops. A common way to exit an infinite loop is to use an IF statement to determine when to 'break' (break is the keyword used to exit a loop) - here is an example of an IF statement breaking an infinite loop...

 for(;;)
 {
   wait 1;
   if(var1 == var2)
   {
     break;
   }
 }

The above sequence simply goes...

- wait 1 second - check if statement... - if var1 is equal to var2, break out of the loop - else continue the loop