Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8698135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:40:21+00:00 2026-06-13T01:40:21+00:00

Suppose some situations exist where you would like to increment and decrement values in

  • 0

Suppose some situations exist where you would like to increment and decrement values in the same for loop. In this set of situations, there are some cases where you can “cheat” this by taking advantage of the nature of the situation — for example, reversing a string.

Because of the nature of building strings, we don’t really have to manipulate the iterate or add an additional counter:

public static void stringReversal(){
    String str = "Banana";
    String forwardStr = new String();
    String backwardStr = new String();

    for(int i = str.length()-1; i >= 0; i--){
        forwardStr = str.charAt(i)+forwardStr;
        backwardStr = backwardStr+str.charAt(i);
    }

    System.out.println("Forward String:  "+forwardStr);
    System.out.println("Backward String: "+backwardStr);   
}

However, suppose a different case exists where we just want to print a decremented value, from the initial value to 0, and an incremented value, from 0 to the initial value.

public static void incrementAndDecrement(){

   int counter = 0;

   for(int i = 10; i >= 0; i--){
       System.out.println(i);
       System.out.println(counter);
       counter++;
   } 
}

This works well enough, but having to create a second counter to increment seems messy. Are there any mathematical tricks or tricks involving the for loop that could be used that would make counter redundant?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T01:40:21+00:00Added an answer on June 13, 2026 at 1:40 am

    Well it looks like you just want:

    for(int i = 10; i >= 0; i--){
        System.out.println(i);
        System.out.println(10 - i);
    } 
    

    Is that the case? Personally I’d normally write this as an increasing loop, as I find it easier to think about that:

    for (int i = 0; i <= 10; i++) {
        System.out.println(10 - i);
        System.out.println(i);
    } 
    

    Note that your string example is really inefficient, by the way – far more so than introducing an extra variable. Given that you know the lengths involved to start with, you can just start with two char[] of the right size, and populate the right index each time. Then create a string from each afterwards. Again, I’d do this with an increasing loop:

    char[] forwardChars = new char[str.length()];
    char[] reverseChars = new char[str.length()];
    for (int i = 0; i < str.length(); i++) {
        forwardChars[i] = str.charAt(i);
        reverseChars[reverseChars.length - i - 1] = str.charAt(i);
    }
    String forwardString = new String(forwardChars);
    String reverseString = new String(reverseChars);
    

    (Of course forwardString will just be equal to str in this case anyway…)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose you have some complex JSON like: {A : valA, B : { C
Propose to consider the following problem. Suppose we have some composite object. Are there
suppose I have this string: some striinnngggg <a href=something/some_number>linkk</a> soooo <a href=someotherthing/not_number>asdfsadf</a> I want
Instinctively, I would say that this is impossible, as NHibernate needs to know some
I have a custom event handler, or I suppose some call it a custom
Suppose I have some class which has a property actor_ of type Actor .
Suppose I have some pointer, which I want to reinterpret as static dimension array
Suppose that I define some class: class Pixel { public: Pixel(){ x=0; y=0;}; int
Suppose an application with some forms and only one data module are created at
Suppose I have a file containing some lines: line 1 ... line 2 ...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.