if-else:


if-else:

These statements are conditional statements and can route the flow of program to different paths

Syntax:

If (condition) {
// statements to be executed when condition is true
} else {
// statements to be executed when condition is false
}

Nested If-else:

If an if-else statement is used inside another if or else block, it is said to be nested if-else.

Syntax:

If (condition) {
...
If (condition-two) {
...
} else
{ ...
}
...
} else
{ ...
}

if-else-if ladder:

It is a sequence of if-else-if statements that are evaluated in the given order. If a condition is evaluated to be true then all following if-else statements are skipped
Note: The order of execution is: if -> else if -> else if -> … -> else.
Note: Control will come out of chain after execution of matched block.

Syntax:

If (condition) {
...
} else if (condition-two) {
...
} else if (condition-three) {
...
} else {
...
}

Example No.1:

Com.knowledge2life class Condition { public static void main(String[] args) { String a = new String("Knowledge2life"); if (a.equals("Knowledge2life")) { System.out.println("Knowledge2life!"); } } }

OUTPUT:

Knowledge2life!

Example No.2:

Com.knowledge2life class Condition1 { public static void main(String[] args) { int a=20; int b=10; if (a==b) { System.out.println("A equal to B"); } else System.out.println("Knowledge2life!"); } }

OUTPUT:

Knowledge2life!