0

I am using a for loop to print the backwards alphabet in uppercase but I would like to know how to do the same thing with a while loop, which I am not very good with.

String alphabet = "abcdefghijklmnopqstuvwxyz";      
int x = 0;

for(x = alphabet.length() - 1; x > -1; x--) {
  System.out.print(alphabet.charAt(x));
  System.out.print(alphabet.toUpperCase());                 
}

I also need to terminate it when it reaches A. think it's something like this, but I don't know how to make it loop backwards. I'd really appreciate your help!

while(alphabet.length() < 26) {
  System.out.print(alphabet.charAt(x));  
  System.out.print(alphabet.toUpperCase());     
  if(x == A) {
    break;
  }     
}
6
  • 8
    "for(A; B; C) { body; }" is equivalent to "A; while(B) { body; C; }" Commented Oct 27, 2012 at 16:50
  • @ignis You should add that an answer :). Commented Oct 27, 2012 at 16:51
  • 1
    The while(alphabet.length()<26) {} loop will never terminate. Commented Oct 27, 2012 at 16:51
  • @ignis that would be the answer. Commented Oct 27, 2012 at 16:52
  • ... And there's no need to iterate over a string (as long as you want the english alphabet): start with char c = 'Z', and while c >='A', print and then decrement c. You can do it in a single 'for' statement. Commented Oct 27, 2012 at 16:56

4 Answers 4

4
for (initialization; condition; increment) {

}

is same as:

{
    initialization;
    while(condition) {
       // body
       increment;
    }
}

The outer block creates a block scope for the initialized parameter, that we get in a for loop also. But, if you are declaring your for-loop variable outside the loop, then you can omit that outer block.

Now you can map your for loop to the while loop.

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

4 Comments

@user1707517. Yeah. both the loops work exactly in the same way with some modification in structure. :)
I would also place second code in outer block like { initialization; while(condition) { body; increment;} } since variables declared in initialization wont be accessible after that block.
@Pshemo. Ah! true. nice catch. Will add it. :) +1 on it.
The translation is not so simple if there are continues in the loop. In the for loop, a continue jumps to the increment code, in the while loop that would be skipped. Not sure what the best way to treat that is, without goto.
2

There is a much simpler way to reverse a string in Java.

public class StringReversalExample {
    public static void main(String[] args) {
      String alphabet = "abcdefghijklmnopqstuvwxyz";
      String reversedString = new StringBuilder(alphabet).reverse().toString();
      System.out.println(reversedString);
    }
}

Comments

2

Try this:

public static void main(String[] args) {
  char[] alphabet =  "abcdefghijklmnopqstuvwxyz".toCharArray();
  int index = alphabet.length - 1;

  while (index >= 0) {
    System.out.println(alphabet[index--]);
  }
}

This is a most efficient solution with minimal memory overhead.

Comments

0
x = alphabet.length() - 1;
while( x > -1 )
    {
        System.out.print(  alphabet.charAt( x ));  
        System.out.print( alphabet.toUpperCase() );
        x--;
    }

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.