The Bear Minimum - Decision Making, The If Statement

What would you do if somebody asked you the question:
Are you ready for some football?!

If you like football, I'm sure you would say something like "I can't wait, I looOOove the {{insert Team Name here}}!". Otherwise you'd say "Nope" to some degree.

An expression like this can easily be represented in code. A super simple program may be represented as:

Prompt a message "Are you ready for some football?"
If the user types in "Yes" the display "I can't wait!"
otherwise display "No way!"
The general form of making a decision in a programming language is with the if-statement. A single if-statement performs a task similarly to if something is true then do this. Additionally, the if-statement has a best friend -- the else keyword. In most languages you can follow the if-statement with an else to do if something is true then do this else do that.

Java Examples:
boolean cond = true;
if (cond) System.out.println("The condition is true!");

boolean conditionTwo = false;
if (conditionTwo) System.out.println("The second condition is true!");
else System.out.println("The second condition is false!");
The above will print out "The condition is true!" and "The second condition is false!" When you've mastered the art of using if-else, you can also pick up another trick -- the else-if-statement. This allows you to chain together multiple if statements where only one of the statements will ever be executed.

Here's a simple program named This program attempts to compare an integer with various conditions.
class DecisionMaker {
public static void main(String args[]) {
int i = 0;
if (i == 1) System.out.println("one!");
else if (i == 2) System.out.println("two!");
else if (i == 3) System.out.println("three!");
else System.out.println("The variable i is not one, two, or three!");
The output will be "The variable i is not one, two, or three!"

In a file named DecisionMaker.html
<script type="text/javascript">
i = 0
if (i == 1) document.write("one!")
else if (i == 2) document.write("two!")
else if (i == 3) document.write("three!")
else document.write("The variable i is not one, two, or three!")
The output will be "The variable i is not one, two, or three!"

In a file named
i = 0
if i == 1: print "one!"
elif i == 2: print "two!"
elif i == 3: print "three!"
else: print "The variable i is not one, two, or three!"
The output will be "The variable i is not one, two, or three!"