Page 250 - PowerPoint Presentation
P. 250
CAVITE STATE UNIVERSITY
TRECE MARTIRES CITY CAMPUS
Department of Information Technology DCIT 111 - Advanced Programming
Example:
class ConditionalExample { //use of Logical Operators
public static void main(String[]args) {
int a=20, b=10, c=5;
if (a > b && a>c) {
System.out.println(“A is the greatest!”);
}
if (a>b || a>c) {
System.out.println(“A is the greatest!”);
} }
Conditional Statements
The Java if statement is used to test the condition. It checks Boolean condition: true
or false. There are various types of if statement.
- if statement
- if-else statement
- if-else-if ladder
- nested if statement
Java if Statement
The java if statement test the condition. It executes the if block if condition is true.
//Syntax
if (condition) {
// code to be executed
}
// Java program to demonstrate the use of if statement
False
condition
public class ifExample {
True public static void main(String[] args) {
if code int age=20;
Statement if(age>19) {
System.out.println(“Age is greater than 19”);
} } } Output:
Age is greater than 19
Java if-else Statement
The Java if-else statement also test the condition. It executes the if block if condition
is true otherwise else block will be executed.
//Syntax
if (condition) {
// code if condition is true
}
else {
// conde if condition is false
}
26