IFs, Loops & Logic: Difference between revisions

From COD Modding & Mapping Wiki
Jump to navigation Jump to search
(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 ...)
 
No edit summary
Line 1: Line 1:
== [[IFs, Loops & Logic]] ==
== [[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.
[[Image:Nutshell.png]] This section will go into a little more detail of how to use 'if' statements and loops.


An 'If' statement is used to compare data and decide what is done after the data is compared.
An 'if' statement is used to verify whether some data satisfies certain conditions, and then to execute code depending on the outcome.


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


So...
<pre>
<pre>
== :: Equal To
== :: Equal To
!= :: Not Equal To
!= :: Not Equal To
!  :: Negation (Not)
!  :: Negation (Not equal to)
<  :: Less than
<  :: Less than
>  :: Greater than
>  :: Greater than
Line 19: Line 18:
|| :: Or
|| :: Or
</pre>
</pre>
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.
Ok, now that we have some operators, lets get started on the statement.


Here are some examples...
An 'if' statement, requires a minimum of one arguement and usually one operator.


  If(variable) // If variable is true
Here are some examples...notice the lack of semicolons at the end of the statements.
  If(!variable) // If variable is not true
<pre>
  If(variable1 == variable2) // If variable1 is equal to variable2
  if(variable) //If variable is true
  If(variable1 != variable2) // If variable1 is not equal to variable2
  if(!variable) //If variable is not true
  If(integer1 < integer2) // If integer1 is less than integer2
  if(variable1 == variable2) //If variable1 is equal to variable2
  If(integer1 > integer2) // If integer1 is greater than integer2
  if(variable1 != variable2) //If variable1 is not equal to variable2
  If(integer1 <= integer2) // If integer1 is less than or equal to integer2
  if(integer1 < integer2) //If integer1 is less than integer2
  If(integer1 >= integer2) // If integer1 is greater or equal to integer2
  if(integer1 > integer2) //If integer1 is greater than integer2
  If((var1 == var2) && (var3 != var4)) // If var1 is equal to var2 AND var3 is not equal to var4
  if(integer1 <= integer2) //If integer1 is less than or equal to integer2
  If((int1 > int2) || (var1 == var2)) // If int1 is greater than int2 OR var1 is equal to var2
  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
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((int1 > int2) || (var1 == var2)) //If int1 is greater than int2 OR var1 is equal to var2
 
</pre>
  If(var1 == 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...
<pre>
  if(var1 == var2)
  {
  {
   // If statement is true, do this code
   //if var1 equals var2, do this code
  //everything inside these curly braces
  }
  }
  // more code here
  //then do this code regardless of the result
</pre>


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.
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...
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...
 
<pre>
  If(var1 == var2)
  if(var1 == var2)
  {
  {
   // If true
   //if it's true then do this
  }
  }
  Else
  else
  {
  {
   // If False
   //if it's false then do this
  }
  }
 
</pre>
You can also use an "Else If" in the statement. This is used in a scenario where you want to check multiple comparisons.
You can also use an "else if" in the statement. This is used in a scenario where you want to check multiple comparisons.
 
<pre>
  if(var1 == var2)
  if(var1 == var2)
  {
  {
   // If above arguement is true
   //if above arguement is true
  }
  }
  Else if(var1 == var3)
  else if(!var1 && var3)
  {
  {
   // If first arguement is false, but second is true.
   //if var1 is false but var3 is true
  }
  }
  Else
  else
  {
  {
   // If all arguements are false
   //if all other if statements were false
  }
  }
 
</pre>
Thats the basics of If's, so let move to Loops.
Thats the basics of if's, so let move on to loops.


Loops come in different forms...
Loops come in different forms...
Line 79: Line 81:
'''For''' :: A for loop is a loop that loops a set amount of times
'''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);"
To use a while loop, a condition/some conditions must be stated: "while(conditions)"
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)
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(1)
  while(true)
  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.
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).
 
<pre>
  int = 0;
  int = 0;


Line 94: Line 96:
   int++;
   int++;
  }
  }
 
</pre>
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 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.
The same applies for FOR loops.


An FOR loop requires 3 arguements.
A FOR loop requires 3 arguements.


for(declare;while;do)
for(declare; while; do)


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


While is what determines when the loop breaks
'''While''' is what determines when the loop breaks


Do is what the loop should do after each loop.
'''Do''' is what the loop should do after each loop.


A common FOR loop looks like this...
A common FOR loop looks like this...
<pre>
for(i = 0; i < int; i++)
</pre>
The above code means: (i is 0, while i is smaller than int, add one to i).


for(i=0;i<int;i++)
Let's replace int with an actual number:
 
<pre>
The above code is read, "'i' is equal to 0, while 'i' is less than 'int', add 1 to i.
  for(i = 0; i < 10; i++)
Lets use the code, and replace int...
 
  for(i=0;i<10;i++)
  {
  {
wait 1;
  wait 1;
  thread function();
  }
  }
 
</pre>
This is the sequence of events...
This is the sequence of events...


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


The FOR loop can also be used as an "infinite loop" using the "forever loop"
The FOR loop can also be used as an "infinite loop" using the "forever loop"
Line 134: Line 136:
  for(;;)
  for(;;)


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


The problem with infinite loops is they give an error if you do not allow the loop to take a breath.
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.
 
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...


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...
<pre>
  for(;;)
  for(;;)
  {
  {
   wait 1;
   wait 1;
   if(var1 == var2)
   if(var1 == var2)
    {
  {
      break;
    break;
    }
  }
  }
  }
</pre>
The above sequence simply goes...


The above sequence simply goes...
- wait 1 second
- Wait 1
- check if statement...
- check if statement...
+ if var1 is equal to var2, exit loop
- if var1 is equal to var2, break out of the loop
+ else continue
- else continue the loop
- loop

Revision as of 02:38, 24 October 2008

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