/java_ifElse

Example Application: Conditional: IF/ELSE

Primary LanguageJava

Java Conditional IF/Else

Example Application: Conditional: IF/ELSE

Simple conditional IF and ELSE

int value = 1;
if (value == 1) {
	// TODO
	System.out.println("Value is 1");

} else {
	// TODO
	System.out.println("Value is not 1");

}

IF conditional with multiple ELSE

int value = 1;
if (value < 1) {
	// TODO
	System.out.println("Value is less than 1");

} else if (value == 1) {
	// TODO
	System.out.println("Value is 1");

} else if (value == 2) {
	// TODO
	System.out.println("Value is 2");

} else if (value > 2) {
	// TODO
	System.out.println("Value is greater than 2");

}

IF with Boolean

boolean bVal = true;

if (bVal) {
	// TODO
	System.out.println("TRUE");

} else {
	// TODO
	System.out.println("FALSE");

}

IF with "!".

boolean bVal = true;
if (!bVal) {
	// TODO
	System.out.println("TRUE");
} else {
	// TODO
	System.out.println("FALSE");
}

IF String Equals.

String sVal = "wwww.google.com";
if (sVal.equals("wwww.google.com")) {
	// TODO
}

IF String Equals Ignore Case Sensitive.

String sVal = "wwww.google.com";
if (sVal.equalsIgnoreCase("WWW.GOOGLE.COM")) {
	// TODO
}

IF String starts with the specified prefix.

String sVal = "wwww.google.com";
if (sVal.startsWith("www")) {
	// TODO
}

IF String end with the specified prefix.

String sVal = "wwww.google.com";
if (sVal.endsWith(".com")) {
	// TODO
}

IF String using &&

String sVal = "wwww.google.com";
if (sVal.startsWith("www") && sVal.endsWith(".com")) {
	// TODO
}

IF String using ||

String sVal = "wwww.google.com";
if (sVal.startsWith("www") || sVal.endsWith(".com")) {
	// TODO
}

IF String is Empty value.

String sVal = "wwww.google.com";
if (sVal.isEmpty()) {
	// TODO
}

If conditional (ternary)

Sintax conditional (ternary)

condition ? exprIfTrue : exprIfFalse

boolean bVal = true;
String value = "This boolean is: "+ (bVal==true?"True":"False");

Some links for more in depth learning