Java If … Else
Java Conditions and If Statements
Java supports the usual logical conditions from mathematics:
- Less than: a < b
- Less than or equal to: a <= b
- Greater than: a > b
- Greater than or equal to: a >= b
- Equal to a == b
- Not Equal to: a != b
You can use these conditions to perform different actions for different decisions.
Java has the following conditional statements:
- Use
if
to specify a block of code to be executed, if a specified condition is true - Use
else
to specify a block of code to be executed, if the same condition is false - Use
else if
to specify a new condition to test, if the first condition is false - Use
switch
to specify many alternative blocks of code to be executed
The if Statement
Use the if
statement to specify a block of Java code to be executed if a condition is true
.
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
Note that if
is in lowercase letters. Uppercase letters (If or IF) will generate an error.
In the example below, we test two values to find out if 20 is greater than 18. If the condition is true
, print some text:
Example
if (20 > 18) {
System.out.println("20 is greater than 18");
}
We can also test variables:
Example
int x = 20;
int y = 18;
if (x > y) {
System.out.println("x is greater than y");
}
Example explained
In the example above we use two variables, x and y, to test whether x is greater than y (using the >
operator). As x is 20, and y is 18, and we know that 20 is greater than 18, we print to the screen that “x is greater than y”.
The else Statement
Use the else
statement to specify a block of code to be executed if the condition is false
.
Syntax
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
Example
int time = 20;
if (time < 18) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
// Outputs "Good evening."
Example explained
In the example above, time (20) is greater than 18, so the condition is false
. Because of this, we move on to the else
condition and print to the screen “Good evening”. If the time was less than 18, the program would print “Good day”.
The else if Statement
Use the else if
statement to specify a new condition if the first condition is false
.
Syntax
if (condition1) {
// block of code to be executed if condition1 is true
} else if (condition2) {
// block of code to be executed if the condition1 is false and condition2 is true
} else {
// block of code to be executed if the condition1 is false and condition2 is false
}
Example
int time = 22;
if (time < 10) {
System.out.println("Good morning.");
} else if (time < 20) {
System.out.println("Good day.");
} else {
System.out.println("Good evening.");
}
// Outputs "Good evening."
Example explained
In the example above, time (22) is greater than 10, so the first condition is false
. The next condition, in the else if
statement, is also false
, so we move on to the else
condition since condition1 and condition2 is both false
– and print to the screen “Good evening”.
However, if the time was 14, our program would print “Good day.”
buy levofloxacin 250mg pills levofloxacin 250mg cheap
avodart 0.5mg price order zofran 4mg online ondansetron price
aldactone 100mg price purchase aldactone pills fluconazole 200mg price
ampicillin 250mg drug buy ampicillin 500mg pill order erythromycin 250mg without prescription
sildenafil 50mg generic cheap bimatoprost robaxin cheap
sildenafil 50mg for sale aurogra 100mg drug buy generic estrace 2mg
purchase lamotrigine without prescription order mebendazole pill buy retin cream generic
tadalis 10mg oral tadalafil pills order diclofenac 100mg generic
indocin cost indomethacin price purchase trimox for sale
order cialis 10mg pills Viagra mail order sildenafil 50mg drug
order arimidex without prescription Viagra australia sildenafil medication
cialis 10mg en france cialis 20mg gГ©nГ©rique acheter 200mg du viagra
prednisone brand buy viagra generic buy generic sildenafil 100mg
tadalafil 20mg kaufen ohne rezept sildenafil generika viagra 200mg kaufen ohne rezept
accutane 40mg oral buy amoxicillin 500mg without prescription ivermectin generic name
provigil 100mg cost provigil 200mg usa diamox 250 mg uk
doxycycline 200mg brand vardenafil 20mg drug oral furosemide 40mg
buy ramipril pill order clobetasol generic order azelastine 10 ml without prescription
catapres 0.1 mg oral antivert usa spiriva 9 mcg uk
order buspar 5mg for sale order dilantin 100mg online oxybutynin tablet
terazosin 5mg over the counter purchase arava without prescription order sulfasalazine 500mg for sale
buy fosamax 35mg pills buy fosamax 35mg generic pepcid 40mg pill
olmesartan 10mg cost buy acetazolamide 250 mg online order acetazolamide
prograf 5mg pills tacrolimus 1mg tablet buy generic urso 150mg
imdur online buy cheap isosorbide 40mg micardis 20mg price
buy zyban 150 mg online cheap order seroquel 100mg for sale seroquel 50mg pill
molnunat cheap molnupiravir 200mg ca buy prevacid online cheap
order zoloft 50mg pill order zoloft generic brand viagra pills
cheap imuran viagra 25mg for sale order viagra 50mg online
cialis 10mg drug fluoxetine 40mg drug purchase viagra sale
tadalafil 10mg over the counter buy symmetrel 100 mg sale cost amantadine 100mg
order revia 50mg online cheap buy letrozole 2.5mg without prescription buy abilify 20mg online cheap
avlosulfon 100mg ca buy allegra 120mg for sale cheap perindopril
provera 10mg brand order microzide 25mg sale order cyproheptadine 4 mg online cheap
order provigil sale provigil 200mg cost ivermectin 12mg for sale
fluvoxamine uk buy ketoconazole sale brand glipizide
order isotretinoin online order amoxicillin 1000mg sale generic deltasone 10mg
buy piracetam 800mg for sale viagra online buy buy sildenafil sale
azithromycin pills buy azithromycin sale order gabapentin without prescription
buy cialis pill purchase tadalafil online sildenafil over counter
purchase lasix without prescription purchase doxycycline sale order plaquenil 200mg pills
buy generic cialis 40mg buy anafranil without prescription clomipramine 50mg pills
https://drugsoverthecounter.shop/# uhc over the counter essentials 2019
omeprazole over the counter nausea medicine over the counter for pregnancy
https://over-the-counter-drug.com/# the best over counter sleep aid
over the counter inhaler walmart over the counter migraine medicine
amoxicillin over the counter cvs over the counter asthma inhaler
https://over-the-counter-drug.com/# best over the counter cold medicine
over the counter ear drops over the counter birth control
over the counter medication bv treatment over the counter
over the counter ear drops chlorhexidine mouthwash over the counter
over the counter appetite suppressant strongest over the counter painkillers
https://over-the-counter-drug.com/# best over the counter toenail fungus medicine
metronidazole over the counter over the counter testosterone
https://over-the-counter-drug.com/# oral thrush treatment over the counter
over the counter tapeworm treatment for dogs best hemorrhoid treatment over the counter
over the counter essentials united healthcare over the counter blood thinners
best over the counter toenail fungus medicine ringworm treatment over the counter
is viagra over the counter over the counter ear drops
over the counter medicine for anxiety and stress what is the best over-the-counter anti-inflammatory for dogs
https://over-the-counter-drug.com/# nystatin cream over the counter
best hemorrhoid treatment over the counter over the counter
amoxicillin 500 mg cost amoxil for sale
https://stromectol.science/# ivermectin canada
doxycycline 100mg tablets doxycycline hyc
https://stromectol.science/# dynacin minocycline
minocycline 50mg otc stromectol for sale
https://zithromax.science/# where can i buy zithromax in canada
generic zithromax where to buy zithromax in canada
https://doxycycline.science/# buy generic doxycycline
buy zithromax online can you buy zithromax over the counter in australia
odering doxycycline 100mg doxycycline or doxycycline 50mg
http://colourcoded.net/__media__/js/netsoltrademark.php?d=doxycycline.science buy cheap doxycycline online
doxycycline 100mg online doxycycline hydrochloride 100mg and buy doxycycline online uk doxycycline mono
stromectol pills generic stromectol
https://stromectol.science/# ivermectin stromectol
order doxycycline 100mg without prescription doxycycline order online
buy stromectol online uk cheap stromectol
https://amoxil.science/# amoxicillin 250 mg
buy doxycycline cheap buy doxycycline cheap
https://amoxil.science/# amoxicillin 500mg tablets price in india
buy zithromax zithromax 500mg
https://zithromax.science/# zithromax z-pak price without insurance
cheap doxycycline online where can i get doxycycline or doxycycline 50 mg
http://oncedotdotdot.com/__media__/js/netsoltrademark.php?d=doxycycline.science buy generic doxycycline
doxycycline doxycycline hyclate and 100mg doxycycline price of doxycycline
can i buy zithromax over the counter in canada buy zithromax online
https://zithromax.science/# average cost of generic zithromax
ivermectin 0.1 cost of stromectol
average cost of generic zithromax zithromax 500 mg for sale or zithromax online usa
http://labeirut.net/__media__/js/netsoltrademark.php?d=zithromax.science zithromax 250 mg
where can i buy zithromax uk generic zithromax 500mg india and where can i buy zithromax uk zithromax capsules price
doxycycline 100mg online doxycycline 100mg dogs or doxycycline 100mg
http://mastercopyright.com/__media__/js/netsoltrademark.php?d=doxycycline.science doxycycline hyclate
online doxycycline doxycycline 50mg and doxycycline 200 mg doxycycline monohydrate
amoxicillin 825 mg can you purchase amoxicillin online or medicine amoxicillin 500
http://mplscityhall.org/__media__/js/netsoltrademark.php?d=amoxil.science buy amoxicillin from canada
amoxicillin tablet 500mg amoxicillin cephalexin and can we buy amoxcillin 500mg on ebay without prescription buy amoxicillin online uk
doxycycline 100 mg cheap doxycycline online
https://zithromax.science/# zithromax purchase online
stromectol pills where to buy ivermectin cream
how much does ivermectin cost minocycline 100 mg tablets for human or where to buy stromectol
http://religiousindia.com/__media__/js/netsoltrademark.php?d=stromectol.science ivermectin price comparison
ivermectin ebay cost of ivermectin cream and cost of stromectol stromectol uk buy
https://zithromax.science/# average cost of generic zithromax
buy doxycycline without prescription 200 mg doxycycline
https://stromectol.science/# minocycline 100mg tablets online
amoxicillin 500 mg price order amoxil
https://stromectol.science/# generic ivermectin for humans
zithromax for sale zithromax online usa
Best and news about drug. Long-Term Effects.
ivermectin 90 mg
All trends of medicament. Get information now.
Best and news about drug. All trends of medicament.
https://stromectolst.com/# generic ivermectin for humans
Long-Term Effects. Read information now.
Prescription Drug Information, Interactions & Side. Top 100 Searched Drugs.
https://stromectolst.com/# stromectol cvs
Read here. Top 100 Searched Drugs.
Read now. Long-Term Effects.
stromectol 3 mg tablets price
What side effects can this medication cause? Drugs information sheet.
stromectol 3mg tablets stromectol 12mg or ivermectin lice
http://baycarehealthaccess.org/__media__/js/netsoltrademark.php?d=stromectolst.com stromectol medicine
ivermectin 500mg cost of ivermectin 1% cream and ivermectin australia stromectol tab price
Definitive journal of drugs and therapeutics. What side effects can this medication cause?
https://stromectolst.com/# ivermectin iv
Read here. Generic Name.
Read here. Commonly Used Drugs Charts.
ivermectin usa price
Learn about the side effects, dosages, and interactions. Actual trends of drug.
ivermectin gel stromectol medication or stromectol australia
http://platinumadvisors.biz/__media__/js/netsoltrademark.php?d=stromectolst.com how much is ivermectin
ivermectin 3mg tablets cost of ivermectin medicine and where to buy ivermectin ivermectin rx
Commonly Used Drugs Charts. Read here.
ivermectin new zealand
Long-Term Effects. Read information now.
ivermectin 8000 mcg ivermectin price canada or stromectol tab
http://unfcu.me/__media__/js/netsoltrademark.php?d=stromectol.science buy stromectol online uk
ivermectin stromectol ivermectin pill cost and minocycline 100mg tablets ivermectin 15 mg