Short Hand If Else Statement

Submitted by: 
Language: 

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;

  1. boolean klo = true;
  2. System.out.println(klo ? "Hey" : "Hi");

Which gives the output:

Hey

And here is an example of setting the value of an integer in relative to a boolean variable ("klo"):

  1. boolean klo = true;
  2. int lok = klo ? 1 : 2;
  3. System.out.println(lok);

Which gives the output:

1

Finished!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.