Having learned the logical AND, OR, and NOT in a previous article, let’s move on to something only slightly more complex:
Set up a class (and your main method) called ConditionalPractice.
In the main method, declare a Boolean called isMonday. Set
it to true, like this:
boolean isMonday = true;
In plain English, what we want to do is System.out.println()
something if today is Monday.
What you’re about to see is something called pseudocode for the first time; it
isn’t Java, so it won’t run if you try to run it in a class, but it gives a
high-level, close-to-plain-English view of what code does in a way that what
language choose to write in doesn’t matter. As we get more advanced, you’ll start
seeing pseudocode in these articles a lot more frequently.
If today is Monday
Print “Today is Monday”
In Java, we have if-statements.
Their syntax is the following
if(a condition){
what to do if that condition is
true;
}
That condition must always be evaluable to a boolean. Refer back to our Math lessons—“is
50 % 2 equal to 0?” (written as 50 % 2 == 0) is an example of such an
expression that isn’t immediately a boolean like isMonday, but can become one. Both
true-false questions and immediate booleans can go in the condition spot in the
if.
You are not always obligated to have curly braces around ifs,
but I always use them as a matter of personal preference. If your list of
things to do inside a block only contains one thing, you can omit the braces. But
if your list of thgs to do includes more than one statement, you must have them.
if(todayIsMonday)
System.out.println(“Oh no!”);
works just as well (even though I don’t like it as much) as
if(todayIsMonday){
System.out.println(“Oh no!”);
}
BUT:
(A)
if(todayIsMonday)
System.out.println(“Oh no!”);
System.out.println(“I don’t like Mondays”);
will produce a different—perhaps unintended result than
(B)
if(todayIsMonday){
System.out.println(“Oh no!”);
System.out.println(“I don’t like Mondays”);
}
In Java, if you omit braces from if-statements, Java assumes you only want the
first statement inside the if bound by its condition.
So A and B are different. Can you figure out why? Pause for
a second to think, and then keep reading.
A prints “Oh no!” only if today is in fact Monday, but prints “I don’t like
Mondays” regardless.
B prints BOTH “Oh no!” and “I don’t like Mondays” only if today is in fact Monday,
and nothing if it isn’t.
It's perfectly natural to want to do one of two other things:
·
If today is any other day of the week but
Monday, print something else
· If today is Tuesday, print X; if today is Wednesday, print Y; if today is Thursday, print Z; and so on
For this, we need an extra bit of syntax: the else clause.
Else clauses are always paired with if-statements, and always come after them. Else
clauses have the same brace rules as if-statements, but, again, I like to
always use them even when not technically required by the compiler.
To write the first bullet point, we need a straight-up else statement without
any frills.
in pseudocode, it looks like:
If a number is even:
Print “YAY!”
Else
Print “BOO!”
In Java, that would look like
if(){
} else{
}
and the code to be executed in each situation would go inside the relevant
curly braces.
So back to our other-days example.
·
If today is Monday, print “I don’t like Mondays!”
·
Otherwise, for any other day of the week, print “Today
is OK, I guess”
if(todayIsMonday){
System.out.println(“I don’t like
Mondays!”);
} else{
System.out.println(“Today is OK, I
guess”);
}
Now, what if you wanted to get a little more specific with
how you handled branching, and have a few more cases? Perhaps “I really enjoy
the weekend!!” gets printed if today is Saturday or Sunday?
in that case, let’s assume that all these variables are properly created and
initialized (I won’t show that here, but you’ll need to make sure that’s true
if you’re following along in your IDE window):
First, in pseudocode:
If today is Monday
Print “I don’t like
Mondays!”
If we know today isn’t Monday, but we still check if today is the weekend and find
out it is
Print “I really enjoy the
weekend!!”
In every other situation, when today isn’t Monday or the weekend
Print “Today is OK, I guess”
now in Java (remember to declare and initialize everything properly or you’ll
get an error!)
if(todayIsMonday){
System.out.println(“I don’t like
Mondays!”);
}else if (todayIsWeekend){
System.out.println(“I really enjoy
the weekend!!”);
}else{
System.out.println(“Today is OK, I
guess”);
}
You could, in theory, have as many different else-if cases in between as you
wanted. Make that an exercise. Go through and set one day a week to be true at
a time, and run the code many times, setting a different message to be true for
each day of the week
Remember, it would look something like:
if(todayIsMonday){
System.out.println(“I don’t like
Mondays!”);
} else if (todayIsTuesday){
System.out.println(“I go to the
store on Tuesdays”);
} else if (todayIsWednesday){
System.out.println(“I go to the park
on Wednesdays”);
} else if (todayIsWeekend){
System.out.println(“I really enjoy
the weekend!!”);
}else{
System.out.println(“Today is OK, I
guess”);
}
of course, you could do something purely mathematical—
Like:
·
Start with 5040
- Using % to check, if x (5040) is a multiple of 2, print “2!!”
- Else if x is a multiple of 3, print “3!!”
- Else if x is a multiple of 4, print “4!!”
- Else if x is a multiple of 5, print “5!!”
- Else if x is a multiple of 6, print “6!!”
- Else if x is a multiple of 7, print “7!!”
- Else if x is a multiple of 8, print “8!!”
- Else if x is a multiple of 9, print “9!!”
- Else if x is a multiple of 10, print “10!!”
- Else—that is, only as a last resort, if nothing else triggers—print “prime??”
Be careful—I’ve set a trap here! This is like the days of
the week example—so only the first true thing triggers (or the else), not
everything that is true!
To make everything that is true trigger, turn all else-ifs into regular ifs.
(Ifs can stand alone without an else, but the reverse is not true.)
No comments:
Post a Comment