Solved: PLEASE READ! Java, Re-write 'for' with 'while'

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

ElementOfMagic

Thread Starter
Joined
Dec 30, 2012
Messages
52
Hello. :)

I was wondering whether anyone could possibly tell me, what result this Java ('For' loop) code fragment would generate and how I would go about re-writing it with a 'While' loop?

Code:
for (i = 1 ; i <= n ; i += 2)
{
System.out.println(i) ;
}
Many thanks in advance for any help/guidance/assistance. (y)
 

ElementOfMagic

Thread Starter
Joined
Dec 30, 2012
Messages
52
That will give you an error - what is "n" defined as?
It isn't at the moment, this fragment is all I've been given to work with.

I tried defining it myself, but when 'n' is set to 0, the program runs but doesn't do anything (it just goes straight to "program finished") and when 'n' is set to anything else, the program just outputs a 1 (I'm not sure if it's supposed to do that :confused:).
 

TheShooter93

Cody
Malware Specialist
Joined
Jul 9, 2008
Messages
11,250
If you set n to zero, the program doesn't run because that would translate to 1<=0 which is not true, then the loop wouldn't run.

If you set n to one, it would be 1 is less than or equal to 1, therefore it would run at least once. The line in the loop is to print whatever "i" is, therefore it would print 1.
 

ElementOfMagic

Thread Starter
Joined
Dec 30, 2012
Messages
52
If you set n to zero, the program doesn't run because that would translate to 1<=0 which is not true, then the loop wouldn't run.

If you set n to one, it would be 1 is less than or equal to 1, therefore it would run at least once. The line in the loop is to print whatever "i" is, therefore it would print 1.
Ahh okay, so based on that ^, would I be right in saying that as long as 'n' is defined as a number that is greater than or equal to 1, the program would print a 1?
 

TheShooter93

Cody
Malware Specialist
Joined
Jul 9, 2008
Messages
11,250
ElementofMagic said:
Ahh okay, so based on that ^, would I be right in saying that as long as 'n' is defined as a number that is greater than or equal to 1, the program would print a 1?
Yes. And depending on how much greater "n" is, the program may print more than 1.

Say for instance, if n is 10, it would print "1,3,5,7,9".
 

ElementOfMagic

Thread Starter
Joined
Dec 30, 2012
Messages
52
Yes. And depending on how much greater "n" is, the program may print more than 1.

Say for instance, if n is 10, it would print "1,3,5,7,9".
Ahh okay, thank-you. :)

Now that I understand the syntax, is there a way I could code it into a main class so that it runs correctly?
I've had a go at it, but for some reason I keep getting 'cannot find symbol' errors. :confused:

Code:
public class Task5
{
	
public static void main (String args [ ])
	{
		int n = 1;
	
		for (i = 1; i <= n; i += 2)		
		{
			System.out.println(i);
		}
	
	}
	
}
 

ElementOfMagic

Thread Starter
Joined
Dec 30, 2012
Messages
52
Nevermind, I've got it working now. :)

Thanks a lot for all your help though, it's much appreciated. (y)
 

TheShooter93

Cody
Malware Specialist
Joined
Jul 9, 2008
Messages
11,250
Nevermind, I've got it working now. :)

Thanks a lot for all your help though, it's much appreciated. (y)
Glad to hear it. (y)

You can mark this thread as solved by clicking the "Mark Solved" button at the top and bottom of the thread.
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Top