 Up until now, we haven't been able to test for conditions. We do the same instructions every time. The Java if statement lets us do things depending on whether some condition is true or false. Here's the flow chart. And here's the generic form. If some condition is true, we do the statements inside a block, otherwise we don't do the statements at all. That's a bit abstract. So let's write a program that asks the user for an integer. If the integer is even, it gives a message that says so. And if the integer is a multiple of 7, the program gives a message saying so. What happens if we enter 18 as our input? The first condition tests to see whether 18 mod 2, the remainder when you divide 18 by 2, equals 0. Notice that we have two equal signs here. We use one equal sign to mean a sign of value to a variable. We use two equal signs to ask a question. True or false, are these two things equal? In this case, the answer is true. 18 mod 2 is 0, so the program will print that 18 is even. Java then proceeds to ask another question. Is the number 18 mod 7 equal to 0? In this case, the answer is false, because 18 mod 7 is 4, and 4 is certainly not equal to 0. Because the condition is false, Java does not do the statement in the block and doesn't print that message. And when we run the program and enter 18, we get only the message that 18 is even. What would happen if we gave 21 as an input? Our first condition, is 21 mod 2 equal to 0? No, it isn't, so we won't print out that it's even. In the second condition, is 21 mod 7 equal to 0? Yes, 21 divided by 7 gives you a remainder of 0, so the program will print out that 21 is a multiple of 7. Let's try that and enter 21, and that's the message we get. What if we enter 28 for our number? The first condition will be true, 28 mod 2 equals 0, so the program will tell us that 28 is even. 28 mod 7 is also equal to 0, and the program will tell us that 28 is a multiple of 7. And sure enough, we get both messages. What will happen with 19 as an input? 19 mod 2 is not equal to 0, so we won't get the first message. 19 mod 7 is not equal to 0, so we won't get the second message. In this case, when we enter 19, we'll get no output at all. What you've just seen is a one-way or unary if statement. There's another kind of if statement that's also very common, where you ask a true or false question and do one thing if the answer is true, and something different if the answer is false. Here's the generic model in Java. If some condition is true, we do one block of statements. Otherwise, the condition is false, and we do a different block of statements. Let's put that into our age in days program. As it currently stands, if you run the program and enter a negative number or zero, the program will calculate an age, even though the input really doesn't make any sense. Let's use an if else to fix that. Once we get the number of years, we're going to ask if the years is greater than zero. If that's true, then we want to do both of these statements. We'll indent them in the block and put in a closing brace. Now I need to specify what to do if my condition, years greater than zero, is false. That's the else block, and I will print a message that says age must be greater than zero. And again, that block will be inside of braces. Let's compile this and run it. If I say 30 years old, everything works great. If I say negative five, I get my error message. That's how you use if and if else statements in Java.