---
title: "Finding error in the following if statement using java"  
description: "Finding error in the following if statement using java"  
author: "E E Cummings"  
published: 2013-10-05  
updated: 2013-10-05  
canonical: https://www.mindstick.com/forum/1636/finding-error-in-the-following-if-statement-using-java  
category: "java"  
tags: ["java"]  
reading_time: 1 minute  

---

# Finding error in the following if statement using java

Find the [error](https://yourviews.mindstick.com/view/88527/fixing-quickbooks-error-4120-reinstalling-vs-repairing) in the following if statement that is intended to [select](https://www.mindstick.com/forum/160534/orderby-then-select-vs-select-then-orderby-performance) a [language](https://www.mindstick.com/startup/28/preply-the-fast-growing-platform-transforming-language-learning) for a given [country](https://yourviews.mindstick.com/view/85543/do-you-think-that-french-will-be-islamic-country) and state/province. (5 points)

\

```
language = "English";
if (country.equals("Canada"))
    if (stateOrProvince.equals("Quebec"))
         language = "French";
    else if (country.equals ("China"))
language = "Chinese";
```

\

## Replies

### Reply by Dag Hammarskjold

Braces are important. Your code actually gets executed as

```
if (country.equals(“Canada”)) {
  if (stateOrProvince.equals(“Quebec”)) {
    language = “French”;
  } else if (country.equals (“China”)) {
    language = “Chinese”;
  }
}
```

So, your country.equals("China") block would never get executed because within that block country.equals("Canada") is already true. Rewrite your code with proper braces as\

```
if (country.equals(“Canada”)) {
  if (stateOrProvince.equals(“Quebec”)) {
    language = “French”;
  }
} else if (country.equals (“China”)) {
  language = “Chinese”;
}
```


---

Original Source: https://www.mindstick.com/forum/1636/finding-error-in-the-following-if-statement-using-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
