Tip:
Highlight text to annotate it
X
Hello, I'm Eli for edu4java and this is tutorial number 5
which will talk about the instruction or sentence "if",
logic expressions and logic operators
before starting with the tutorial
I would like to correct a mistake in the tutorial number 4
when I defined the name of the java package to organize classes
I made an error
Java packages
are named with the domain
but the other way round
First we put the domain and
then the name
ok
Continue...
As you can see
I've already changed it
and Eclipse has changed the content too
Perfect!
New-package
"com.edu4java.tutorial5
Remember that the package goes with lower case
New-class
"Tutorial5" remember that classes are written with upper case
Let's check "public static void main" so that it creates the method main so that we can run the program
Let's delete this code that was generated automatically
The first thing I would like to explain
is the concept of the "program flow control"
I hope you don't have this too... I don't think it's important
"Instruction 1"
As you can see I don't like to write a lot
"Instruction 1, 2, 3 and 4"
To run the program for the first time
You will have to do it from here
Right button- run as-java application
In the console we have the result
of this small program
1,2,3,4 ... we have already seen this but I do this so that you get an idea
The idea is the flow
The flow of a program is the order in which the instructions are executed
This first instruction is executed first and then this one and then the third and so on
As you can see this is correct. First this one was printed and the others
The instruction "if"...
Ask for help with ctrl+space
You can choose this one; If (condition)
or this one
if, else...Let's start with the if
if(condition)
This means that if
what is inside the condition is true whatever is inside will be run. Let's copy
the "instruction 2"
inside
Now let's write a condition
Let's write a condition which is going to be false
1>2
Here I am writing
that 1 is bigger than 2, so what do you think? is this true? no, this will be false because 1 is smaller than 2
This is false
Let's run it
and as you can see the instruction 1, 3 and 4 are printed but the instruction number 2 is not printed
If we wrote 3
and we run it
The instruction number 2 is printed
why? because 3 is bigger than 2 so
because this is true
this instruction will be ran
Here we have to
now the concept of true and false
This is an expression which will have as a result true or false
True and false is a type of data called boolean which we will see later on
Talking about the flow control... notice that we've changed the flow control
This instruction changes the order in which the instructions are executed. For example here we see
that the instruction 1,3 and 4
were executed but not the 2
If here we put a 3 again
All the instructions are executed. This condition changes the flow of the program
This is why this instructions are called flow control instructions
Do you remember that we had seen an else?
That we could choose and if or and if and else?
else...Let's put here the instruction number 3
ok else...
if this is true
we execute this and if not
we execute this
Let's see if it's true
1,2,4...
Instruction number 1 was executed. It evaluated the condition 3>2
Instruction 2 was printed
Instruction 3 was not printed because it was the "if not"
and it continues to 4
We can see that
it is another way
of changing the flow of the program
This instruction is going to be executed if this is false
If we write 1
we run; 1,3,4
1,3,4
Ok...
With this I think we can see what the instruction if does
Let's do something a bit more complicated and see "logic expressions"
so you don't get bored
Let's copy the code of the tutorial 4
and let's copy it in our program
If you remember
scanner was an object to which we could ask for texts
It asked to write something on the console. We wrote on the console "edu" for example and return
This recovered this "edu", it put it into "entrada" and after System.out showed "mostrar lo escaneado
and this one printed "edu" which was what we wrote in the first place
Let's change it a bit.
We are going to create a program which asks for the age
I wrote "edad" as an int type, why?
Because "age" is a number, it's not a String, before it was a word. Here we have an error; next reads words
But thankfully scanner has also a nextInt
which reads numbers
We could write "escanear"(scanner)
"edad"(age)
"mostrar la edad" (show age)
Here we write "age"
Let's run it
"Enter age"
Let's write ;11
Show age; 11
If we had a bar and we had slow employees we would like a program
to know if they can sell drinks to people or not They are going to ask the age using this program
if...
We are going to use the second type... if... else
if the age
is >18
I think this is the legal age to start drinking in most of the countries...
if(age>18)
then
"puede beber" (he can drink)
if it's not so
"he can't drink"
Let's run it
"scanner age"
Let's say he has 17
It says
"no puede beber" (he can't drink)
"mostrar la edad" 17(show age:17)
Let's do a trick here
so it's in the same line. I'm taking out the println
"System.out" has also a method called "print"
Let's run it again
age;21
"puede beber": (he can drink) and "mostrar edad" (show age) 21
Let's do this here too
Let's get rid of the println
age
11, he can't drink... ok!
Notice that this program is not correct
why?
scanner age: 18
"he can't drink" this is wrong. We had said that with 18 he could drink!
This is because 18 is not > than 18
We are talking of the people who are 18 or more
This is a logic operator
and we are going to change it for this one
>=
edad>=18
Let's run it again
18
now he can drink
Let's write again
17
he can't drink
This is quite intuitive
if this is true this happens, if it's false this happens
Here for the first time we are using
a variable in the if expression
This variable is an int type
and when we are using this operator
with another int returns
a result which is an integer
No, sorry, true or false...
Sorry!! the time which youtube gives for tutorials is finished. It will continue in Tutorial 5.1 java Logic Expressions and operators!