IFs, Loops & Logic

From COD Modding & Mapping Wiki
Revision as of 19:53, 15 October 2008 by Zeroy (talk | contribs) (New page: == IFs, Loops & Logic == Image:Nutshell.png This section is to go into a little more detail of how to use statements such as If and different loops. An 'If' statement is used to ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

IFs, Loops & Logic

This section is to go into a little more detail of how to use statements such as If and different loops.

An 'If' statement is used to compare data and decide what is done after the data is compared.

To go into detail in this section, It is required you know of 'Logic' and the operators used to compare data.

So...

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

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

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

Here are some examples...

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 statement is true, do this code
}
// more code here

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 true
}
Else
{
  // If False
}

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 first arguement is false, but second is true.
}
Else
{
  // If all arguements are false
}

Thats the basics of If's, so let move 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, an arguement is needed "while(arguement);" Often, this loop is used for infinite loops. An infinite loop is a loop that loops forever. 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.

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.

An 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 is read, "'i' is equal to 0, while 'i' is less than 'int', add 1 to i. Lets use the code, and replace int...

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

This is the sequence of events...

- 'i' = 0 - loop check "while" (if i is less than 10, continue) - perform code (wait 1;) - increment 'i' (i++) - 'i' = 1 - etc.

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 manual stopped.

The problem with infinite loops is they give an error if you do not allow the loop to take a breath.

Infinite loops require a wait statement. If you get an "Infinite Loop" error, this is the reason.

That is about the loops, but 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 exiting an infinite loop...

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

The above sequence simply goes... - Wait 1 - check if statement... + if var1 is equal to var2, exit loop + else continue - loop