Results 1 to 5 of 5
Thread: Replacing for with while loop.
- 10-21-2011, 01:13 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
Replacing for with while loop.
Hey there,
I am currently attending my first year of computer programming languages and I have my first tests in a few weeks. My teacher gave me an example of exercises we can expect on our test.
One of the test exercises is this:
I got this piece of code. And now I have to replace the while loop with the for loop, and the for loop with the while loop. So basically turn those 2 arround.Java Code:public void testWhileLoop(int limit) { int i = 1; while ( i <= limit ) { // Print a number of '*' characters for (int j=0; j<i; j++) { System.out.print("*"); } // Print a 'newline' character System.out.print("\n"); i++; } }
Basically what this code does is: It asks the user for an input int. And say for example the user inputs 4. This code outputs:
*
**
***
****
Can you guys help me?
Thanks in advance!
- 10-21-2011, 01:58 PM #2
Member
- Join Date
- Oct 2011
- Posts
- 5
- Rep Power
- 0
Re: Replacing for with while loop.
Here's a suggestion to get you started
Why not replace the while loop with a for loop first, and see if you can get the code to run properly. Think about what the i++ does.
After that try and change the while loop
- 10-22-2011, 10:01 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 2
- Rep Power
- 0
Re: Replacing for with while loop.
I tried to replace the loop but I just couldn't get a working solution. I kept getting endless loops or unwanted results. Could anyone help me a bit more on this one? Shouldn't be too much of a problem for
experienced java programmers I guess.
- 10-24-2011, 11:12 AM #4
Member
- Join Date
- Jul 2011
- Posts
- 26
- Rep Power
- 0
Re: Replacing for with while loop.
Its simple u can try.jst check the limit condition in for and no of time you need "*" in inside while.
- 10-24-2011, 11:43 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,606
- Blog Entries
- 7
- Rep Power
- 17
Re: Replacing for with while loop.
If you look at your code from a great distance, you see something like this:
I doesn't take rocket science to change the code to:Java Code:int i= 1; while (i <= limit) { // here is code that uses, but doesn't change, variable i i++; }
You can apply the same logic (but all reversed) to change your inner loop.Java Code:for (int i= 1; i <= limit; i++) { // here is code that uses, but doesn't change, variable i }
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Replacing(?) applet with another
By DarthCoffee in forum Java AppletsReplies: 2Last Post: 03-10-2011, 10:31 PM -
Replacing a char with the next
By hiei_yasha in forum New To JavaReplies: 14Last Post: 01-15-2011, 02:12 AM -
Replacing Else If
By jingly99 in forum Advanced JavaReplies: 5Last Post: 12-28-2009, 11:06 PM -
Help Replacing String
By 7oclock in forum New To JavaReplies: 5Last Post: 02-14-2009, 07:31 AM -
Replacing at an index
By bugger in forum New To JavaReplies: 2Last Post: 01-29-2008, 06:33 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks