Java If Statements:
We can use the Eclipse IDE for this example. If you do not know about it then follow this link- How to Install Eclipse For Java and create a program in it. |
Java if statements are defined as simple if or if else statements. The simple if statement executes a statement or program segment for a true if condition. The second form of an if statement executes a statement or program segment based on whether the condition is true or false and the statement is defined as if else.

The expression in parentheses must produce a boolean result. At run time, the computer evaluates the expression. If the value is true, the computer executes Statement 1A. If the value of the expression is false, it executes Statement 1B.
if Statement Example:

package com.java_tutorial;
import java.util.Scanner;
public class If_statement {
public static void main(String[] args) {
// TODO Auto-generated method stub
int age;
System.out.println("Enter Your Age:-- ");
Scanner x = new Scanner(System.in);
age = x.nextInt();
if (age >= 25)
{
System.out.println("You can watch this movie.......");
}
}
}
if else Statement Example:

package com.java_tutorial;
import java.util.Scanner;
public class If_statement {
public static void main(String[] args) {
// TODO Auto-generated method stub
int age;
System.out.println("Enter Your Age:-- ");
Scanner x = new Scanner(System.in);
age = x.nextInt();
if (age >= 25)
{
System.out.println("You can watch this movie.......");
}
else {
System.out.println("You cannot watch this movie.......");
}
}
}
if ….. else if ….. else Statement Example:

package com.java_tutorial;
import java.util.Scanner;
public class If_statement {
public static void main(String[] args) {
// TODO Auto-generated method stub
int age;
System.out.println("Enter Your Age:-- ");
Scanner x = new Scanner(System.in);
age = x.nextInt();
if (age >= 35)
{
System.out.println("You can watch this movie.......");
}
else if (age>= 25)
{
System.out.println("You can watch this movie for free.......");
}
else {
System.out.println("You cannot watch this movie.......");
}
}
}
Comments (No)