SlideShare uma empresa Scribd logo
1 de 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"); }

Mais conteúdo relacionado

Semelhante a Java-if-Statement-Activity2.docx

Java input Scanner
Java input Scanner Java input Scanner
Java input Scanner Huda Alameen
 
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 ladderMoni Adhikary
 
Java Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, LoopsJava Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, LoopsSvetlin Nakov
 
Computational Problem Solving 016 (1).pptx
Computational Problem Solving 016 (1).pptxComputational Problem Solving 016 (1).pptx
Computational Problem Solving 016 (1).pptx320126552027SURAKATT
 
Lecture - 5 Control Statement
Lecture - 5 Control StatementLecture - 5 Control Statement
Lecture - 5 Control Statementmanish kumar
 
Selection Control Structures
Selection Control StructuresSelection Control Structures
Selection Control StructuresPRN USM
 
12422, 744 PM Assignment_3localhost8888nbconverthtml.docx
12422, 744 PM Assignment_3localhost8888nbconverthtml.docx12422, 744 PM Assignment_3localhost8888nbconverthtml.docx
12422, 744 PM Assignment_3localhost8888nbconverthtml.docxrobert345678
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control StructureSokngim Sa
 
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_selectionalish 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_selectionalish sha
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptxDEEPAK948083
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programmingnmahi96
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions imtiazalijoono
 

Semelhante a 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
 

Último

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
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 REVIEWERMadyBayot
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
"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 ...Zilliz
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
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...DianaGray10
 

Último (20)

AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
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
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
"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 ...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
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...
 

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