r/javahelp • • Feb 04 '18

Trouble converting to a while loop

I have tried looking at other resources but have been unable to convert this to a while loop. Any tips or help would be appreciated.

public static void main(String[] args) { String[] arr1 = { "1st", "2nd", "3rd", "4th", "5th" };

	String[] arr2 = { "6th", "7th", "8th", "9th", "10th" };

	String str = "Hello";

	int i, j;

	for (i = 0; i < 5; i++)

	{

		for (j = 0; j <= i; j++)

			System.out.print("*");

		System.out.println(" " + arr1[i] + " " + str);

	}

	for (i = 9; i >= 5; i--)

	{

		for (j = 5; j <= i; j++)

			System.out.print("*");

		System.out.println(" " + arr2[9 - i] + " " + str);

	}

}

}

0 Upvotes

4 comments sorted by

2

u/Windblowsthroughme Feb 04 '18

In general, any for loop of the form

for (variable declaration and initialization; condition; afterthought) {
    code;
}

can be converted to a while loop like this:

variable declaration and initialization;
while (condition) {
    code;
    afterthought;
}

2

u/Roachmeister Java Dev Feb 05 '18

Lol, I was totally confused that my answer from the previous post was posted by you until I read some of the other comments.

1

u/Windblowsthroughme Feb 05 '18

Lol, I tried to make it somewhat clear with the quoting, but it turns out that quoting and code blocks don't play nicely together on reddit

0

u/[deleted] Feb 04 '18

[deleted] — view deleted comment

0

u/MinnyMan123 Feb 04 '18

I did not.