1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

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

Discussion in 'Software Development' started by ElementOfMagic, Jan 1, 2013.

Thread Status:
Not open for further replies.
Advertisement
  1. ElementOfMagic

    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)
     
  2. TheShooter93

    TheShooter93 Malware Specialist

    Joined:
    Jul 9, 2008
    Messages:
    11,242
    First Name:
    Cody
    That will give you an error - what is "n" defined as?
     
  3. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    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:).
     
  4. TheShooter93

    TheShooter93 Malware Specialist

    Joined:
    Jul 9, 2008
    Messages:
    11,242
    First Name:
    Cody
    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.
     
  5. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    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?
     
  6. TheShooter93

    TheShooter93 Malware Specialist

    Joined:
    Jul 9, 2008
    Messages:
    11,242
    First Name:
    Cody
    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".
     
  7. ElementOfMagic

    ElementOfMagic Thread Starter

    Joined:
    Dec 30, 2012
    Messages:
    52
    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);
    		}
    	
    	}
    	
    }
    
     
  8. ElementOfMagic

    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)
     
  9. TheShooter93

    TheShooter93 Malware Specialist

    Joined:
    Jul 9, 2008
    Messages:
    11,242
    First Name:
    Cody
    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.
     
  10. Sponsor

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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1083305

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice