0

Why does my program skip the while loop?

class DigitRepeat
{
  public static void main(String args[])
  {
      int n=133,dig=3,digit,count=0;

      while(n!=0)
      {
         digit=n%10;
         if(dig==digit)
         {
            count=count++;
         }
         n=n/10;
      }

      if(count==0)
          System.out.println("digit not found");
      else
          System.out.println("digit occurs "+count+" times.");
  }
}
sarnold
  • 102,305
  • 22
  • 181
  • 238
prj
  • 11
  • 1

6 Answers6

10

> count=count++;

should be

> count++;

explain:

> count=count++;
is
a_temp_var=count;
count=count+1;
count=a_temp_var;
equals to:
a_temp_var=count;
count=a_temp_var;
equals to do nothing.

wuxb
  • 2,572
  • 1
  • 21
  • 30
  • 3
    To explain this: `count++` is a post-increment. It first assigns the value of count to the left side and then increments. On the other hand you have the pre-increment `++count` which does the inverse. SO you /could/ write `count = ++count`, but that isn't good style. – Pascal Wittmann Jun 05 '11 at 09:55
  • True but it's not his problem. his condition to enter the loop is false so it never goes in. (but he needs to change count++ also – Jason Rogers Jun 05 '11 at 09:56
  • @Jason, It does go into the loop. – Peter Lawrey Jun 05 '11 at 09:57
6

If I look at the code in my IDE it warns

The value changed at count++ is never used.

i.e. it is warning me that the value calculated is discarded.

If I step through the code in my debugger, I can see the loop is executed but the line

count = count++;

does not change count.

You want either

count++;

count+=1;

count=count+1;
R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 2
    +1 For a real explanation including the mention of compiler/IDE warnings and using the debugger. Who cares about what answer was fastest? – R. Martinho Fernandes Jun 05 '11 at 10:00
  • 1
    These things are easy once you know the answer, but when you don't know I ask myself; How was I supposed to work that out? Hopefully, the OP will know how to solve this problem and many like it. ;) – Peter Lawrey Jun 05 '11 at 10:15
  • what is the excellent IDE? pls let us know? I have not been using IDE for long time.. – wuxb Jun 05 '11 at 12:08
  • There are a number of popular IDEs; Eclipse, Netbeans and my favourite IntelliJ CE (all free) – Peter Lawrey Jun 05 '11 at 18:32
1

I do not know what do you mean that the program skips something. But I think I can see your bug. It is here: count=count++;

++ operator increments count after the assumption, so the variable count remains 0 forever.

I think that you wanted to say count++;

AlexR
  • 114,158
  • 16
  • 130
  • 208
1

You've got a little error in your code:

count = count++;

Should be change to:

count++;

Have a look over here for a running example, all I did was remove the assignment.

(Included for completeness)

  int n = 133;
  int dig = 3;
  int digit;
  int count = 0;

  while (n != 0)
  {
     digit = n % 10;
     if (dig == digit)
     {
        count++;
     }
     n = n / 10;
  }

  if(count = =0)
      System.out.println("digit not found");
  else
      System.out.println("digit occurs " + count + " times."); 
Kevin
  • 5,626
  • 3
  • 28
  • 41
0

First advice: when you think your program is skipping a loop add a print after the while it will help you narrow down the problem.

Then you have to be careful with post increments. the value countRight is assigned to countLeft then countLeft is incremented but it doesn't matter because the value of count is already set. the value of count is copied therefore when the ++ takes effect its on a different count (sorry if this isn't so clear).

you can use :

count++;

count = count +1;

count += 1;

or count = ++count;

the last one pre increment works because the value is incremented before being copied ^^

(read up on this because it always turns up in interviews ^^

Jason Rogers
  • 19,194
  • 27
  • 79
  • 112
0

Here is kinda what you're trying to code in C#:

class Program
{
    static void Main(string[] args)
    {
        var number = 12289;
        var search = 2;

        var count = Search(number, search);
    }

    static int Search(int number, int search)
    {
        return number < 10 ?
            (number == search ? 1 : 0) :
            (number % 10 == search ? 1 : 0)
                + Search((int)Math.Floor(number / (double)10), search);
    }
}
Konstantin Tarkus
  • 37,618
  • 14
  • 135
  • 121