Conditional statements: Difference between revisions

From Legacy Roblox Wiki
Jump to navigationJump to search
>Gamekid334
>Flurite
(Honestly, why are we describing the setup of studio and making scripts on this page? Also, reworded stuff.)
 
(31 intermediate revisions by 5 users not shown)
Line 1: Line 1:
__TOC__
__TOC__


==Introduction==
This guide is for absolute beginners. It is intended to familiarize you with conditionals, or conditional statements, in Lua. If you haven't already, please see [[Your first script]] as a beginner tutorial. You will learn:
This guide is for absolute beginners. It is intended to familiarize you with conditionals, or Conditional statements, in Lua. If you haven't already, please see [[Your first script]] as a beginner tutorial.
 
== What this tutorial will teach you ==


* What conditional statements are
* What conditional statements are
* Certain types of conditional statements
* Certain types of conditional statements
* How to use conditional statements
* How to use conditional statements
== Setup ==
You will need to open [[Roblox Studio]].  Once you have done that, you will need to click "My ROBLOX", select your map, and click "Edit". At this point, you should see your familiar map.
You will need some essentials in [[Roblox Studio]] to make this work:
* The Output window:
[[Image:Outputwindow.JPG]]
* The Explorer window:
[[Image:Explorerwindow.JPG]]
and
* The Command bar:
[[Image:Commandtoolbar.JPG]]
* To make sure you have the Output window, click View / Output.
* To make sure you have the Explorer window visible, click View / Explorer.
* To make sure you have the Command bar visible, click View / Toolbars / Command.


