These Statements are able to execute a block of instructions on the basis of a particular condition.
if statement
Syntaxes:
1.
if(condition) {
—--instructions—--
}
2.
if(condition) {
—---instructions—--
} else {
—----instructions—---
}
3.
if(condition) {
—-----instructions—--
} else if(condition) {
—----instructions—---
} else if(condition) {
—----instructions—---
}
—----
—----
else {
—-----instructions—----
}
Examples
Ex:
class Test{
public static void main(String[] args){
int num = 10;
if(num % 2 == 0){
System.out.println(num+" Is Even Number");
}
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
10 Is Even Number
Ex:
class Test{
public static void main(String[] args){
int num = 5;
if(num % 2 == 0){
System.out.println(num+" Is Even Number");
}else{
System.out.println(num+" Is ODD NUmber");
}
}
}
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
5 Is ODD NUmber
EX:
class Test{
public static void main(String[] args){
int num1 = 10;
int num2 = 10;
if(num1 > num2){
System.out.println(num1+" Is Biggest Number");
}else if(num2 > num1){
System.out.println(num2+" Is Biggest Number");
}else{
System.out.println(num1+", "+num2+" Are Equal");
}
}
}
Variables – scope
class A{
int i = 10; —---> Class level variable
void m1(){
int j = 20; —-> Local Variable
System.out.println(j);
System.out.println(k);------> Error
System.out.println(i);
}
void m2(){
int k = 30; —---> Local Variable
System.out.println(k);
System.out.println(j);------> Error
System.out.println(i);
}
}
In Java , only class level variables are having default values, local variables are not having default values. If we declare any local variable then we must provide initialization for that local variable explicitly otherwise the compiler will raise an error. If we declare any local variable without the initialization and if we access that variable then the compiler will raise an error.
class A{
int i;
void m1(){
int j;
System.out.println(i);// 0
System.out.println(j); // Error
j = 10;
System.out.println(j); // 10
}
}
Conditional Expressions
In Java, there are 2 types of conditional expressions :
- Constant Conditional Expression:
It includes only constant variables or direct constants.
These conditional expressions are evaluated by the compiler at the time of compilation.
EX: if(10==10) {
}
EX: if(true) {
}
EX: final int i = 10;
if(i == 10) {
}
- Variable Conditional Expression :
It includes at least one variable [non final variable].
These conditional expressions are evaluated by JVM at runtime.
EX: int i = 10;
int j = 10;
if(i == j) {
}
EX: int i = 10;
if(i == 10) {
}
EX:
class Test{
public static void main(String[] args){
int i = 10;
int j;
if(i == 10){
j = 20;
}
System.out.println(j);
}
}
Status: Compilation Error.
D:\Fullstack10AM>javac Test.java
Test.java:8: error: variable j might not have been initialized
System.out.println(j);
^
1 error
EX:
class Test{
public static void main(String[] args){
int i = 10;
int j;
if(i == 10){
j = 20;
}else{
j = 30;
}
System.out.println(j);
}
}
Status: No Compilation Error
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
20
EX:
class Test{
public static void main(String[] args){
int i = 10;
int j;
if(i == 10){
j = 20;
}else if (i == 20) {
j = 30;
}
System.out.println(j);
}
}
Status:Compilation Error
D:\Fullstack10AM>javac Test.java
Test.java:10: error: variable j might not have been initialized
System.out.println(j);
^
1 error
EX:
class Test{
public static void main(String[] args){
int i = 10;
int j;
if(i == 10){
j = 20;
}else if(i == 20){
j = 30;
}else{
j = 40;
}
System.out.println(j);
}
}
Status: No Compilation Error
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
20
Using final variable for conditional expression
EX:
class Test{
public static void main(String[] args){
final int i = 10;
int j;
if(i == 10){
j = 20;
}
System.out.println(j);
}
}
STatus: No Compilation Error
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
20
EX:
class Test{
public static void main(String[] args){
int j;
if(true){
j = 20;
}
System.out.println(j);
}
}
Status: No Compilation Error
OP: 20
D:\Fullstack10AM>javac Test.java
D:\Fullstack10AM>java Test
20