1

the code executes fine but the warning still bothers me. I have used the variable decNumber we were instructed to use only for loop

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner in = new Scanner(System.in);

    int decNumber,rem=0;

    System.out.print("Enter a decimal number: ");
    decNumber = in.nextInt();

    for(int i=0;decNumber>0;i++) {


                rem = (decNumber%2);
                decNumber = (decNumber/2);
                System.out.println(rem);


    }
Lana
  • 55
  • 1
  • 5
  • 4
    Were you assuming that we could all see your screen? _What_ warning? – ajb Jul 26 '17 at 04:21
  • Sure, but wouldn't a `while-loop` or `do-while` loop be better? – MadProgrammer Jul 26 '17 at 04:22
  • sorry. i'm new here. forgot to mention where the warning was. it's found in the conditional expression in the for loop. It says "The value of the local variable i is not used. I was just wondering if it's okay neglect it. – Lana Jul 26 '17 at 04:28
  • The warning makes a good point. What's the point of defining `i`? If you think you have to have an `i` in order to have a loop, then (1) you can have `for` loops with empty parts, so you could just delete `int i = 0` and `i++` (but not the semicolons), and (2) if you don't need a variable like `i`, then your loop is in essence a `while` loop (but I see that you weren't suppose to use one). – ajb Jul 26 '17 at 04:31

3 Answers3

7

This would be a bit more kosher:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter a decimal number: ");
        int decNumber = in.nextInt();

        for (int i = decNumber; i > 0; i = i / 2) {
            int rem = (i % 2);
            System.out.println(rem);
        }

        in.close();
    }
}

The advantages here are

  1. You don't destroy the decNumber variable, so it can still be used later on
  2. The for loop keeps internal behaviour (i.e. rem) from bleeding out
  3. The for loop is actually used like a for instead of a while
Daniel Centore
  • 3,220
  • 1
  • 18
  • 39
2

You didn't add the warning but I am sure that doesn't matter, considering context of your question.

You must know the difference between a for and while loop. A good explanation can be found here.

In your code, you're not using the local variable i to control your loop. Your loop is controlled by decNumber>0, so for is totally unnecessary here.

A better way is to use while

while ( decNumber > 0 ) {
    rem = (decNumber%2);
    decNumber = (decNumber/2);
    System.out.println(rem);
}
Junaid
  • 1,270
  • 3
  • 14
  • 33
  • yes that's what i thought also but since my instructor wanted us to use for loop maybe I'll find another way for i. Thank you! – Lana Jul 26 '17 at 04:31
2

In your code, you're not using i at all. In fact, decNumber is serving as the iteration index, because it's the thing that changes from one iteration of the loop to the next. You have lines in your code that perform the functions of all three parts of a for loop -

  • initialisation: int decNumber = in.nextInt();
  • continuation condition: decNumber > 0
  • increment: decNumber = decNumber / 2, which you can also write as decNumber /= 2.

Note also that since you only use rem inside the loop, it's better to declare it inside the loop, than right at the beginning.

Putting all this together, you can remain in the spirit of a for loop, by rewriting your code as follows.

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.print("Enter a decimal number: ");
    for( int decNumber = in.nextInt(); decNumber > 0; decNumber /= 2 ) {
         int rem = decNumber % 2;
         System.out.println(rem);
    }
}

This makes it clear how your loop is initialised, what the condition is that makes it continue, and what changes from one iteration to the next; so is better for readability than using a redundant index variable.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110