Iffy Programming |
This lesson is going to be all about
if. Withoutif, all our programs would be very boring, as they could only do one thing. But by usingif, you can make very complex and interesting programs which do all sorts of things. Have a copy of Lecture Note 7, Section 7.2 handy. If you do, you'll notice that our next piece of jargon is conditional statements (like our new friendif).Anyway, forget all that waffle, let's do some more Java. Delete all the rubbish from the last lesson so that we have our very first program again.
class Hello { public static void main(String[] args) { System.out.println("Hello, world!"); } }Don't turn all nostalgic on me now. We've got work to do. Change your program to look like the following:
class Hello { public static void main(String[] args) { if (2 == 3) System.out.println("This message shouldn't be printed!"); } }You'll need to indent the
System.out.println("Hello, world!");line by clicking just in front of it and pressing the Tab key once (the one just above Caps Lock). Although you don't strictly have to do this, it's good practice and a useful habit to get into before you start writing 300 line programs. Trust me. Notice one thing - theifline doesn't have a semicolon at the end.
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello [nurmes]btg:Sure enough, the message wasn't printed (well unless you typed my program in wrongly). Cool! Now let's try changing a couple of things.
class Hello { public static void main(String[] args) { if (3 == 3) System.out.println("This message should be printed!"); } }
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello This message should be printed! [nurmes]btg:Sure enough, that one was! Now you should see how
ifworks. Let's explain it in detail so you understand properly. You writeif (and then you write a "true/false question" as we referred to them earlier, for example2 == 3or3 == 3. Then we write)to show that our question is finished. What happens is thatiflooks at our question and plays the game - "Is it true, or false? True, or false?". Figuring out whether a question like this is true or false is known as evaluating it.Once
ifhas done this, it will have an answer (either true for the example3 == 3, or false for the example2 == 3). If the answer is true, then the statement below theifhappens and a message is printed out. If the answer is false, then the statement doesn't happen and no message is printed. We indent the statementSystem.out.println("This message should be printed!");to show that it only happens if theiftells it to (indent means put it further across to the right using the Tab key).Read those two paragraphs again, just to get it into your head. Before we go any further I should teach you one more bit of jargon (sorry!). When I say that the statement happens, what I should really say is that the statement is executed. This doesn't mean it's hung, drawn and quartered or anything weird like that, it means that we do what the statement says. Personally I think I prefer "happens", but it's not real computer jargon so I won't use it again.
Great, so now let's add another line (sorry, statement) to our program:
class Hello { public static void main(String[] args) { if (3 == 3) System.out.println("This message should be printed!"); System.out.println("I wonder if this message is printed too..."); } }
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello This message should be printed! I wonder if this message is printed too... [nurmes]btg:Ooh, it was! Let's try the other way around:
class Hello { public static void main(String[] args) { if (2 == 3) System.out.println("This message shouldn't be printed!"); System.out.println("This message shouldn't be printed either..."); } }Don't forget to change
3 == 3back to2 == 3.
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello This message shouldn't be printed either... [nurmes]btg:Haha! I bet you weren't expecting that! So what has just happened? The
ifonly affects the first statement immediately following it. Not the second, or the third, or anything. So in fact the way we indented our program was a bit misleading. We made it look like the second message ("This message shouldn't be printed either...") would depend on theif. In fact it didn't at all. So let's just change the layout of the program ever so slightly, to make it clear what actually happens:
class Hello { public static void main(String[] args) { if (2 == 3) System.out.println("This message shouldn't be printed!"); System.out.println("This message is printed, actually!"); } }
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello This message is printed, actually! [nurmes]btg:Now both the program itself and what it prints out make a whole load more sense. But suppose we want to do two things inside the
if? Well, although we're only allowed one statement after theif, we can put more than one if we put them in a block. You might remember that right at the beginning I mentioned blocks. They are basically a load of statements (well in fact, zero or more statements) inbetween a{and a}. In fact it is even more general than that - we can use a block anywhere we can use a single statement. Well don't worry, I'll show you what I mean. Let's put a block around two of our statements:
class Hello { public static void main(String[] args) { if (2 == 3) { System.out.println("This message shouldn't be printed!"); System.out.println("This message is printed, actually!"); } } }Make sure that you type both the
{and the}.
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello [nurmes]btg:Hmm... fascinating, nothing is printed out. It's elementary, my dear Watson, two does not equal three. So the block of statements after the
ifis never executed. If we simply make the question inside theif"does three equal three?" then I believe we may catch this villain!
class Hello { public static void main(String[] args) { if (3 == 3) { System.out.println("This message should be printed!"); System.out.println("This message is printed, actually!"); } } }
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello This message should be printed! This message is printed, actually! [nurmes]btg:Good, so we can see that now, both statements depend on whether the "question" inside the
ifis true or false. Just what we wanted. We could add more than two statements if we wanted, so long as they were inside the block. Before going on, we should tidy up our indenting. Both the statements depend on theifnow, so they should both be indented to start at the same place, like this:
class Hello { public static void main(String[] args) { if (3 == 3) { System.out.println("This message should be printed!"); System.out.println("This message is printed, actually!"); } } }We haven't changed any of the program, we've just indented it so that it makes more sense to us. To the computer, it doesn't matter how we use spaces and tabs, or new lines (these are called whitespace) - the program will behave just the same. But it makes things much clearer to us humans. Let's take an extreme example:
class Hello{public static void main(String[] args){if(3==3){System.out.println("This message should be printed!");System.out.println("This message is printed, actually!");}}}This is the same program with all the unnecessary whitespace taken out - that's spaces, tabs and new lines. The spaces that are left are the bare minimum for it to work. Believe it or not this is exactly the same program! I'll prove it:
[nurmes]btg: javac Hello.java [nurmes]btg: java Hello This message should be printed! This message is printed, actually! [nurmes]btg:Now just to make my point absolutely clear, I'm going to move one of the
}s. Watch:
class Hello{public static void main(String[] args){if(3==3){System.out.println("This message should be printed!");}System.out.println("This message is printed, actually!");}}I've just altered the structure of the program. But even with the red highlight, it's not very clear, is it? Perhaps it would be better if I had written:
class Hello { public static void main(String[] args) { if (3 == 3) { System.out.println("This message should be printed!"); } System.out.println("This message is printed, actually!"); } }Our next lessson teaches us about
if's big sister,else.... or else!
Too patronising? Too complex? Typing error? Offended by traffic cones?
Got a question or something I should add? Send an email to ben_golding@yahoo.co.uk !
visits to this site
The contents of this site are copyright of Ben Golding