[MUSIC].
>> Greetings, everyone, and welcome back to
Microsoft's Java for Beginners series.
My name is Brian Benz and I'm a Cloud advocate at Microsoft.
I'm joined by RJ today,
who's going to tell us a little bit about conditional logic.
RJ, do you want to introduce yourself and the topic?
>> Sure. Thanks, Brian. Hey, everyone.
My name is Rajasa Savant and I'm
a Software Engineer here at Microsoft.
In this session, we'll talk about
conditional and logical operators briefly,
and then dive deep into what
options Java offers for conditional logic.
Conditional logic helps to branch
the execution of a program based on certain conditions.
It includes the following components. It has
the test expression and the body or code block to be executed.
Before we go into that, first,
we'll talk about operators that
help with establishing these conditions.
We have the logical operators which carry out the
AND, NOT, and OR logic and the conditional operators.
For the logical operators,
these examples might explain a couple of things.
This is how you use a logical operator
OR, this is how you use the operator AND,
and this is how you use the operator NOT.
When we look at conditional operators,
they help us to compare expressions.
Here we have the greater than,
greater than or equal to, less than,
less than or equal to,
equal to, and not equal to operators.
We'll see some examples to see
how we use the conditional operators.
Now, let's begin with the conditional if statements in Java.
It evaluates the condition given in the parenthesis,
and if this evaluates to true,
the code in the block below is executed once.
If this condition evaluates to false,
the code execution moves to
the next statement after this closed curly brace here.
Essentially, if this condition evaluate to false,
the statements in this particular code block are not executed.
As you see in this example,
let's try to see if a certain number is odd or even.
If they have the number in this variable,
we try and find out if the remainder
after division by 2 is equal to 0.
If the remainder is 0,
then the number is even.
The if executes this,
if this condition comes out to be true.
The if-else statement like the name suggests,
provides an alternate execution that if
the condition in the parenthesis evaluates to false.
As you see here, if this condition is true,
it will go ahead and execute the statements here.
If this condition is false,
then the statement in the else block are executed.
This can be seen in the example here,
so if the number is divisible by 2,
the statement is executed else,
the statement is executed,
and we will see that the numbers are odd.
How do we handle multiple conditions?
We can have a multiple chain of if-else-if loop
or if-else-if conditions.
The only thing that we have to be careful about
is that chaining and nesting can make the code difficult to read,
so if there are multiple of these
if-else-if conditions,
that might make the code a bit more difficult to read.
As you see here, if the number of 0, print the number is 0.
If it is divisible by 2,
print the numbers even else,
print the numbers is odd.
In this case, we are not checking for negatives,
we are not checking for rationals, a lot of
things that are missing in this particular piece of code.
If we think that this number is
going to be something else other than int.
How do we go about that?
Let's talk about something called as a switch statement.
Now, the switch statement in Java
focuses on not evaluating conditions,
but picking one possibility out of several.
We'll deal with a single expression,
and we are going to pick one of the possibilities out
of several that this expression could take.
Now, the default statement that you see here is
optional and can appear anywhere inside the switch block.
This is the switch block.
In case if it does not at the end the default statement,
then a break statement must be kept after
the default statement to omit
the execution of the next case statement.
Let's see what we mean by that.
Here, we are in this example,
we are trying to see or print the day of the week,
whether that is a weekend or not.
Let's say this is the variable that has a day of the week
and let's say currently the value of this variable is Friday.
In that case, the expression day
of the week is going to be evaluated for these cases.
As you see here,
not every case has a break statement and not only that,
not every case has some statements that need to be executed.
In this situation,
what happens is, since the day of the week is Friday,
the first case is evaluated. It's not Monday,
it'll move down, not Tuesday,
move down, not Wednesday,
not Thursday but it is Friday.
In this case, the statement is executed,
so weekend will get the value false and
the switch statement will break and
assign the weekend variable a value of false.
What would have happened if the day of
the week would have been, let's say Wednesday?
In that case, the code would have moved down to
here evaluated that this is true, Wednesday.
But the statement for Wednesday is right here,
because this statement is applicable to all these cases,
since these cases do not have a break after them.
Now, similarly,
if the day of the week is Saturday or Sunday,
this is what is assigned to the variable weekend.
Moving forward, we will
see another version of this switch statement.
Now, this version of
switch statement that we see here is from Java 7
onwards but switch existed in Java even before this.
Now, the version of switch statement that we see here is
a combination of several versions of
switch statement available over several versions of Java.
All of these should be available together in version 14 or later.
In version 14, we see this modified version of
the switch statement that we'll see how that works.
First, notice how we no longer need a break.
The switch statement just returns
whatever expression comes after the arrow operator,
and because the switch statement now has a return value,
it can be a single expression on the right side of an assignment.
What this means is, we don't have to declare
a variable like we did in the previous case here,
we don't have to declare a variable
separately and assign it from each case.
But in this situation, what happens is,
the switch statement itself can
now function as a single expression with
a return value that can be assigned to
the boolean weekend in this case.
One thing to note, is that we have to be very
careful about using brakes and switch statement.
In this example that we see here,
the day of the week is evaluated
by per case and if it is let's say, Thursday,
this is the value on the right side of the arrow that is assigned
a return back as of return of the switch statement.
That value is assigned to the variable weekend.
With this, we cover conditional logic in Java. Thank you.
>> Thanks, RJ. That was great.
Please remember to check out the rest of
the topics in Microsoft's Java for Beginners series.
>> Thanks, everyone.
[MUSIC]