1. Java if Statement
The syntax of if-then statement in Java is:
if (expression)
{ // statements }
Example1
public static void main(String[] args) {
// TODO code application logic here
int number =0;
if (number > 0)
{
System.out.println("Number is positive.");
}
}
Example2
public static void main(String[] args) {
// TODO code application logic here
int password =123;
if (password==123)
{
System.out.println("Access Grandted.");
}
}
Java if...else (if-then-else) Statement
The if statement executes a certain section of code if the test expression is evaluated to true. The if
statement can have optional else statement. Codes inside the body of else statement are executed if the
test expression is false.
Example1
int number =5;
if (number > 1)
{
System.out.println("Number is greater than 1.");
}
else
{
System.out.println("Number is less than 1.");
}
}
2. Example2
public static void main(String[] args) {
// TODO code application logic here
String Username="MNHS246";
if (Username.equals("MNHS246"))
{
System.out.println("Access Granted");
}
else
{
System.out.println("Access Denied");
}
Activity1
1. Compute and display the TOTAL of items purchased by the customer.
If Total is greater than 1000, statement “You have the privilege of 5% discount”,
else “You have no privilege of 5% discount ”
Given: Items = “PC Accessories”
Price = 2000
Quantify = 3
Activity2
2. Compute and display the Name and TestScore of Juan Dela Cruz .
Quiz 1 = 90.21
Quiz 2= 75.00
Quiz3 = 88.50
Quiz4 = 76.56
Condition: If TestScore is equal and greater than 75, then statement “ Passed” else “Failed”.
Activity3
3. Compare and display the Name , Age , Height of Gina Tolentino. If Age is equal to Height then,
statement “Your height is equal to your age!”, else “Your height is
less than your age!".
Activity4
4. Create a program that will display and accept the username and
password of MNHS.
Username = “MNHS246”, Password =2346
If username and password are equal to the given account above, then
your statement is “Access Granted ”, else “Access Denied”
3. public static void main(String[] args) {
// TODO code application logic here
String Username="MNHS246";
String Password = "MAIN123";
if (Username.equals("MNHS246") && Password.equals("MAIN1123"))
{
System.out.println("Access Granted");
}
else
{
System.out.println("Access Denied");
}
public static void main(String[] args) {
// TODO code application logic here
String Username="MNHS246";
String Password = "MAIN123";
if (Username.equals("MNHS246") || Password.equals("MAIN123"))
{
System.out.println("Access Granted");
}
else
{
System.out.println("Access Denied");
}
public static void main(String[] args) {
// TODO code application logic here
String Username="MNHS246";
String Password = "MAIN123";
if (Username.equals("MNHS246") != Password.equals("MAIN123"))
{ System.out.println("Access Granted"); }
else
{ System.out.println("Access Denied"); }
public static void main(String[] args) {
// TODO code application logic here
String Username="MNHS246";
int Password = 123;
if (Username.equals("MNHS246") && (Password==123))
{ System.out.println("Access Granted"); }
else { System.out.println("Access Denied"); }