Java nested if statement
In this chapter you will learn:
Description
A nested if
is an if
statement inside another another if
statement or else
.
Example
The following code uses a nested if statement to compare values.
public class Main {
public static void main(String[] argv) {
int i = 10;/*from w w w . ja v a 2s . co m*/
int j = 4;
int k = 200;
int a = 3;
int b = 5;
int c = 0;
int d =0;
if (i == 10) {
if (j < 20){
a = b;
}
if (k > 100){
c = d;
}
else{
a = c;
}
} else{
a = d;
}
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
}
}
The output:
Next chapter...
What you will learn in the next chapter:
- What is the Java Switch Statement
- Syntax for Java switch Statement
- Example - Java Switch Statement
- Example - How to use the optional break statement
- Example - How to write nested switch Statements