Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Java Basics!
You have completed Java Basics!
Preview
In this video, we receive feedback on our application from Treehouse teachers. We will learn how to use the conditional statement "if" to exit our program.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Welcome back.
0:05
I've gathered some feedback
0:06
and example stories to check
if our program is working as we intended.
0:07
We'll use this information
to improve our program.
0:11
I'm excited to look through these,
so let's check them out.
0:14
Okay, here's the first story.
0:17
Travis is a big dork.
0:18
While true, not very nice.
0:22
All right, let's look at the next one.
0:25
This one starts the same way.
0:27
Travis is a...
0:28
What?
0:31
What even is that?
... OH!
0:32
Well, one thing's for certain.
0:36
We're going to want some kind of age
restriction on this thing.
0:39
After seeing those first two stories,
I'm positive that we don't want
0:42
this getting in the hands of anyone
under the age of 13.
0:46
So let's write some code
to stop the program if the user doesn't
0:49
meet our age requirement.
0:52
To do this, we'll learn a few new things.
0:54
First, we need to work with numbers.
0:57
Java has a data type
for working with whole numbers
0:59
called integer, or int for short.
1:01
We'll store the user's age
in a variable of the integer data type.
1:05
Then we're
1:10
going to use what is known
as a conditional statement.
1:10
Conditionals let us make decisions
based on information.
1:13
We do this all the time in real life.
1:17
A clear
example is looking at the conditions
1:20
made by users of an online
dating platform.
1:22
I'll go out with someone
only if they don't smoke,
1:26
don't have kids, and going to concerts.
1:29
Or I'll date anyone
between 28 and 50 years old.
1:32
Only after these conditions are met
is contact made.
1:36
Our condition here is simple,
is the user older than 13?
1:40
The answer is either
yes or no, true or false.
1:45
In programming,
that's called a Boolean value.
1:48
If the user is too young,
1:51
we'll exit the program immediately
so no more code runs.
1:53
Ready?
1:56
Let's go add a
PG-13 warning on our program.
1:57
Okay, first, let's
2:01
create a new variable called age.
2:02
Since it's a whole number,
we want to use that integer data type.
2:05
Here's how we declare it.
2:09
First, before, we state the data type,
which is int in this case.
2:10
Then we give it a name. We'll call it age.
2:16
And let's set ourselves up to test
failing the check first.
2:18
Let's set it equal to 12
and end with a semicolon, of course.
2:22
Okay, let's add
our conditional statement now.
2:26
And remember, we're wanting to check
if the user is younger than 13.
2:29
So if age is less than 13, exit.
2:33
Let's write that out.
2:36
So first we write the keyword if.
2:38
Then we put in a pair of parentheses, and
in here is where we write our expression,
2:40
and that's the condition that must be true
for the code following it to run.
2:45
So we'll say age, then the less
than sign, the angle bracket,
2:50
then 13.
2:55
This reads as if age is less than 13.
2:57
Okay, next we give it a space
and put in a curly brace,
3:01
and this opens a block of code.
3:05
So real quick, I'm going
to just put in a comment here
3:07
to insert exit code.
3:10
And then on the next line,
3:14
we close off this code
block with the other curly brace.
3:16
We can add many lines of code
in this block here.
3:19
So let's give them our message
and say console.printf.
3:22
Sorry, you must be at least
3:29
13 to use this program.
3:31
And let's put in a new line there.
3:37
Now we'll tell the program
that it's time to quit, or exit.
3:41
So far, we've only really used one object,
and that's console,
3:45
but we're about to use another,
and that's the global system object.
3:49
This system object has a method on it
named exit,
3:53
which, as you might have guessed, causes
the program to exit.
3:56
Now the exit method takes an argument
for the status code.
4:00
We want to provide it zero,
which means that
4:04
the program exited intentionally,
we had control of it.
4:07
Any non-zero status
lets it know that it exited abnormally.
4:11
So let's write that out.
4:15
That's system,
uppercase S there, dot exit.
4:17
It's a method that we're calling,
4:22
so we put our parentheses and a semicolon.
4:23
Then, like the methods we've played with,
4:27
we provide our argument
in our parentheses.
4:29
Zero. Awesome.
4:32
This means, if age is less than 13,
print a message telling the user
4:34
they can't use the program,
then stop the program immediately.
4:39
Let save our file and test it out.
4:43
Nice! So because age is hardcoded to 12
it's always going to run into this
4:51
if statement and it will always prove
false, preventing any further code
4:56
below from running.
So our age check is working.
5:00
Great!
5:04
Wow! Lots of good stuff.
5:06
We used a new data type, integer,
for the age variable.
5:08
We learned all about booleans for the true
or false condition.
5:11
we learned how to write
a conditional statement
5:15
and use the system object
to terminate the program.
5:17
One thing I wanted to point out
is that the int data type
5:21
that we used was all lowercased,
unlike the other data type we've seen.
5:24
Remember,
that was string with a capital S.
5:28
When a
5:32
type is all lowercase, it's a special type
known as a primitive.
5:32
Int is one of the eight primitive data
types that come out of the box with Java.
5:36
Basically, primitives
5:41
have special characteristics
about the kind of data they can store,
5:42
and that's about it.
5:46
They don't give you methods or properties.
5:47
They're just data.
5:49
How primitive, right?
5:50
So for now, I just wanted you
5:53
to be aware of the term primitive
in case you run into it.
5:55
So int is a primitive data
type, and string and all other
5:58
non-primitive types are called
object data types.
6:02
Check the teacher's notes
for more information on that.
6:05
Alright, so now our program is safely
blocked from those pesky preteens.
6:08
Well, actually,
6:13
it's kinda blocked from everyone
since we've hardcoded that age value in.
6:15
We need to make that variable dynamic
by prompting the user for their age.
6:19
But before we do that,
let's do a quick exercise to make sure all
6:24
this new info is sticking.
6:27
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up