0

I have created two methods that take in a number and should count from that number to 0 and print it out. One method incorporates a while loop, which works fine. The other method uses a for loop. But for some reason, I'm not getting the expected output in the method that uses a for loop. How come?

 import java.util.*; 

 public class Methods 
 {
     public static void main(String[] args)
     {
         int n = 10;   
         countdown(n); 
         countdown2(n); 
     }

     public static int countdown(int num)
     {
         while(num >= 0)
         {
             System.out.println(num); 
             num--;
         }
             return 0; 
     }

     public static int countdown2(int number)
     {
         for(int i = number; i <= number; i--)
         {
             System.out.println(number);
             number--;
         }
             return 0;        
     }
 }
1
  • you're both decreasing number and using it as an exit condition for the loop,so it'll only loop number/2 times. Commented Jun 10, 2011 at 10:56

7 Answers 7

4

Because of the way you've structured the for loop conditions:

  1. i is initially set to the same value as number.
  2. The loop continues while i is less than or equal to number.
  3. i gets decremented every time round the loop.

Since i starts out as less than or equal to number, and gets smaller, you're not going to exit the loop any time soon. (This will only happen when i gets down to Integer.MIN_VALUE and then underflows to Integer.MAX_VALUE on the next iteration).

Changing the condition to i > 0 would be one way of resolving this; or you could initially set i to zero and increment it each time round the loop.

Sign up to request clarification or add additional context in comments.

Comments

2
 for(int i = number; i <= number; i--)

check loop your count is starting from number to number only. you should count from number to 0.

  for(int i = number; i >= 0; i--)

Comments

1

If ever a program is behaving in a way which does make sense, the first thing you should try is use the debugger to debug your code. This allows you to step through the program line by line and see what all the values are


If you have a while loop you can exchange it with a for loop like so (provide you don't have a continue statement)

for({initialise variable};{condition};{update expression}) {
    {do something}
}

with

{initialise variable}
while({condition}) {
    {do something}
    {update expression}
}

or

{initialise variable}
while(true) {
    if ({condition}) break;
    {do something}
    {update expression}
}

or

{initialise variable}
if({condition}) 
  do {
    {do something}
    {update expression}
  } while({condition});

Comments

1
 public static int countdown2(int number)
 {
     for(;number>=0;)
     {
         System.out.println(number);
         number--;
     }
         return 0;        
 }

Comments

0

That should work

public static int countdown2(int number)
{
    for(int i = number; i >= 0; i--)
    {
       System.out.println(number);
       number--;
    }
    return 0;        
}

Comments

0

The condition in the second loop should be

i >= 0

Comments

0
public static int countdown2(int number)
 {
     for(int i = number; i >= 0; i--)
     {
         System.out.println(number);
         number--;
     }
     return 0;        
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.