Short Hand If Else Statement
Submitted by GeePee on Sunday, June 14, 2015 - 23:49.
Introduction:
This tutorial page will talk about the short hand version of an if else statement.
Important:
I highly recommend you read through the If Else tutorial page before reading this tutorial.
How is the short hand if else statement structured?
The short hand version of an if else statement is used through a couple of symbols, here is the layout of the statement;
Action (setting a variable, function or method, etc.) - condition - "?" - What to do if the condition is true - ":" - What to do if the condition is false.
When is a short hand if else statement used?
This can be used for simple if else statements which only perform one line of script for each condition while still having the following benefits;
- Saves space
- Can make the script look more organised
- Makes the script easier to read
Example:
Here is an example of giving the output of "Hey" if the variable is true, and "Hi" if the variable is false;
Which gives the output:
Which gives the output:
- boolean klo = true;
Hey
And here is an example of setting the value of an integer in relative to a boolean variable ("klo"):
- boolean klo = true;
- int lok = klo ? 1 : 2;
1
Finished!Add new comment
- 93 views