Welcome to the third part of our Core Java series. In the last part, we discussed different expressions, datatypes and operators. If you are new, please check out the previous two parts in this series.
Let’s discuss some more aspects of Java Language.
These statements are formed using boolean expressions and they are used to alter the sequence of execution of our code. It follows the below syntax -
1
2
3
4
5
if (boolean expression) {
statement 1;
} else {
statement 2;
}
else
part is optional. It will execute when the if condition is false. Let’s see one example -
1
2
3
4
5
int a = 2;
if (a == 4)
System.out.println("X")
else
System.out.println("Y")
Output of this code is Y
because the if condition is false. One thing to note here is that just like for mathematical operations we use arithmetic operators, for comparison we use relational operators
. These are <, <=, >=, ==, !=
. These are used to compare two operands and they return a logical value that may be either true or false. In our example, we used ==
to check whether the value of a is equal to 4 or not. ==
and =
are different. One is used for comparison and the other is used for value assignment.
We can use multiple if-else
conditions in our code. Conditions within a conditional statement are called nested conditional statements
.
Apart from these relational operators
there are Boolean Operators
which can also be used in Java. These are used to combine two or more conditional statements. These are -
1 2 3
&& -AND || -OR!-NOT
For both conditions must be true. For
||
one of the two conditions is sufficient for the statement to be true. !
is counterpart(!true == false).
example -
1
2
3
4
5
int a = 1, b = 2;
if (a > 0 && b > 3)
System.out.println("X")
else
System.out.println("Y")
Output: Y
NOTE - In case of multiple boolean operators in a sentence, the precedence order will be NOT > AND > OR.
This is another way of handling conditions in your code. It follows the below syntax -
1
2
3
4
5
6
7
8
9
10
11
12
13
14
switch (integer or string expression) {
case value 1:
statement...
break
case value 2:
statement...
break.
.
.
.
case
default:
statement...
}
We can use default anywhere and it is optional. But if the location of default is changed from last then we have to use break statement there also.
Switch only takes integer or string expressions. Long type will give compile time error.
Ternary operators are another alternative to if-else
and they can be written with less lines of code. It is called ternary because it has three arguments. It follows the below syntax.
variable = (boolean expression)?true-part:false-part;
e.g.
1 2 3 4
if (a > b) max = a; else max = b;
This code is to get the max of two numbers using if-else
. Now see its logic in ternary operator -
max = (a>b)?a:b;
That’s it. Only one line of code :)
Loop means repeated execution of a statement. There are three types of loops we can use in Java. Let’s see them one by one.
while loop -
1 2 3
while (boolean - exp) { statement; }
It takes a boolean exp as argument and executes the code until that condition becomes false.
e.g.
1
2
3
int a = 2;
while (a > 0)
a -= 1;
This code will run two times and each time the value of a will reduce to 1. As soon as it reaches value 0, the loop will terminate.
for loop -
1 2 3
for (initialization part; condition; action part) { statement; }
In for loop, we put an initial value, a boolean condition to be checked and an action part (how the value will change after each iteration).
We can write multiple statements in the initialization part or the action part by separating them with a comma (,). Multiple conditions are always combined by boolean operators.
e.g.
1
2
3
4
int sum = 0;
for (int i = 0; i <= 10, i++)
sum += i;
System.out.println(sum);
Output: 55
All these three parts of for loop are optional. If we remove all of them, then we’ll get an infinite loop that will not stop unless you give a break condition or forcefully close your program.
1 2 3 4
for (;;) { if (condition) break; }
Break
statement is used to skip remaining statements of the loop and shift the control outside the loop.
1 2 3 4 5
for (;;) { statement; // some code break; statement; // some more code }
The above example will throw a compile time error because unreachable code is not allowed in Java
.
do…while loop
1 2 3
do { statement; //some code } while (boolean - exp);
This is like a while loop but the only difference is that it runs at least once
before checking the boolean expression.
Just like nested conditionals, we can put a loop inside another loop statement and that is called a nested loop
.
So far we learned a lot of fundamentals about Java. Now it’s your turn to try out these topics. Try to make some small-small programs to better grasp these topics. I’ll see you in the next part. Until then happy coding :)