Conditions
Conditions are used to check if a certain condition is met and execute some actions based on the result. They are defined using the if
keyword followed by a condition and a block of code enclosed in curly braces. For example:
if (score > 100) {
// Do something
}
In this example, the code inside the block will only be executed if the condition score > 100
is true.
Branching
You can use if
, else if
, and else
to create branching logic based on multiple conditions. For example:
if (score > 100) {
// Do something
} else if (score > 50) {
// Do something else
} else {
// Do something different
}
In this example, the first block will be executed if score > 100
, the second block will be executed if score > 50
but not score > 100
, and the third block will be executed if neither condition is true.