Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
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.
Learn more
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
[MUSIC]
0:00
[SOUND] Welcome back!
0:04
I have solicited some feedback and
some example stories for
0:05
us to ensure that our program is
working the way that we wanted to.
0:08
We will use this information to
iterate and improve our program.
0:12
I'm excited [LAUGH] to look through these.
0:16
Let's look.
Here's the first one here.
0:18
It says, Craig is a big dork.
0:19
Nice.
0:23
While true, it's not very nice.
0:24
Okay, let's look at this other one here.
0:26
[LAUGH] Of course it starts the same way.
0:28
Craig is a, what does that even mean?
0:31
[LAUGH] Well, one thing is certain here.
0:38
We are going to need to put some
sort of age restriction on this.
0:42
After just reviewing those first
two responses, I am certain
0:45
that we better limit this from getting in
the hands of someone under the age of 13.
0:48
So, let's write some code that
stops the program from running,
0:52
if the user doesn't meet
our age requirements.
0:56
In order to do this we're gonna
need to pick up a few new skills.
0:59
To put this age restriction in place,
we are going to need to work with numbers.
1:03
Now, Java has a data type that deals with
whole numbers, and it's called integer.
1:07
We'll store the user's age in
a variable of the integer data type.
1:12
Then we're going to use what is
known as a conditional statement.
1:17
Conditionals help us to make choices
based on information we know.
1:21
We make decisions like these all the time.
1:25
A clear example is to take a look at
the conditions that are made by users of
1:28
an online dating program.
1:31
I will go out with that guy, if he
doesn't smoke, doesn't have kids, and
1:33
likes to go to concerts.
1:38
Or, I will date anyone who is
older than 28 and less than 50.
1:39
Only after these conditions
are met is contact made.
1:44
So the conditional we are specifically
looking for is, is the user older than 13?
1:47
The answer that we get back is
either yes or no, true or false.
1:53
In Java this is a data type
referred to as a boolean.
1:59
And last but not least, we'll learn
how to immediately exit the program so
2:03
that it doesn't continue running
after we've decided they aren't
2:07
old enough to use the program.
2:10
Are you ready?
2:12
Let's go put a PG13
warning on this program.
2:13
All right, so first things first,
we gonna make a new variable called age.
2:16
So remember,
we're gonna make an integer data type.
2:21
So the way we do it is we start by
stating the data type name, which is int.
2:23
And then we type the name of the variable,
which is age.
2:28
And let's go ahead and
set ourselves up to fail.
2:31
And let's set it = 12.
2:33
And then we'll press the semi-colon of
course to end the statement, great.
2:35
Okay, so now let's go ahead and
add our conditional.
2:40
Now remember, we're gonna check and
see if the person is younger than 13.
2:43
In other words,
if the age is less than 13, exit.
2:46
Okay, let's write that.
2:51
So, first we'll write the keyword if.
2:52
And then we'll add an open parentheses.
2:56
And here's where we write our expression.
2:58
And that's the condition that must be
true for the code following it to run.
2:59
So, which we said earlier that is age.
3:04
And then we're gonna do < 13.
3:06
And we're gonna close the parentheses,
and we're gonna give a space and
3:10
a curly brace.
3:14
And a curly brace opens
up a block of code.
3:15
And so let's just go ahead really
quick and add a comment here.
3:18
And this is where we're
gonna insert our exit code.
3:21
And then let's close that curly brace.
3:24
You could have many lines of
code in this code block here.
3:27
So let's go ahead and
3:31
let's add a new line that says that
they're not allowed to use this.
3:31
So, we'll say, Sorry you must be
at least 13 to use this program.
3:37
We're gonna go ahead and
do a new line sequence in the quotes, and
3:45
the semi-colon.
3:50
Okay, now what we'll do is we'll tell the
program that it's time to quit or exit.
3:52
So far the only object that we've
really used is called console, but
3:57
we're about to see a new one.
4:01
And that's the global system object.
4:02
This object has a method
on it which is called exit.
4:05
Which as you might guessed,
causes the program to exit.
4:07
The exit method takes an argument for
the status code,
4:11
which zero means that the system exited
intentionally, we had control of it.
4:14
Any other non-zero value represents a
status code that is used to state that it
4:19
exited abnormally.
4:23
So let's go ahead and write that out.
4:25
So again,
that's System with a big S dot exit,
4:26
and then the status code of 0, great.
4:31
I'm gonna save that.
4:34
And let's give that a go and
see if it works.
4:35
Let's run that, and boom.
4:41
Now because we hard coded, again,
we hard coded the edge up here at 12.
4:44
It is always gonna run into this if block.
4:48
And these two statements are gonna run.
4:50
And it's gonna exit and
the rest of the code won't go.
4:51
So we won't get prompted for
the rest of these.
4:53
So it's working, great!
4:56
Wow, lots of good stuff!
4:58
We use a new data type, we learned
about if statements and code blocks.
5:00
And learned how to safely
exit from our program.
5:04
One thing that I wanted to point out
about the int data type was that it was
5:07
all lowercase,
unlike the other data type we had seen.
5:10
Remember that was the string
with a capital S.
5:14
When a data type is lowercase it is
a special kind of data type called
5:17
a primitive.
5:21
int is one of eight primitive data types
that come out of the box with Java.
5:22
Basically, primitives have
special characteristics
5:27
about the kind of data they can store.
5:30
And that's about it.
5:32
They don't give you methods or properties.
5:33
They're just data.
5:35
How primitive, right?
5:37
For now, I just want you to be
aware of the term primitive
5:39
in case you run into it.
5:42
So, int is a primitive data type,
and string and
5:44
all other non-primitive types
are called object data types.
5:47
Check the teachers notes for
more information.
5:51
[SOUND] Now our application is
safely blocked from pre-teens.
5:53
Well, actually, at the moment it's
pretty much blocked permanently, right?
5:57
We need to make that age variable
dynamic by prompting our user.
6:01
But before we get there,
6:05
let's do a quick exercise to make sure
all this new information is making sense.
6:06
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