== Discussion ==
== Discussion ==
Line 32: Line 11:
"Conditional statements are a way of responding differently based on the result of a comparison, called a conditional expression."[http://wiki.garrysmod.com/wiki/?title=Basic_Lua#Conditionals]  
"Conditional statements are a way of responding differently based on the result of a comparison, called a conditional expression."[http://wiki.garrysmod.com/wiki/?title=Basic_Lua#Conditionals]  


This means that you are comparing the value of two (or more) things.  Let's suppose we want Roblox <b>if</b> (2+3) is equal to 5, <b>then</b> tell us that (2+3) == 5 <b>.</b>
This means that you are comparing the value of two (or more) expressions.  Let's suppose that <b>if</b> 2+3 is equal to 5, <b>then</b> we want Roblox tell us that 2+3 == 5 <b>.</b>


Lua is similar to human language in this regard, we type:
Lua is similar to human language in this regard, we type:


<b>if</b> (2+3) == 5 <b>then</b> print ("(2+3)==5") <b>end</b>
{{code and output|code =
if (2+3) == 5 then
    print ("(2+3)==5")
end
|output = (2+3)==5
}}


Always end an if/then statement with "end".  "End" is like a period to a sentence.
Always close an if/then statement with "end", as it marks the closing of the 'if' block.


An illustration of this in a flowchart would be:
An illustration of this in a flowchart would be:
Line 46: Line 30:
Here's another example.  Let's evaluate <b>if</b> (10-2) is greater than 3, <b>then</b> tell us that (10-2) > 3 <b>.</b>:
Here's another example.  Let's evaluate <b>if</b> (10-2) is greater than 3, <b>then</b> tell us that (10-2) > 3 <b>.</b>:


<b>if</b> (10-2) > 3 <b>then</b> print ("(10-2)>3") <b>end</b>
{{code and output|code =
if 10-2 > 3 then
    print("10-2 is greater than 3")
end
|output = 10-2 is greater than 3
}}


One last example.  Evaluate <b>if</b> 100 is not equal to 4, <b>then</b> tell us that (100~=4)<b>.</b>
One last example.  Evaluate <b>if</b> 100 is not equal to 4, <b>then</b> tell us that (100~=4)<b>.</b>


<b>if</b> 100~=4 <b>then</b> print ("100~=4") <b>end</b>
{{code and output|code =
if 100 ~= 4 then --the '~=' operator means 'not equal to'
    print("100 is not equal to 4")
end
|output = 100 is not equal to 4
}}


== Scripting ==
== If ==


Open [[Roblox Studio]] and make sure that there aren't any other scripts in the [[Explorer]] window. We're going to start off with a fresh script.
The 'if' condition is the starting statement to all conditional blocks of code. Use the 'if' statement to execute a block of code if the inputted expression is true.


* Click Workspace in the Explorer Window.
{{code and output|code =
* Click Insert > Object > Script
if 2 + 3 == 5 then print ("2+3==5") end
* Double click the newly inserted script.
if 5 > 2 then print("5>2") end
* A blank window should open up.
if 2 < 5 then print("2<5") end
* Insert the following script:
if 5 >= 5 then print("5>=5") end
 
if 5 <= 5 then print("5<=5") end
<pre>
if 1 ~= 100 then print("1~=100") end
if (2+3) == 5 then print ("(2+3)==5") end
|output =
if 5 > 2 then print ("5>2") end
2+3==5 (because 5 is equal to 5)<br>
if 2 < 5 then print ("2<5") end
5>2 (because 5 is greater than 2)<br>
if 5 >= 5 then print ("5>=5") end
2<5 (because 2 is less than 5)<br>
if 5 <= 5 then print ("5<=5") end
5>=5 (because 5 is greater than or equal to 5)<br>
if 1 ~= 100 then print ("1~=100") end
5<=5 (because 5 is less than or equal to 5)<br>
 
1~=100 (because 1 is not equal to 100)<br>
</pre>
}}
 
* Test the script by pressing the play button [[Image:Play.JPG]]
* Your Output bar should show:
<b>(2+3)==5</b> (because 5 is equal to 5)<br>
<b>5>2</b> (because 5 is greater than 2)<br>
<b>2<5</b> (because 2 is less than 5)<br>
<b>5>=5</b> (because 5 is greater than or equal to 5)<br>
<b>5<=5</b> (because 5 is less than or equal to 5)<br>
<b>1~=100</b> (because 1 is not equal to 100)<br>


== Else ==
== Else ==


What happens if your statement is <b>not</b> true?  That's when we can use an "else" statement. The <b>"else"</b> statement is similar to "otherwise" in English.  For example, "<b>If</b> it is raining, bring an umbrella, otherwise (i.e., <b>else</b>) bring your suntan lotion."
In an 'if..else' block of code, the statements under the 'else' block will execute if the 'if' condition is false. Keep in mind that 'else' statements can only be used when there is an 'if' statement.


Let's see this as applied to scripting:
Let's see this as applied to scripting:


<pre>
{{code and output|code =
if 10>100  
if 10 > 100 then  
then  
    print("10 is greater than 100")  
print("10 is greater than 100")  
else  
else  
print("10 is less than 100")  
    print("10 is less than 100")  
end
end
</pre>
|output = 10 is less than 100
}}


Since 10>100 is false, the <b>then</b> statement won't execute, but the <b>else</b> statement will.
Since 10>100 is false, the <b>then</b> statement won't execute, but the <b>else</b> block will.


[[Image:Ifelseflowchart.JPG]]
[[Image:Ifelseflowchart.JPG]]
Line 102: Line 87:
== Elseif ==
== Elseif ==


If you want to execute a script if one of several conditions are true, we can use the <b>elseif</b> statement.
In an 'if..elseif'' block of code, the compiler will go from top to bottom, checking whether each, if any, of the conditions are true. If one of the conditions are true, the corresponding block of code will be executed.


"<b>If</b> it is raining, bring your umbrella.  <b>Otherwise, if</b> (i.e., "elseif") it isn't raining, bring your suntan lotion."
"<b>If</b> it is raining, bring your umbrella.  <b>Otherwise, if</b> (i.e., "elseif") it is sunny, bring your suntan lotion."


Since either "it is raining" or "it isn't raining" will be true, we can use the "elseif" statement.  Let's try that in a script:
We can use the "elseif" statement.  Let's try that in a script:


<pre>
{{code and output|code =
if 10>100  
if 10 > 100 then
then  
    print("10 is greater than 100")
print("10 is greater than 100")  
elseif 10 > 50 then
    print("10 is greater than 50")
elseif 10 < 100 then
    print("10 is less than 100")  
end
|output = 10 is less than 100
}}


elseif (10>50)
The only instance any of the conditions would be true is 10<100, which would result in the printing of "10 is less than 100".
then
print("10 is greater than 50")


elseif (10<100)
[[Image:Elseif.JPG]]
then
print("10 is less than 100")


end
'''Note:''' Blocks of 'if..elseif..else' code can be used.
</pre>
 
== Advanced Usage ==
To eliminate the need for long and winding if statements, there is another way. It has the following advantages:
* Makes your script shorter
* Increases the readability of you script
* Is simpler to write
 
Here is an example:
{{code and output|
code=
local variable = 1
print(variable == 1 and "The variable is equal to one."
                    or "The variable is not equal to one")
variable = 23
print(variable == 1 and "1"
  or variable == 2 and "2"
  or variable == 3 and "3"
  or                  "Something else")
|output=
The variable is equal to one.
Something else
}}
This method uses two operators: "and" and "or".
 
This could be compared to a '''if...then''' and a '''else''' statement.


The only instance that would be true is 10<100, which would result in the printing of "10 is less than 100".
Several '''elseif...then''' statements can be rowed up, like in the first example, where the local variable "variable" is compared with several questions and then,


[[Image:Elseif.JPG]]
if none of the statements are true, uses the '''else''' statement, if that is provided by you.


[[Category:Scripting Tutorials]]
[[Category:Scripting Tutorials]]

Latest revision as of 15:41, 16 March 2012

This guide is for absolute beginners. It is intended to familiarize you with conditionals, or conditional statements, in Lua. If you haven't already, please see Your first script as a beginner tutorial. You will learn:

  • What conditional statements are
  • Certain types of conditional statements
  • How to use conditional statements

Discussion

"Conditional statements are a way of responding differently based on the result of a comparison, called a conditional expression."[1]

This means that you are comparing the value of two (or more) expressions. Let's suppose that if 2+3 is equal to 5, then we want Roblox tell us that 2+3 == 5 .

Lua is similar to human language in this regard, we type:

if (2+3) == 5 then
    print ("(2+3)==5")
end
(2+3)==5

Always close an if/then statement with "end", as it marks the closing of the 'if' block.

An illustration of this in a flowchart would be:

Here's another example. Let's evaluate if (10-2) is greater than 3, then tell us that (10-2) > 3 .:

if 10-2 > 3 then
    print("10-2 is greater than 3")
end
10-2 is greater than 3

One last example. Evaluate if 100 is not equal to 4, then tell us that (100~=4).

if 100 ~= 4 then --the '~=' operator means 'not equal to'
    print("100 is not equal to 4")
end
100 is not equal to 4

If

The 'if' condition is the starting statement to all conditional blocks of code. Use the 'if' statement to execute a block of code if the inputted expression is true.

if 2 + 3 == 5 then print ("2+3==5") end
if 5 > 2 then print("5>2") end
if 2 < 5 then print("2<5") end
if 5 >= 5 then print("5>=5") end
if 5 <= 5 then print("5<=5") end
if 1 ~= 100 then print("1~=100") end

2+3==5 (because 5 is equal to 5)
5>2 (because 5 is greater than 2)
2<5 (because 2 is less than 5)
5>=5 (because 5 is greater than or equal to 5)
5<=5 (because 5 is less than or equal to 5)

1~=100 (because 1 is not equal to 100)

Else

In an 'if..else' block of code, the statements under the 'else' block will execute if the 'if' condition is false. Keep in mind that 'else' statements can only be used when there is an 'if' statement.

Let's see this as applied to scripting:

if 10 > 100 then 
    print("10 is greater than 100") 
else 
    print("10 is less than 100") 
end
10 is less than 100

Since 10>100 is false, the then statement won't execute, but the else block will.

Elseif

In an 'if..elseif block of code, the compiler will go from top to bottom, checking whether each, if any, of the conditions are true. If one of the conditions are true, the corresponding block of code will be executed.

"If it is raining, bring your umbrella. Otherwise, if (i.e., "elseif") it is sunny, bring your suntan lotion."

We can use the "elseif" statement. Let's try that in a script:

if 10 > 100 then 
    print("10 is greater than 100") 
elseif 10 > 50 then	
    print("10 is greater than 50") 
elseif 10 < 100 then	
    print("10 is less than 100") 
end
10 is less than 100

The only instance any of the conditions would be true is 10<100, which would result in the printing of "10 is less than 100".

Note: Blocks of 'if..elseif..else' code can be used.

Advanced Usage

To eliminate the need for long and winding if statements, there is another way. It has the following advantages:

  • Makes your script shorter
  • Increases the readability of you script
  • Is simpler to write

Here is an example:

local variable = 1
print(variable == 1 and "The variable is equal to one."
                     or "The variable is not equal to one")
variable = 23
print(variable == 1 and "1"
   or variable == 2 and "2"
   or variable == 3 and "3"
   or                   "Something else")

The variable is equal to one.

Something else

This method uses two operators: "and" and "or".

This could be compared to a if...then and a else statement.

Several elseif...then statements can be rowed up, like in the first example, where the local variable "variable" is compared with several questions and then,

if none of the statements are true, uses the else statement, if that is provided by you.