SlideShare une entreprise Scribd logo
1  sur  3
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.");
}
}
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”
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"); }

Contenu connexe

Similaire à Java-if-Statement-Activity2.docx

Selection Control Structures
Selection Control StructuresSelection Control Structures
Selection Control Structures
PRN USM
 
12422, 744 PM Assignment_3localhost8888nbconverthtml.docx
12422, 744 PM Assignment_3localhost8888nbconverthtml.docx12422, 744 PM Assignment_3localhost8888nbconverthtml.docx
12422, 744 PM Assignment_3localhost8888nbconverthtml.docx
robert345678
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
alish sha
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
alish sha
 

Similaire à Java-if-Statement-Activity2.docx (20)

C Unit-2.ppt
C Unit-2.pptC Unit-2.ppt
C Unit-2.ppt
 
Java input Scanner
Java input Scanner Java input Scanner
Java input Scanner
 
Simple if else statement,nesting of if else statement & else if ladder
Simple if else statement,nesting of if else statement & else if ladderSimple if else statement,nesting of if else statement & else if ladder
Simple if else statement,nesting of if else statement & else if ladder
 
Java Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, LoopsJava Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, Loops
 
Bsit1
Bsit1Bsit1
Bsit1
 
02 - Prepcode
02 - Prepcode02 - Prepcode
02 - Prepcode
 
Computational Problem Solving 016 (1).pptx
Computational Problem Solving 016 (1).pptxComputational Problem Solving 016 (1).pptx
Computational Problem Solving 016 (1).pptx
 
Lecture - 5 Control Statement
Lecture - 5 Control StatementLecture - 5 Control Statement
Lecture - 5 Control Statement
 
Selection Control Structures
Selection Control StructuresSelection Control Structures
Selection Control Structures
 
12422, 744 PM Assignment_3localhost8888nbconverthtml.docx
12422, 744 PM Assignment_3localhost8888nbconverthtml.docx12422, 744 PM Assignment_3localhost8888nbconverthtml.docx
12422, 744 PM Assignment_3localhost8888nbconverthtml.docx
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
Dti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selectionDti2143 chap 4 control structures aka_selection
Dti2143 chap 4 control structures aka_selection
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
Java tut1
Java tut1Java tut1
Java tut1
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 

Dernier

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Dernier (20)

[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 

Java-if-Statement-Activity2.docx

  • 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"); }