An if statement is a conditional statement based on implication. The value of a variable will determine the code that executes.
You are making a battle card game. Each player has a set of skills. Each skill is equal to an integer 1-20 (based on D&D dice).
If a character's spirituality skill minus brutality skill is greater than 10 that character will have the power to heal. To calculate the number of health points a character can heal each round:
(spirituality - brutality) / 5
Write code to determine a character's healing points.
Mouse Hover over the ➼ blue text in the description to highlight the code.
extends Node
# Declare Variables
var spirituality: int = 18
var brutality: int = 5
var baseScore = spirituality - brutality
var healing: int = 0
func _ready():
# If the condition is satisfied, execute this code
if baseScore > 10:
healing += (baseScore / 5)
# print healing points
print (healing)
➼ spirituality equals 18 (an integer)
➼ brutaility quals 5 (an integer)
➼ baseScore equals spiritality minus brutality - This number determins if a character has healing
➼ healing is initalized at 0 - If not other healing code executes, the character has no healing
➼ if the baseScore is greater than 10 - This is the condition that determines if more healing code executes
➼ Add (baseScore / 5) to healing - This only happens if the condition is satisfied: baseScore > 10
➼ Output healing points
** Debug Process Started ** OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2) 2 ** Debug Process Stopped **
In the example above there are two cases: baseScore is greater than 10 or baseScore is not greater than 10. Sometimes there are more than 2 cases. That scenario calls for
Let's look at an example. You are writing code to determine if a number is positive, negative, or zero.
# Declare num
var num = 3 * 2
func _ready():
# Conditional Code
if num < 0:
print ("negative")
elif num == 0:
print ("zero")
elif num > 0:
print ("positive")
else: print ("Error")
➼ if num < 0 - The code will output "negative"
➼ if num == 0 - The code will output "zero"
➼ if num > 0 - The code will output "positive"
➼ if none of the specified conditions are met - The code will output "Error". Even if you do not think there is a possibility outside of the conditions you have named, you should still have an else statement. You never know what a user will break. A good programmer will make every effort to prevent an application from breaking.
** Debug Process Started ** OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2) positive ** Debug Process Stopped **
Sometimes more than one condition has to be satisfied for a block of code to execute. We code this with logical operators.
Billy is ordering t-shirts. Plain youth shirts cost $10. Graphical youth shirts cost $14. Plain adult shirts cost $15. Graphical adult shirts cost $20. Write the code to output the price of a shirt.
# Declare Variables
var adult: bool = true
var graphics: bool = false
func _ready():
# Conditional Code
if adult && graphics:
print ("$20")
elif adult && !graphics:
print ("$15")
elif !adult && graphics:
print ("$14")
elif !adult && !graphics:
print ("$10")
else: print ("error")
➼ If adult and graphics are true - Output "$20"
➼ If adult is true and graphics is false - Output "$15"
➼ If adult is false and graphics is true - Output "$14"
➼ If adult and graphics are false - Output "$10"
➼ If none of the named conditions are satisfied - Output "error"
** Debug Process Started ** OpenGL ES 2.0 Renderer: Mesa DRI Intel(R) UHD Graphics 620 (Kabylake GT2) $15 ** Debug Process Stopped **