• Tech Travel Hub is your one stop, ultimate tour guide to all things tech, travel, visas and digital nomads
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
Menu
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
View Blog
May 9, 2023May 9, 2023

Dangling Else Problem in Java Explained in Just 5 Minutes

Dangling Else Problem in Java:

In my previous post on Execution control in java- here I have discussed the if-else to control of execution. In this post, we will check how else placement can go wrong. So if we misplaced else- it becomes dangling else.
 
In if statement is in particular, it is assumed that statement-1 or statement-2 will get executed.The problem arises if statement-1 is in an if statement that has no else.This is forbidden in java:

In the below example our objective is to check if the x value is greater than 5 and y value is greater than 10.For the given input of x=10 and x=4


public class HelloWorld{

public static void main(String []args){
int x=10;
int y=20;

if(x>5)
if(y>10)
System.out.println(“First case”);
else
System.out.println(“Second case”);
}
}

x=10
The output of the code:
$javac HelloWorld.java
$java -Xmx128M -Xms16M HelloWorld
First case
x=4
$javac HelloWorld.java
$java -Xmx128M -Xms16M HelloWorld

It is printing only the First case or no output at all. This is because of the dangling else problem. As the else part is closer to the second if, the JVM will treat this as part of the inner if. not the outer if. However, we can force the JVM to treat it differently by using a bracket.


public class HelloWorld{

public static void main(String []args){
int x=5;
int y=20;

if(x>15){
if(y>10)
System.out.println(“First case”);
}
else
System.out.println(“Second case”);
}
}

Now the output of the code is:
$javac HelloWorld.java
$java -Xmx128M -Xms16M HelloWorld
Second case

if else if construction:

if the code is like below:


if(boolean-expression)
statement-1
else if(boolean-expression)
     statement-2
     else
     statement-3
 

the JVM will treat this as:


if(boolean-expression)
statement-1
else if(boolean-expression)
     statement-2
else
     statement-3
 

It is kind of 3 way branching.
JVM will execute the first boolean-expression.If it is true,it will execute statement-1 and skips the remaining if conditions and go out of the if block.If the first expression is false it will execute the second boolean-expression and decide the statement-2 and statement-3.
The above could be modified as:


if(boolean-expression)
statement-1
else if(boolean-expression)
     statement-2
else if
     statement-3
 

In this way, it will become multiway branching.

Post navigation

Previous post
Next post

Umer Umer

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • The Core Element Of A Woocommerce Store And 5 Incredible Plugins
  • 15 Amazing Must-Have WooCommerce Plugins in 2021
  • How to Make A MP4 File Smaller Using Wondershare UniConverter – 5 Important Tips
  • Wondershare Filmii – Create Videos Easily with AI-Powered Video Editor With 4 Important Tips
  • 10 Wonderful Places in India

Categories

  • Adventure
  • Agile Methodology
  • AI
  • All world Tour
  • Android Development
  • Ansible
  • App
  • App Development
  • Artificial Intelligence
  • AWS
  • Bitcoin
  • BlockChain
  • Blogging
  • Box Packing
  • Business
  • business
  • Business Communication
  • buying guide
  • C
  • car
  • Care
  • Certification
  • Certification
  • Chatbot
  • cinema
  • Cloud Tech
  • Content Marketing
  • Corporate
  • covid-19
  • crucible
  • Cryptocurrency
  • Database
  • DevOps
  • DevOps
  • Digital Marketing
  • Drawing
  • Dress
  • Earn Money Online
  • Educational
  • Employment
  • Entertainment
  • Exceptions In Java
  • Fashion
  • Festivals
  • Finance
  • Fitness
  • Food
  • Game
  • Gift ideas
  • Graphic Designers
  • Groovy
  • Health
  • Health
  • home
  • home improvement
  • HR
  • IllegalThreadStateException in Java
  • India Tour
  • Instagram
  • Ios
  • ISO
  • Java
  • Javascript
  • job
  • Kids Section
  • Kitchen
  • Law
  • Life Style
  • Lifestyle
  • Lifestyle Home Improvement
  • Magento
  • Main Stories
  • Management
  • Mattress
  • Motorable
  • Movie
  • nursing
  • Office Productivity
  • Pendrive
  • Perspective
  • Pet
  • Popular
  • Programming
  • Python
  • RDT
  • Relationships
  • Selenium
  • SEO
  • Skin
  • SOA
  • SOAPUI
  • Social Media
  • Social Media Marketing
  • Software
  • software development
  • Software Development Model
  • Software Engineering
  • Software Testing
  • Sports
  • Startup
  • T Robot
  • TDD
  • Tech
  • Tech News
  • Tech News
  • Technology
  • Tool
  • Tour Guide
  • Tour Guide
  • Travel
  • UFT
  • Uncategorized
  • USB Cable
  • VBscript
  • VPN
  • Web Development
  • WebHosting
  • WebServices
  • Wildlife
  • Wordpress
  • Yoga
  • Youtube

Tags

Instagram Instagram Marketing Marketing SEO Thoptv App

Categories

  • Tech
  • Travel
  • Visas
  • Digital Nomad
  • Tech
  • Travel
  • Visas
  • Digital Nomad

Publishing

If you would like to publish on our site, please email admin@techtravelhub.com

Quick Links

  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact

Contact

Tech Travel Hub LLC

Address:
8 The Green STE A
Dover
Delaware, 19901

Phone: +1 302 956 9948