Hi folks..


Can you tell how many times the loop is executed with exact reason.

int c=10;
while(c>1)
{
  c++;
}

System.out.println("loop over.");

I am expecting an indepth answer...

Recommended Answers

All 20 Replies

Zero Time. Check ur condition its a straight forward maths question.
1 cant be greater then 1, which causes loop condition false and it ends before it starts.

I am a really such an ...:)

its a typo...

int c=10; (it is not c=1)

int c=10;
while(c>1)
{
  c++;
}


System.out.println("loop over.");

If it was c=1 , it will be a idiotic question as i know..

Infinite Loop then... 10>1 after first pass it will be 11>1 and then so on....

That's where the logic lies..

Your answer is correct theorotically but practically the answer is different ..
Observe the code carefully...
The loop will terminate .
..

int has its limit, it cant go beyond that. So loop terminate. Try long or double it will even grab more time then int.

int c=10;
while(c<10)
{  
  System.out.println("loop inside No -> " + c);
  c++;
}
System.out.println("loop over.");

2147483637

As int is 32 bit signed number.. from -2147483648 to 2147483647

as c starts from 10 so subtract 10 from its +ve range ;)

@Majestics
That's what my main intention in posting this question..
I already know the answer that int has a range..
but how many times does it is looping and what exactly is the value of c ..

Can I expect an accurate explanation...

What happens if you add a long counter and execute the code and print out the value of the counter?

it will print (2^64)/2-1 :D

it will print

What will print that number?
No way it goes around the loop that many times with an int control.
The int c will increment from c=7fffffff to c=80000000, a large positive number to a large negative number.

long lpCnt = 0;
      int c= 10;
      while(c > 1 )       {  
        c++;
        if(lpCnt >= 2147483636)
          System.out.println("c=" + Integer.toHexString(c) + " " + c); // c=7fffffff  c=80000000
        lpCnt++; // count loops
      }
      System.out.println("loop over. lpCnt=" + lpCnt); // loop over. lpCnt=2147483638

ahan thanks for info :)

well, the number of times will depend on the bit number of ur system.for 32bit system,i think the last value of c will be 32757,then after it gives an error msg on incrementing c because the vslue of c has exceeded the positive range value of int datatype

Have you read the specifications for java's data types. Their sizes are fixed, not machine dependent.

Ok. I understand the range can be from -2147483648 to 2147483647.
But why is the answer negative? O I get it not it goes all the way up to 2147483647 and then it comes to the bottom of the rang which is negative. - is less than one so it ends the loop and c is the bottom of the range

Since you started at 10 and it ends at the bottom of the range with a negative number. giving it that one extra loop. take nine from the top of the range. 2147483638.

Useful way to figure out what you computer usage is.
Example my comp did it in 4 sec
so CalcPerSec = 536870912.
Can that possibly be right?

Hi folks..


Can you tell how many times the loop is executed with exact reason.

int c=10;
while(c>1)
{
  c++;
}

System.out.println("loop over.");

I am expecting an indepth answer...

size of int is 4 bytes and range is -2,147,483,648 to 2,147,483,647.

so loop will execute 2147483638 times.

The reason is :

when c will reach its maximum range 2,147,483,647 still it satisfies the c>1 condition.

so in last ideration 2,147,483,647 will become as -2147483648 because of c++, finally condition will get fails because of negative value.

Initially c value was 10.

so (2147483647-10) + 1(last ideration) = 2147483638 times loop will get execute !

My purpose got satisfied..
When i first saw that question , i thought that it was a silly question but when i think deeply it's a kind of interesting , logical question. Eventhough i know the answer , just posted because to know different ideas and comments from you all people...

@Muralidharan.E... i like your straight forward answer....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.