dead code warning in the i++ part in the for loop
Asked Answered
B

2

5

This code keeps giving me a dead code warning on the i++ in the for loop and it is not incrementing the i for some reason!

import java.util.Scanner;


public class HideThatNumber {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int enc=input.nextInt();
        int tur=0;
        String test="";
        double x;
        for (int i=1;i<10;i++){
            test="";
            test+=i;
            test+=enc;
            x=Integer.parseInt(test);
            x/=11;
            if(x==Math.round(x));{
                tur=i;
                break;
            }
        }
        if(tur==0)
            System.out.println("Impossible");
        else 
            System.out.println(Integer.parseInt(test)/11);
    }
}
Bridegroom answered 27/10, 2012 at 15:46 Comment(0)
E
15
    if(x==Math.round(x)); <--semi-colon
    {
        tur=i;
        break;
    }

Inside your for loop, you have put a semi-colon at the end of your if. Thus the next block of code will be executed in any case, and thus you would break out of your loop after the first iteration.

    {
        tur=i;
        break;
    }

This block will be executed regardless of what your if condition evaluate to. And you break out of the loop.

And hence you get the warning, because i++ will never be executed.

Expressage answered 27/10, 2012 at 15:48 Comment(7)
+1 your answer came just as i spotted the error. Question: Is a codeblock inside curly brackets whithin a method really called initializer block? I don't know how to call them, but I thought initializer block is inside a class definition.Devaluation
YEs, they are initializer block. I also thought earlier that we can have them only in the class, but few days back I came to know this.Expressage
Actually, when we declare and initialize out instance variable outside a constructor, then the compiler puts that initialization code inside an initializer block in every constructor. And that's when I came to know that, rather we can have them inside any block.Expressage
@RohitJain That's not at all correct. A plain block inside a method is not an initializer block, since it's not initializing anything - it's not run as part of the constructor as initializer blocks would be. It's just a regular "block" that gets executed in sequence, and besides scoping local variables doesn't have much use.Grantham
@millimoose. But I read it somewhere, that the initiazation code is shifted to an initializer block in the constructor?? Is it wrong?Expressage
@RohitJain It is for initialiser blocks. Those are plain blocks that appear in the body of the class. (Meaning, not inside methods.) Plain blocks that are inside methods aren't shifted anywhere and thus are not initialiser blocks.Grantham
@millimoose. Ok. Got it. And updated post accordingly. Thanks for information. AT first I was also surprised on reading that. I guess I got my wrong information corrected now. :)Expressage
G
3

It's this line:

if(x==Math.round(x)); {

The semicolon shouldn't be there. In your code, the block with the break; always gets executed - so it breaks after the first iteration.

Grantham answered 27/10, 2012 at 15:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.