简体   繁体   中英

How to Decrement a loop in Java

I am trying to print the output of numbers from 50 to 40 by decrementing the numbers to 1 and to 2.

I've tried using for loop and do while loop to decrement the given numbers but it only decrements to one by one

package hahas;

public class Loop {

    public static void main(String[] args) {
        int x;
        int y = 50;

        do {
            System.out.println("X is = " + y);              

            if (y >= 0) {
                y -= 1;
            }
        } while(y > 40);
    }
}

This is the actual output:

X is = 50
X is = 49
X is = 48
X is = 47
X is = 46
X is = 45
X is = 44
X is = 43
X is = 42
X is = 41
X is = 40

The expected output is:

X is = 50
X is = 49
X is = 47
X is = 44
X is = 40

You want to decrease the loop values in incremental of 1 right? Here you need to update the code as below.

public class Loop {

    public static void main(String[] args) {

        int x=50, y= 1;
        do {

          System.out.println("X is = " + x);             
          if (x >= 0) {
            x -=y++;
          }
        }while(x>=40);
    }
}

In above example,
For decrement I've used x and for increment I've used y so loop will turn from 50 - 40 and y will helpfull to decrese values in Incremental form.


Updated answer

@Goion has suggested, you can use for-loop

public class Loop {

    public static void main(String[] args) {

        int y= 1; //To decrese 'x' in incremental order of `y`

        for(int x=50; x>=40; x-=y++){
          System.out.println("X: " + x);
        }
    }
}

You have to update the value of your decrement too. Your code is just subtracting 1 on each iteration. To achieve your goal you need to use a variable to keep the value that you will deduct from y. You can also use x for that, as it's not used on the code.

public class Loop {

    public static void main(String[] args) {
        // the initial value
        int x = 1;
        int y = 50;

        do {
            System.out.println("X is = " + y);              

            if (y >= 0) {
                y -= x;
                x += 1; // or x++;
            }
        } while(y > 40);
    }
}

I think that's what you need to do to decrease by 1 and 2

public class Loop {

public static void main(String[] args) {

 int x = 0;
 int y = 50;

 do {
  System.out.println("output is = " + y);


  if (x % 2 == 0)
   y -= 1;
  else
   y -= 2;
  x++;

 }

 while (y >= 40);
}

@Edray Tuyor, In your program you were decrementing a value y by -1. Following is a solution.

public class Loop {

    public static void main(String[] args) {
        int counter = 1;
        int x = 50;
        while (x >= 40) {
            System.out.println("X is = " + x);
            x -= counter++;
        }
    }
}

Here, this code counter is getting increment by +1 when while loop executes and a value X is decrementing by value counter .

If you want your expected output you can do

package hahas;

public class Loop {

    public static void main(String[] args) {
        int x=1;
        int y = 50;

        do {
            System.out.println("X is = " + y);  
             y-=x;
             x++;
        } while(y > 40);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM