If the expression is too long, you can use two units of indentation for subsequent lines.
public class MainClass {
public static void main(String[] args) {
int numberOfLoginAttempts = 10;
int numberOfMinimumLoginAttempts = 12;
int numberOfMaximumLoginAttempts = 13;
int y = 3;
if (numberOfLoginAttempts < numberOfMaximumLoginAttempts
|| numberOfMinimumLoginAttempts > y) {
y++;
}
}
}
3. Using braces makes your 'if' statement clearer
If there is only one statement in an if or else block, the braces are optional. |
public class MainClass {
public static void main(String[] args) {
int a = 3;
if (a > 3)
a++;
else
a = 3;
}
}
|
|
Consider the following example: |
public class MainClass {
public static void main(String[] args) {
int a = 3, b = 1;
if (a > 0 || b < 5)
if (a > 2)
System.out.println("a > 2");
else
System.out.println("a < 2");
}
}
|
|
- It is hard to tell which if statement the else statement is associated with.
- Actually, An else statement is always associated with the immediately preceding if.
|
Using braces makes your code clearer. |
public class MainClass {
public static void main(String[] args) {
int a = 3, b = 1;
if (a > 0 || b < 5) {
if (a > 2) {
System.out.println("a > 2");
} else {
System.out.println("a < 2");
}
}
}
}
|
|
4. Multiple selections
If there are multiple selections, you can also use if with a series of else statements. |
if (booleanExpression1) {
// statements
} else if (booleanExpression2) {
// statements
}
...
else {
// statements
}
|
|
For example: |
public class MainClass {
public static void main(String[] args) {
int a = 0;
if (a == 1) {
System.out.println("one");
} else if (a == 2) {
System.out.println("two");
} else if (a == 3) {
System.out.println("three");
} else {
System.out.println("invalid");
}
}
}
|
|
5. The if Statement in action
public class MainClass {
public static void main(String[] arg) {
int a = 0;
if (a == 0) {
System.out.println("a is 0");
}
}
6. The else Clause
public class MainClass {
public static void main(String[] arg) {
int a = 0;
if (a == 0) {
System.out.println("in the block");
System.out.println("in the block");
} else {
System.out.println("a is not 0");
}
}
}
compile results
in the block
in the block
7.
Nested if Statements
public class MainClass {
public static void main(String[] arg) {
int a = 2;
if (a == 0) {
System.out.println("in the block");
if (a == 2) {
System.out.println("a is 0");
} else {
System.out.println("a is not 2");
}
} else {
System.out.println("a is not 0");
}
}
}
compile results
a is not 0
8.Using && in if statement
public class MainClass {
public static void main(String[] arg) {
int value = 8;
int count = 10;
int limit = 11;
if (++value % 2 == 0 && ++count < limit) {
System.out.println("here");
System.out.println(value);
System.out.println(count);
}
System.out.println("there");
System.out.println(value);
System.out.println(count);
}
}
there
9
10
9.Using || (or operator) in if statement
public class MainClass {
public static void main(String[] arg) {
int value = 8;
int count = 10;
int limit = 11;
if (++value % 2 != 0 || ++count < limit) {
System.out.println("here");
System.out.println(value);
System.out.println(count);
}
System.out.println("there");
System.out.println(value);
System.out.println(count);
}
}
here
9
10
No comments:
Post a Comment