Hey there.
Conditional statements are used in programming to control their program floor in Java language as in
other programming languages.
There is an if statement this operator is used to check a condition if the condition is true then the
code that is in the body or the operator is executed.
If it is false then this code is not executed.
Let's look at an example.
For example the variable contains the age of the user.
And we need to do the following.
If the age of the user is more than 16 then we display access is allowed.
Let's remove the template code and create their user age variable
set it to 20 and write next code.
If user age is more than 16
Max open the kernel brace
and write system out brain to land.
Access permitted
right.
Semicolon
we will remove the line too and try to run the program.
Huh.
An error is displayed here that it is impossible to find the symbol.
I wrote their name or their variable incorrectly made a mistake in the name
let's fix it and Iran again.
So as you can see the text access permitted is displayed.
Now let's change the value of the user age to 15.
That is less than 16 around the program.
And as you see nothing happens.
As I said the cord in the body of the if statement is caused between two curly braces he's executed
only if their condition placed in the pants.
This is true.
That is when the user age variable is greater than 16.
This part of the code is executed and the text is displayed.
In this case the user age variable is 15.
If we substitute 15 for user age then 15 is greater than 16.
This is false.
So this piece of code does not execute.
But let's say we want to perform some action if the condition is false.
Else operator is used for this.
It is used with the if statement.
Well let's print for example the text.
Access is denied.
If the condition is false.
Right here.
Else open the color braces again and let's copy this text here paste it here and right here.
Denied Access denied.
So now we have the variable user age equal to 15.
That is the condition is false 15 is more than 16.
This is a lie.
Therefore this text will be displayed which is in the body of the else block.
As you can see everything worked correctly.
Let's say now that we want to check one more condition for example if the user age variable is less
than y in this case let's print you are too small to do this use there.
Else if statement here after all else we will write.
If and the following condition here I will write.
User age less than y
then we will write you two small
it's wrong and nothing happened since non or the conditions is met that is here the condition user age
is greater than 16 user age is 15 therefore it is false here the condition user age is less than five
but 15 not less than five.
Therefore it is also not satisfied.
And if we put here for example one and run the program as you can see the text that is in this block
of code is displayed because their condition worked.
Let's suppose we want to display access permitted text if user age is greater than or equal to 16.
What should we do.
In this case for example if we put here 16 and run the program then nothing is displayed since 16 is
not more than 16 for these we need to put an additional equal sign here.
If user age is greater than or equal to 16 then this text access permit it will be displayed.
Let's run it.
The text is displayed.
And if we put 18 the text is also displayed another operator used in comparison.
Is there a quality operator for example we want to display the text you are 18 years old.
If user age is 18 let's write another ls statement for these else if.
And here we specify user age to equal signs and 18 since 1 equals sign is used in Java to assign a value
to a variable and 2 equals science are used for comparison.
And here.
All right system out parental then
you are 18 years old
let's run the code
huh.
Forgot to put a semicolon put and run again.
And displayed access permitted.
Why did this happen.
This happened because 18 is greater than or equal to 16.
The code is executed from top to bottom and the first condition is satisfied.
All subsequent statements are not executed after the first one is executed.
If we switch places here we put the user age equal to 18.
And here we put more than or equal to sixteen swap them.
Well of course the text also needs to be changed.
And as you can see the first text is displayed since the condition is checked in the if statement if
it is satisfied.
This piece of code is executed and the rest of the code is not executed well now we can apply our knowledge
in our application.