Your web browser does not support Java. Go to http://www.java.com to get Java.

Person asks computer if it wants to go to a movie. Computer replies that it doesn't know.

A program lets a computer follow instructions, but so far the computer doesn't know how to make decisions.

Picture of 3 identical dogs and an elephant. "One of these things is not like the other"

With the "if" instruction, a computer can compare two things and make a decision. One way it can compare things is to check if two things are the same.

a = input("Is it raining?");
if (a == "yes") { show("You should bring an umbrella."); }

with arrows pointing out different parts

You do this by typing "if" with an opening bracket after it. Then you write the first thing you want the computer to look at. Then you type two equals signs and then the second thing you want the computer to look at. After that, you type a closing bracket, and then an opening "curly brace". You then give the computer some instructions that the computer should follow if the two things ARE the same. Then you type closing curly brace.

a = input("Is it raining?");
if (a == "yes") { show("You should bring an umbrella."); }
Small windows tracing execution with different possibilities with yes and no

In this little program. If you answer yes, the computer will tell you to bring an umbrella. If you type something that isn't exactly the same as "yes" then the computer won't do anything. You can type "no" or "nope" and the computer won't do anything. If you use different letters like "yEs", it's not exactly the same as "yes" and the computer will also not do anything.

a = input("Is it cloudy?");
if (a == "yes") { show("You should bring an umbrella."); }
if (a != "yes") { show("You should wear sunscreen."); }
with arrows pointing out different parts

An exclamation mark followed by an equals sign means "not equal." In the program above, it first checks to see if you typed "yes." If so, it tells you to bring an umbrella. Then it checks to see if you typed something other than "yes." If so, it will tell you to wear sunscreen instead.

a = input("Is it cloudy?");
if (a == "yes") 
{ 
  show("You should bring an umbrella"); 
  show("or a raincoat."); 
}
else 
{ 
  show("You should wear sunscreen."); 
}
with arrows pointing out different parts

Instead of having two different if instructions, you can use the else instruction. In the above program, if a is "yes," then it does one thing. If a isn't "yes" then it does the instructions that come after the else. Also notice that you can have more than one instruction inside of the curly braces. And you can put things on different lines to make things easier to read.

a = input("What is the weather?");
if (a == "sunny") 
{ 
  show("You should wear sunscreen"); 
}
else if (a == "rainy")
{ 
  show("You should bring an umbrella"); 
} 
else if (a == "snowy")
{
  show("You should dress warmly");
}
else
{
  show("I don't understand.");
}

with arrows pointing out different parts

This is useful because you can have lots of if instructions all put together. Here, if you type in "sunny," "rainy," or "snowy" the computer will tell you what to wear. If you type something else, the computer will go right to the end and tell you that it does not understand what you typed.

Diagram showing colour mixing

On the left, you need to write the program for a paint-mixing machine. It uses red, blue, and yellow paint to make other types of paint.

Diagram showing different machine parts.
with 
c = machine.order();
show("Making " + c + " paint.");
machine.conveyor();
if (c == "red") {red.spray();}
machine.conveyor();

On the right of the machine is an order for a type of paint. You have to get the machine to make that type of paint. You can use machine.order() to give you a string with the type of paint you need to make. Then, you must use machine.conveyor(), to move the paint bucket under the sprays on the conveyor belt. red.spray(), blue.spray(), and yellow.spray() will shoot paint into the bucket to make the right type of paint. Then machine.conveyor() again will move the paint away, and then the paint will be checked to see if it's correct. The order changes each time you run your program. At the top of the exercise area, you can change what sort of orders you will get. Right now, you will get orders for red paint, but you can change it.

Previous | Next

Programming Basics

E-mail | License | Privacy Policy