Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dead code warning in the i++ part in the for loop

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);
    }
}
like image 435
user1712638 Avatar asked Dec 01 '22 21:12

user1712638


2 Answers

    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.

like image 188
Rohit Jain Avatar answered Dec 15 '22 19:12

Rohit Jain


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.

like image 24
millimoose Avatar answered Dec 15 '22 19:12

millimoose